Interpolating with predefined max and min values
18 views (last 30 days)
Show older comments
Hi all,
I'd like to interpolate a smooth function through a specified set of min max values. I've attached a sample with various positions on the x axis and y values either 0 or one. The resulting function should have its minimums at the 0- and maximums at the 1-positions with the min=0 and max=1. The new x should go from 1:565.
When using interp1 I get "overshoots" so the max>1 and min<0.
%Code generating the overshoots
clear; clc;
load('test.mat')
x = xy(1,:);
y = xy(2,:);
xi = 1:565;
yi = interp1( x, y, xi, 'spline' );
plot(x,y,'m.'); hold on; plot(xi,yi,'-')
ylim([-1 2])
How can I achieve my desired result? If you are a crack...a 2d solution would be awesome as well! ;)
Thanks a lot, Florian
0 Comments
Answers (3)
Matt J
on 22 Sep 2015
Edited: Matt J
on 22 Sep 2015
The SLM package on the File Exchange ( link ) allows you to impose Max/Min constraints (as well as many other kinds). It requires the Optimization Toolbox, however.
3 Comments
Matt J
on 24 Sep 2015
In addition to constraining the max/min, the routine gives the option of forcing the plot through a prescribed set of points (see the 'xy' input parameter). You will need to use a spline of higher order than 3, however. There aren't enough degrees of freedom in a cubic spline to both pass through all the points and control the max/min.
WAT
on 25 Sep 2015
You're right about the degrees of freedom, so what I said below about using a cubic spline doesn't apply.
I think you can make your life simpler (in the 1D case) if you don't actually need smoothness but instead just want continuous 0th and 1st derivatives.
In this case your interpolating polynomial between each pair of nodes would just be a cubic (4 unknowns) where f(x0) = y0, f(x1) = y1, f'(x0) = 0, f'(x1) = 0. The resulting function isn't "smooth" in the mathematical sense but it would look "smooth" (ie, continuous with no sharp edges).
Looking at the description a bit more, I have no clue how you'd generalize this to 2D though...
WAT
on 22 Sep 2015
Edited: WAT
on 22 Sep 2015
This strikes me as a homework question, which may end up requiring a very different format of an answer. Are you simply generating a set of points representing the interpolating function? And if so, how are you proving that this is a "smooth function"?
If you're fine solving this with MATLAB commands I think you might try something like
yi = interp1( x, y, xi, 'cubic' );
If you also need to know the exact expressions for each spline function (in order to prove that your solution is in fact smooth) then you'll also want to do something like
pp = interp1(x,y,'cubic','pp')
Then you'd have to show that the derivatives match at all of the points in x.
But be warned that if this is a homework assignment and the entire point is for you to set up and solve the system of equations that results from setting up your spline with the necessary conditions at each point in x then interp1 probably won't get you full credit.
It seems to me you could also solve this problem by interpolating with sine or cosine functions and that might even require less work than a cubic spline.
Stephen23
on 25 Sep 2015
Edited: Stephen23
on 25 Sep 2015
yi = interp1( x, y, xi, 'pchip' );
plot(x,y,'m.'); hold on; plot(xi,yi,'-')
ylim([-1 2])
Which creates this:
1 Comment
WAT
on 25 Sep 2015
Edited: WAT
on 25 Sep 2015
For now that's exactly the same as the 'cubic' option which was suggested above. Except it has the advantage that it won't get broken by a future release of MATLAB, which is helpful.
Reading the documentation, it sounds like the result is exactly what I was trying to describe in my comment. So my guess is that this should be what was needed, unless continuity of more than one derivative is required.
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!