Biggest Error in Spline Interpolation
21 views (last 30 days)
Show older comments
I am using spline (standard MATLAB spline) to make piecewise interpolation of the dataset with 11 data points. I have to plot the error i.e. the difference between the spline result and the actual function in the given interval. I assume that I've understood my professor and done this correctly, although I was confused whether we should compute the error or just overlay the actual function and the spline interpolation in the plot.
I also have to, using the command fminbnd, find for which value of x is the biggest error in spline interpolation!
Here is the MATLAB code:
xx = linspace(-5, 5, 11)
yy = 1 ./ (1 + xx.^2)
pp = spline(xx,yy)
v = ppval(pp, x)
err = abs(y - v)
plot(x, err)
My question is since, fminbnd needs to have a function as a parameter, how do I determine the function that returns my err values in order to find the maximum in the interval -5, 5?
Thank you in advance for any help.
0 Comments
Answers (1)
dpb
on 6 May 2018
OK, good start and marks for posting work! :)
You didn't define x above; I presume you somewhere else did something like
x=linspace(-5,5,100); % many more points to evaluate spline at than used to fit
and
plot(xx,yy,'*-') % show the original data, linear interpolation between
hold on
plot(x,v,'r-') % and add the interpolant
Now to compute the error between the actual curve and the interpolated values, must also have the value of the function at places other than the fit points; by definition the spline interpolant passes through those points so the error is always zero.
What about
fnF=@(x) 1 ./ (1 + xx.^2);
fnErr=@(x) fnF(x)-ppval(pp,x);
once you have done the spline interpolation so the coefficients are evaluated and thus will be embedded into the anonymous function? That give you an idea?
2 Comments
John D'Errico
on 6 May 2018
Edited: John D'Errico
on 6 May 2018
Be careful about using fminbnd on a spline to find the maximum error. It is not that you cannot do so. But as with anything, it helps to foresee the problems, and know how to get around them.
A spline has the characteristic that it goes exactly through every data point that was used to build it. It is an interpolant after all, and that is what a spline does - interpolate.
So visualize the error of the spline, compared to the function.
xx = linspace(-5, 5, 11)
yy = 1 ./ (1 + xx.^2)
pp = spline(xx,yy)
What will be the error of the spline at the break points (thus xx)? The error must be ZERO at those locations.
What happens between the breaks? The spline can do anything it darn well pleases, as long as it tries to be as smooth as possible. And since we assume the function itself is smooth, then we hope the spline fits well between the data points. So what happens if you look at the error function for the spline, thus perhaps
abs(f(x)-S(x))
where S(x) is the spline fit to the function f? Our expectation is this plot would have scallops in it. (Actually, humps, since it is always positive.) It goes to zero at every break, but then will be some smooth positive value between each pair of breaks. It might even be zero at some intermediate points too. But my general expectation is it will look like a bunch of peaks.
Now since fminbnd is a minimizer, we might minimize the negative absolute error.
-abs(f(x)-S(x))
So what does this mean when you use an optimizer? Fminbnd is not a perfect, global optimizer. It can get stuck in a local min, just like any other method. And once fminbnd finds a location where that error is at a local extreme point, it will stop the search. Why look further, when you have found a local min?
So my recommendation would be one of several options.
1. Check EVERY interval in the spline to find the max error in each break point interval. Then take the best solution over all intervals. That assures you that you will have at least a very good chance of finding the globally maximum error.
2. The alternative is to sample the error of the spline at hundreds of points initially. Then choose the break point interval with the point that had the largest error from your initial sample. Use fminbnd on that interval.
Both of the above options will generally suffice to get fminbnd to find the location on the spline fit with the largest error overall.
One other point: I mentioned above that the error for a spline fit can actually have a zero at an intermediate location between a pair of breaks. If that happens, then the error plot will actually have an extra hump in it. In fact, I'm sure that with just a little thought and play, I could indeed choose a spline that would have zero error at such an intermediate location.
dpb
on 6 May 2018
+1 on comments re: particulars about how spline interpolants work; I certainly glossed over such...
See Also
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!