plotting a natural cubic spline

Hello, im a bit confused on how to approach this problem im supposed to find and plot the cubic spline S satisfying S(0) = 1,S(1) = 3,S(2) = 3,S(3) = 4,S(4) = 2 and with S''(0) = S''(4) = 0.

2 Kommentare

darova
darova am 24 Mär. 2020
What does S(0) mean? Can you make a simple drawing? DO you have a picture or something?
it'
it's refering to the function/equation we're trying to find. like the function S(x)=1 when x is zero.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

John D'Errico
John D'Errico am 24 Mär. 2020
Bearbeitet: John D'Errico am 24 Mär. 2020

1 Stimme

Are you allowed to use existing code? So if you have the curve fitting toolbox, you can just use csape.
x = 0:4;
y = [1 3 3 4 2];
spl = csape(x,y,'variational')
spl =
struct with fields:
form: 'pp'
breaks: [0 1 2 3 4]
coefs: [4×4 double]
pieces: 4
order: 4
dim: 1
fnplt(fnder(spl,2))
yline(0);
As you can see, the second derivative as plotted is zero at the ends, and the second derivative curve is piecewise linear. So csape did as was needed, producing a natural cubic spline interpolant.
Plotting the plsine is pretty easy too.
fnplt(spl)
hold on
plot(x,y,'ro')
However, I would not be remotely surprised if your question is to actually formulate the equations and then solve for the spline yourself. After all, this is surely homework. So is that your need? (I'm not at all sure why you would be doing this for any other purpose than homework.)
If your need is to formulate the plsine completely from scratch, then my hope is you are not asking someone to do that part for you. It is easy enough to do anyway. Start by reading here:

4 Kommentare

John D'Errico
John D'Errico am 24 Mär. 2020
Let me know if you need some help in the code. I'll help, although I won't do it for you. But I can at least help you to get your code running, having used and written spline codes for 40 years.
John D'Errico
John D'Errico am 24 Mär. 2020
Note that in your other question about splines, you asked about the equations of the spline, as if you want to know the actual functional form of the spline. You need to explain more clearly what you are trying to do.
Justin Howard
Justin Howard am 26 Mär. 2020
ah okay got it thank you.
Justin Howard
Justin Howard am 26 Mär. 2020
I know how to do it by hand without the use of matlab , but our professor wanted us to redo it on matlab, and i just wasn't sure where to start to properly construct on there.

Melden Sie sich an, um zu kommentieren.

Robert U
Robert U am 24 Mär. 2020

0 Stimmen

Hi Justin Howard,
I guess there is a mistake concerning the second derivative at the start and end points of the requested spline interpolation. Usually just the slope is given which would be denoted S'.
You can use the function spline() to provide a cubic spline interpolation of the given points including the slopes at start and end points.
x = [0,1,2,3,4];
y = [0,1,3,3,4,2,0];
x_spline = 0:0.1:4;
y_spline = spline(x,y,x_spline);
fh = figure;
ah = axes(fh);
hold(ah,'on')
plot(ah,x,y(2:end-1),'o');
plot(ah,x_spline,y_spline)
Kind regards,
Robert

2 Kommentare

John D'Errico
John D'Errico am 24 Mär. 2020
Robert - actually, this is a request for a NATURAL cubic spline. This is the case where the SECOND derivative is forced to zero at each end point, not the first derivative. Here the name natural probably arises from the calculus of variations, where the name natural end conditions are exactly as indicated.
Robert U
Robert U am 24 Mär. 2020
Ah. Thanks for the clarification.

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 24 Mär. 2020

Kommentiert:

am 26 Mär. 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by