Cubic Spline interpolation implementation
In the following code I am trying to implement the following
- write a function naturalSpline that implements cubic spline interpolation with natural boundary conditions
- Use a tridiagonal solver to solve the arising tridiagonal system for the first derivatives.
- The prototype of the function should read yy=naturalSpline(x,y,xx) where (x,y) are the input points and data, and xx are the points where the data should be interpolated.
I figured first I would start with the second bullet point, creating the tridiagonal solver. So this is just the Thomas algorithm. I spent some time to create this part of the code and I have formatted it below. But now I am trying to finish the first and third bullet points but I am not sure how to use what I have done already to finish those. Looking for some help with this! Thanks in advance.
import numpy as np
def TDMA(a,b,c,d):
n = len(d)
w= np.zeros(n-1,float)
g= np.zeros(n, float)
p = np.zeros(n,float)
w[0] = c[0]/b[0]
g[0] = d[0]/b[0]
for i in range(1,n-1):
w[i] = c[i]/(b[i] - a[i-1]*w[i-1])
for i in range(1,n):
g[i] = (d[i] - a[i-1]*g[i-1])/(b[i] - a[i-1]*w[i-1])
p[n-1] = g[n-1]
for i in range(n-1,0,-1):
p[i-1] = g[i-1] - w[i-1]*p[i]
return p
A = np.array([[10,2,0,0],[3,10,4,0],[0,1,7,5], [0,0,3,4]],dtype=float)
a = np.array([3.,1,3])
b = np.array([10.,10.,7.,4.])
c = np.array([2.,4.,5.])
d = np.array([3,4,5,6.])
print (TDMA(a, b, c, d))
Which gives the correct output, I even tested it against np.linalg.solve(a,b,c,d) to make sure it was correct
[ 0.14877589 0.75612053 -1.00188324 2.25141243]
python numpy
|
show 1 more comment
In the following code I am trying to implement the following
- write a function naturalSpline that implements cubic spline interpolation with natural boundary conditions
- Use a tridiagonal solver to solve the arising tridiagonal system for the first derivatives.
- The prototype of the function should read yy=naturalSpline(x,y,xx) where (x,y) are the input points and data, and xx are the points where the data should be interpolated.
I figured first I would start with the second bullet point, creating the tridiagonal solver. So this is just the Thomas algorithm. I spent some time to create this part of the code and I have formatted it below. But now I am trying to finish the first and third bullet points but I am not sure how to use what I have done already to finish those. Looking for some help with this! Thanks in advance.
import numpy as np
def TDMA(a,b,c,d):
n = len(d)
w= np.zeros(n-1,float)
g= np.zeros(n, float)
p = np.zeros(n,float)
w[0] = c[0]/b[0]
g[0] = d[0]/b[0]
for i in range(1,n-1):
w[i] = c[i]/(b[i] - a[i-1]*w[i-1])
for i in range(1,n):
g[i] = (d[i] - a[i-1]*g[i-1])/(b[i] - a[i-1]*w[i-1])
p[n-1] = g[n-1]
for i in range(n-1,0,-1):
p[i-1] = g[i-1] - w[i-1]*p[i]
return p
A = np.array([[10,2,0,0],[3,10,4,0],[0,1,7,5], [0,0,3,4]],dtype=float)
a = np.array([3.,1,3])
b = np.array([10.,10.,7.,4.])
c = np.array([2.,4.,5.])
d = np.array([3,4,5,6.])
print (TDMA(a, b, c, d))
Which gives the correct output, I even tested it against np.linalg.solve(a,b,c,d) to make sure it was correct
[ 0.14877589 0.75612053 -1.00188324 2.25141243]
python numpy
It would be convenient to give some info on your setting, since some people may not know these formulas by heart. For example, is your polynomial p = a x^3 + ... + d or p = a + ... + d x^3 ? What is your specific problem? For spline interpolation, you solve the equations for (x,y) and get a number of polynomials that represent your spline. Now, for each point xx[i], you evaluate the polynomial that covers the range that the point in xx[i] lies in (the is exactly one k with xx[i] in [x_k, x_(k+1)] ).
– randomwalker
Nov 13 '18 at 8:55
@randomwalker it is f(x)=sinx on the interval [0,5pi] and the data y is given at 25 equally spaced points and should be interpolated to 250 equally spaced points , could you show how this is done? it would be greatly appreciated and I will accept your answer
– user123
Nov 13 '18 at 11:36
In your code, what does A, a, b, c, and d stand for?
– randomwalker
Nov 13 '18 at 12:30
just the arrays in the triagdigonal system
– user123
Nov 13 '18 at 13:34
I mean what do you expect them to be in your application of solving the spline equations, e.g. a=(x, f(x), f'(x))
– randomwalker
Nov 13 '18 at 13:49
|
show 1 more comment
In the following code I am trying to implement the following
- write a function naturalSpline that implements cubic spline interpolation with natural boundary conditions
- Use a tridiagonal solver to solve the arising tridiagonal system for the first derivatives.
- The prototype of the function should read yy=naturalSpline(x,y,xx) where (x,y) are the input points and data, and xx are the points where the data should be interpolated.
I figured first I would start with the second bullet point, creating the tridiagonal solver. So this is just the Thomas algorithm. I spent some time to create this part of the code and I have formatted it below. But now I am trying to finish the first and third bullet points but I am not sure how to use what I have done already to finish those. Looking for some help with this! Thanks in advance.
import numpy as np
def TDMA(a,b,c,d):
n = len(d)
w= np.zeros(n-1,float)
g= np.zeros(n, float)
p = np.zeros(n,float)
w[0] = c[0]/b[0]
g[0] = d[0]/b[0]
for i in range(1,n-1):
w[i] = c[i]/(b[i] - a[i-1]*w[i-1])
for i in range(1,n):
g[i] = (d[i] - a[i-1]*g[i-1])/(b[i] - a[i-1]*w[i-1])
p[n-1] = g[n-1]
for i in range(n-1,0,-1):
p[i-1] = g[i-1] - w[i-1]*p[i]
return p
A = np.array([[10,2,0,0],[3,10,4,0],[0,1,7,5], [0,0,3,4]],dtype=float)
a = np.array([3.,1,3])
b = np.array([10.,10.,7.,4.])
c = np.array([2.,4.,5.])
d = np.array([3,4,5,6.])
print (TDMA(a, b, c, d))
Which gives the correct output, I even tested it against np.linalg.solve(a,b,c,d) to make sure it was correct
[ 0.14877589 0.75612053 -1.00188324 2.25141243]
python numpy
In the following code I am trying to implement the following
- write a function naturalSpline that implements cubic spline interpolation with natural boundary conditions
- Use a tridiagonal solver to solve the arising tridiagonal system for the first derivatives.
- The prototype of the function should read yy=naturalSpline(x,y,xx) where (x,y) are the input points and data, and xx are the points where the data should be interpolated.
I figured first I would start with the second bullet point, creating the tridiagonal solver. So this is just the Thomas algorithm. I spent some time to create this part of the code and I have formatted it below. But now I am trying to finish the first and third bullet points but I am not sure how to use what I have done already to finish those. Looking for some help with this! Thanks in advance.
import numpy as np
def TDMA(a,b,c,d):
n = len(d)
w= np.zeros(n-1,float)
g= np.zeros(n, float)
p = np.zeros(n,float)
w[0] = c[0]/b[0]
g[0] = d[0]/b[0]
for i in range(1,n-1):
w[i] = c[i]/(b[i] - a[i-1]*w[i-1])
for i in range(1,n):
g[i] = (d[i] - a[i-1]*g[i-1])/(b[i] - a[i-1]*w[i-1])
p[n-1] = g[n-1]
for i in range(n-1,0,-1):
p[i-1] = g[i-1] - w[i-1]*p[i]
return p
A = np.array([[10,2,0,0],[3,10,4,0],[0,1,7,5], [0,0,3,4]],dtype=float)
a = np.array([3.,1,3])
b = np.array([10.,10.,7.,4.])
c = np.array([2.,4.,5.])
d = np.array([3,4,5,6.])
print (TDMA(a, b, c, d))
Which gives the correct output, I even tested it against np.linalg.solve(a,b,c,d) to make sure it was correct
[ 0.14877589 0.75612053 -1.00188324 2.25141243]
python numpy
python numpy
edited Nov 13 '18 at 2:01
hpaulj
111k778146
111k778146
asked Nov 13 '18 at 0:55
user123user123
1468
1468
It would be convenient to give some info on your setting, since some people may not know these formulas by heart. For example, is your polynomial p = a x^3 + ... + d or p = a + ... + d x^3 ? What is your specific problem? For spline interpolation, you solve the equations for (x,y) and get a number of polynomials that represent your spline. Now, for each point xx[i], you evaluate the polynomial that covers the range that the point in xx[i] lies in (the is exactly one k with xx[i] in [x_k, x_(k+1)] ).
– randomwalker
Nov 13 '18 at 8:55
@randomwalker it is f(x)=sinx on the interval [0,5pi] and the data y is given at 25 equally spaced points and should be interpolated to 250 equally spaced points , could you show how this is done? it would be greatly appreciated and I will accept your answer
– user123
Nov 13 '18 at 11:36
In your code, what does A, a, b, c, and d stand for?
– randomwalker
Nov 13 '18 at 12:30
just the arrays in the triagdigonal system
– user123
Nov 13 '18 at 13:34
I mean what do you expect them to be in your application of solving the spline equations, e.g. a=(x, f(x), f'(x))
– randomwalker
Nov 13 '18 at 13:49
|
show 1 more comment
It would be convenient to give some info on your setting, since some people may not know these formulas by heart. For example, is your polynomial p = a x^3 + ... + d or p = a + ... + d x^3 ? What is your specific problem? For spline interpolation, you solve the equations for (x,y) and get a number of polynomials that represent your spline. Now, for each point xx[i], you evaluate the polynomial that covers the range that the point in xx[i] lies in (the is exactly one k with xx[i] in [x_k, x_(k+1)] ).
– randomwalker
Nov 13 '18 at 8:55
@randomwalker it is f(x)=sinx on the interval [0,5pi] and the data y is given at 25 equally spaced points and should be interpolated to 250 equally spaced points , could you show how this is done? it would be greatly appreciated and I will accept your answer
– user123
Nov 13 '18 at 11:36
In your code, what does A, a, b, c, and d stand for?
– randomwalker
Nov 13 '18 at 12:30
just the arrays in the triagdigonal system
– user123
Nov 13 '18 at 13:34
I mean what do you expect them to be in your application of solving the spline equations, e.g. a=(x, f(x), f'(x))
– randomwalker
Nov 13 '18 at 13:49
It would be convenient to give some info on your setting, since some people may not know these formulas by heart. For example, is your polynomial p = a x^3 + ... + d or p = a + ... + d x^3 ? What is your specific problem? For spline interpolation, you solve the equations for (x,y) and get a number of polynomials that represent your spline. Now, for each point xx[i], you evaluate the polynomial that covers the range that the point in xx[i] lies in (the is exactly one k with xx[i] in [x_k, x_(k+1)] ).
– randomwalker
Nov 13 '18 at 8:55
It would be convenient to give some info on your setting, since some people may not know these formulas by heart. For example, is your polynomial p = a x^3 + ... + d or p = a + ... + d x^3 ? What is your specific problem? For spline interpolation, you solve the equations for (x,y) and get a number of polynomials that represent your spline. Now, for each point xx[i], you evaluate the polynomial that covers the range that the point in xx[i] lies in (the is exactly one k with xx[i] in [x_k, x_(k+1)] ).
– randomwalker
Nov 13 '18 at 8:55
@randomwalker it is f(x)=sinx on the interval [0,5pi] and the data y is given at 25 equally spaced points and should be interpolated to 250 equally spaced points , could you show how this is done? it would be greatly appreciated and I will accept your answer
– user123
Nov 13 '18 at 11:36
@randomwalker it is f(x)=sinx on the interval [0,5pi] and the data y is given at 25 equally spaced points and should be interpolated to 250 equally spaced points , could you show how this is done? it would be greatly appreciated and I will accept your answer
– user123
Nov 13 '18 at 11:36
In your code, what does A, a, b, c, and d stand for?
– randomwalker
Nov 13 '18 at 12:30
In your code, what does A, a, b, c, and d stand for?
– randomwalker
Nov 13 '18 at 12:30
just the arrays in the triagdigonal system
– user123
Nov 13 '18 at 13:34
just the arrays in the triagdigonal system
– user123
Nov 13 '18 at 13:34
I mean what do you expect them to be in your application of solving the spline equations, e.g. a=(x, f(x), f'(x))
– randomwalker
Nov 13 '18 at 13:49
I mean what do you expect them to be in your application of solving the spline equations, e.g. a=(x, f(x), f'(x))
– randomwalker
Nov 13 '18 at 13:49
|
show 1 more comment
1 Answer
1
active
oldest
votes
For each interval [x_k, x_(k+1)], you can solve the four equations
- p_k(x_k) = f(x_k) = y_k
- p_k'(x_k) = f'(x_k) = d_k
- p_k(x_(k+1)) = f(x_(k+1)) = y_(k+1)
- p_k'(x_(k+1)) = f'(x_(k+1)) = d_(k+1)
(without checking your code, I assume that this is what you did).
From this, you can construct a dict
'polynomials': [ [a_0, ..., d_0], ..., [a_24, ..., d_24] ],
'knots': [x_0, ..., x_24]
For each x of your 250 point, you check for which k the point x is in the interval [x_k, x_(k+1)] and evaluate p_k(x).
All of this is straight forward mathematics and python coding. If something is not clear, you are better of learning more about both fields, instead of getting specialized advise on this website.
As I said, this is seriously basic stuff. At this point, if you do not understand it deep enough to produce the code yourself, you have to learn more about python and/or spline interpolation. A good start is the wikipedia article on splines.
– randomwalker
Nov 13 '18 at 13:54
thanks for your help!
– user123
Nov 13 '18 at 13:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272235%2fcubic-spline-interpolation-implementation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
For each interval [x_k, x_(k+1)], you can solve the four equations
- p_k(x_k) = f(x_k) = y_k
- p_k'(x_k) = f'(x_k) = d_k
- p_k(x_(k+1)) = f(x_(k+1)) = y_(k+1)
- p_k'(x_(k+1)) = f'(x_(k+1)) = d_(k+1)
(without checking your code, I assume that this is what you did).
From this, you can construct a dict
'polynomials': [ [a_0, ..., d_0], ..., [a_24, ..., d_24] ],
'knots': [x_0, ..., x_24]
For each x of your 250 point, you check for which k the point x is in the interval [x_k, x_(k+1)] and evaluate p_k(x).
All of this is straight forward mathematics and python coding. If something is not clear, you are better of learning more about both fields, instead of getting specialized advise on this website.
As I said, this is seriously basic stuff. At this point, if you do not understand it deep enough to produce the code yourself, you have to learn more about python and/or spline interpolation. A good start is the wikipedia article on splines.
– randomwalker
Nov 13 '18 at 13:54
thanks for your help!
– user123
Nov 13 '18 at 13:55
add a comment |
For each interval [x_k, x_(k+1)], you can solve the four equations
- p_k(x_k) = f(x_k) = y_k
- p_k'(x_k) = f'(x_k) = d_k
- p_k(x_(k+1)) = f(x_(k+1)) = y_(k+1)
- p_k'(x_(k+1)) = f'(x_(k+1)) = d_(k+1)
(without checking your code, I assume that this is what you did).
From this, you can construct a dict
'polynomials': [ [a_0, ..., d_0], ..., [a_24, ..., d_24] ],
'knots': [x_0, ..., x_24]
For each x of your 250 point, you check for which k the point x is in the interval [x_k, x_(k+1)] and evaluate p_k(x).
All of this is straight forward mathematics and python coding. If something is not clear, you are better of learning more about both fields, instead of getting specialized advise on this website.
As I said, this is seriously basic stuff. At this point, if you do not understand it deep enough to produce the code yourself, you have to learn more about python and/or spline interpolation. A good start is the wikipedia article on splines.
– randomwalker
Nov 13 '18 at 13:54
thanks for your help!
– user123
Nov 13 '18 at 13:55
add a comment |
For each interval [x_k, x_(k+1)], you can solve the four equations
- p_k(x_k) = f(x_k) = y_k
- p_k'(x_k) = f'(x_k) = d_k
- p_k(x_(k+1)) = f(x_(k+1)) = y_(k+1)
- p_k'(x_(k+1)) = f'(x_(k+1)) = d_(k+1)
(without checking your code, I assume that this is what you did).
From this, you can construct a dict
'polynomials': [ [a_0, ..., d_0], ..., [a_24, ..., d_24] ],
'knots': [x_0, ..., x_24]
For each x of your 250 point, you check for which k the point x is in the interval [x_k, x_(k+1)] and evaluate p_k(x).
All of this is straight forward mathematics and python coding. If something is not clear, you are better of learning more about both fields, instead of getting specialized advise on this website.
For each interval [x_k, x_(k+1)], you can solve the four equations
- p_k(x_k) = f(x_k) = y_k
- p_k'(x_k) = f'(x_k) = d_k
- p_k(x_(k+1)) = f(x_(k+1)) = y_(k+1)
- p_k'(x_(k+1)) = f'(x_(k+1)) = d_(k+1)
(without checking your code, I assume that this is what you did).
From this, you can construct a dict
'polynomials': [ [a_0, ..., d_0], ..., [a_24, ..., d_24] ],
'knots': [x_0, ..., x_24]
For each x of your 250 point, you check for which k the point x is in the interval [x_k, x_(k+1)] and evaluate p_k(x).
All of this is straight forward mathematics and python coding. If something is not clear, you are better of learning more about both fields, instead of getting specialized advise on this website.
answered Nov 13 '18 at 12:41
randomwalkerrandomwalker
2627
2627
As I said, this is seriously basic stuff. At this point, if you do not understand it deep enough to produce the code yourself, you have to learn more about python and/or spline interpolation. A good start is the wikipedia article on splines.
– randomwalker
Nov 13 '18 at 13:54
thanks for your help!
– user123
Nov 13 '18 at 13:55
add a comment |
As I said, this is seriously basic stuff. At this point, if you do not understand it deep enough to produce the code yourself, you have to learn more about python and/or spline interpolation. A good start is the wikipedia article on splines.
– randomwalker
Nov 13 '18 at 13:54
thanks for your help!
– user123
Nov 13 '18 at 13:55
As I said, this is seriously basic stuff. At this point, if you do not understand it deep enough to produce the code yourself, you have to learn more about python and/or spline interpolation. A good start is the wikipedia article on splines.
– randomwalker
Nov 13 '18 at 13:54
As I said, this is seriously basic stuff. At this point, if you do not understand it deep enough to produce the code yourself, you have to learn more about python and/or spline interpolation. A good start is the wikipedia article on splines.
– randomwalker
Nov 13 '18 at 13:54
thanks for your help!
– user123
Nov 13 '18 at 13:55
thanks for your help!
– user123
Nov 13 '18 at 13:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272235%2fcubic-spline-interpolation-implementation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It would be convenient to give some info on your setting, since some people may not know these formulas by heart. For example, is your polynomial p = a x^3 + ... + d or p = a + ... + d x^3 ? What is your specific problem? For spline interpolation, you solve the equations for (x,y) and get a number of polynomials that represent your spline. Now, for each point xx[i], you evaluate the polynomial that covers the range that the point in xx[i] lies in (the is exactly one k with xx[i] in [x_k, x_(k+1)] ).
– randomwalker
Nov 13 '18 at 8:55
@randomwalker it is f(x)=sinx on the interval [0,5pi] and the data y is given at 25 equally spaced points and should be interpolated to 250 equally spaced points , could you show how this is done? it would be greatly appreciated and I will accept your answer
– user123
Nov 13 '18 at 11:36
In your code, what does A, a, b, c, and d stand for?
– randomwalker
Nov 13 '18 at 12:30
just the arrays in the triagdigonal system
– user123
Nov 13 '18 at 13:34
I mean what do you expect them to be in your application of solving the spline equations, e.g. a=(x, f(x), f'(x))
– randomwalker
Nov 13 '18 at 13:49