@Uwe Brauer: this time I formatted your code for you. In future you can do it yourself by selecting the code text and clicking the {} Code button.
MATLAB: Natural spline, unmkpp gives ‘wrong’ answer
spline natural unmkpp
Related Solutions
The name "natural" applied to the boundary conditions on a spline refer to the use of calculus of variations in the derivation of the spline equations. Why do I say this?
There are two boundary conditions options in csape that allow you to create a natural cubic spline in csape. "variational" explicitly creates the zero second derivative BCs. Again, the word "variational" should be the flag there. I suppose however if you have never used the calculus of variations, you might not notice the reference. For myself, I did not encounter that area of mathematics until I was in grad school.
Can you verify that the default for csape is NOT a natural cubic spline? Lets try csape with the default first.
x = linspace(0,2*pi,5);%Function
y = cos(x);spl1 = csape(x,y);% evaluate the second derivative at the end points
fnval(fnder(spl1,2),[0,2*pi])ans = -0.74302 -0.74302
Note that the second derivative of cos(x) at 0 and 2*pi would be expected to be NON-zero. In fact, the true second derivative of cos(x) at those points is -1. So if the spline was a perfect approximation to the curve, we would have gotten -1. In context, -0.74302 is not really that far off, and for a curve based on only 5 points, I wold say that is as good as I could possibly have expected. Therefore, fitting a natural cubic spline through these points with those end conditions would be a poor choice, because the natural cubic explicitly forces the second derivatives to be zero at those points. It is totally counter to what the data implies at those points.
But I expect this is why you were assigned this assignment, to learn why other choices of boundary conditions generally tend to work better. (It is exactly the thing I had been known to do when I taught about splines in the past.) Clearly the default for csape is NOT a natural spline.
Now, try it using the variational boundary conditions.
spl2 = csape(x,y,'variational');fnval(fnder(spl2,2),[0,2*pi])ans = -1.3878e-16 1.6653e-16
So the second derivative is now (effectively) zero at the ends, as we specified.
fnplt(spl1)hold onfnplt(spl2)plot(x,y,'o')legend('Default BCs','Natural BCs','Data')
The green curve is the natural spline. Note that the true underlying cosine function would actually have a zero first derivative at the ends of the interval, not a zero second derivative.
As such a natural spline would have been a relatively poor choice for this particular set of points. Regardless, now you know how to choose the boundary conditions you wanted to achieve. We could also have done it as:
spl2 = csape(x,y,'second',[0 0]);fnval(fnder(spl2,2),[0,2*pi])ans = -1.3878e-16 1.6653e-16
When I teach about the use of cubic splines, I like to explain that splines really fall into a class of models I like to call metaphorical models. If I look for the definition of metaphor, I find in Grammarly:
"A metaphor is a figure of speech that describes an object or action in a way that isn’t literally true, but helps explain an idea or make a comparison.".
We often build mathematical models for some process where we lack a model based on physical principles. In those cases, such a model can be thought of as a metaphor. It helps us to understand the process under study, even though the model is not directly applicable to that process. For example, Shakespeare, in Romeo & Juliet has Romeo in the balcony scene describing Juliet...
But soft! What light through yonder window breaks?
It is the east, and Juliet is the sun.
Arise, fair sun, and kill the envious moon,
Who is already sick and pale with grief,
That thou, her maid, art far more fair than she.
Be not her maid since she is envious.
Her vestal livery is but sick and green,
And none but fools do wear it.
William Shakespeare, Romeo and Juliet
In there we see Romeo describing Juliet, comparing her to the sun, a bright shining light. While we know that Juliet is not actually a star, with surface temperature of many thousands of degrees, the metaphor teaches us how Romeo sees her.
In the same sense, it is not uncommon to see other mathematical models used in a metaphorical context. For example, there are very good differential equation models used to predict the spread of viruses, thus pandemics. However, I can also point out instances where those same models were used in a metaphorical sense to predict the early sales of consumer appliances, even washing machines. The idea is that just as you catch a virus by being in contact with a person who has that virus, you might also buy a washing machine or a camera because your neighbor just bought one, and they told you how much they liked it.
So we use mathematical metaphors all the time when building models. We use a mathematical model of a real world process that we do understand, to help us to predict and understand the behavior of a real world process where we lack a direct model.
Even the name spline is a reference to the use of long thin strips of wood used to create smooth curves for those who would create the shape of the hull of a ship. You can even see such pieces of wood used by those who build wooden boats. For example, the fellows who are building Arabella use physical splines extensively in the lofting process:
You can also see Leo using splines when lofting the lines for Tally Ho.
I hope that gives you some background on splines when used in shipbuilding. One characteristic of such a curve is the ends of such a chape as created by a long thin piece of wood, is the ends of that curve will always have zero second derivative! That is, if you force a uniform, long thin piece of wood through a set of points, at each end of the curve it creates, there will be no force applied at the ends of the curve to bend it. Lacking a bending moment at the ends, the curve will approach a straight line at each end of the curve.
That behavior is perfectly "natural" for a piece of wood, when forced to pass through a sequence of points. And a spline indeed does a VERY nice job of fitting many such curves. However the natural cubic spline metaphor fails just a bit when we try to use a spline with natural end conditions and force it through points from a function where we know the end second derivatives are not in fact zero. As you see in the plot above, the green curve has the shape a long thin piece of wood would be expected to have when passed through those points. However, it is the wrong fundamental shape for that curve. The difference is not terrible, but it is also not terribly accurate for cos(x).
Now, let me redo the same modeling process for sin(x) over the same domain.
x = linspace(0,2*pi,5);ysin = sin(x);spl1 = csape(x,ysin);spl2 = csape(x,ysin,'variational');fnplt(spl1)hold onplot(x,ysin,'ro')fnplt(spl2)
In this case, the red curve is a very good approximation to the real shape of sin(x). It looks far closer to my eyes to what I would want, than is the curve in blue. Here you can see the end point second derivatives for the default spline interpolant, compared to the natural spline second derivatives.
fnval(fnder(spl1,2),[0,2*pi]) % default spline f"
ans = -1.6211 1.6211fnval(fnder(spl2,2),[0,2*pi]) % natural cubic f"
ans = 0 0
How well does the natural cubic predict the first derivatives at the end points? For sin(x), the true curve would have them exactly 1.
syms usubs(diff(sin(u),u),u,0)ans =1fnval(fnder(spl1,1),[0,2*pi]) % Default csape f' estimates
ans = 1.6977 1.6977 fnval(fnder(spl2,1),[0,2*pi]) % Natural cubic f' estimates
ans = 0.95493 0.95493
Those end point FIRST derivatives are now almost spot on for the natural cubic spline.
So when the end conditions make perfect sense in context, we truly did get a much better approximation to the desired curve.
In the same way in literature, a good metaphor allows us to accurately understand and predict some aspect about what it describes. What I hope you may take away from this is to choose your metaphors carefully. Now if only all of us could be as good at our jobs as William Shakespeare was at his job. But that is a horse of a different color.
Both interp2 and interp3 use n-dimensional tensor product linear interpolation, thus respectively bilinear and trilinear interpolation.
Interp2 uses bilinear interpolation.
Interp3 uses trilinear interpolation.
How do I know this? Both tools use griddedInterpolant, when called with the 'linear' option.
In the help for griddedInterpolant, we see the statement:
% The Method is one of the following:
% 'linear' - (default) linear, bilinear, trilinear,... interpolation
Just to be painfully sure, I checked. Both interp2 and interp3 produce a characteristic NONLINEAR interpolation, when applied along the diagonal of the corresponding 2-d and 3-d hypercubes. This is a known fact about bilinear and trilinear interpolation, that bilinear interpolation when applied through the main diagonal of a square produces a characteristic quadratic function along the path where X==Y. Likewise, a trilinear interpolant produces a characteristic cubic polynomial along the corresponding diagonal path where X==Y==Z.
Ok, I said this behavior is a known fact. I did not say it was a well known fact. Not difficult to prove though. Just for kicks, in case you might not believe me...
Vq = interp2(0:1,0:1,[0 1;1 0],0:.1:1,0:.1:1,'linear')Vq = 0 0.18 0.32 0.42 0.48 0.5 0.48 0.42 0.32 0.18 0plot(Vq)
You can do a similar test for interp3, generating a very pretty cubic curve.
Note that the nonlinear behavior of bilinear and trilinear interpolation along that diagonal are in fact a reason why other interpolation methods are sometimes chosen, when a linear interpolant is required. However, it is also true that any variety of "linear" interpolant will exhibit similarly interesting artifacts if you push them. And yes, there is at least one alternative method one can choose that also produces an interpolant that is arguably "linear".
Best Answer