MATLAB Answers

Florian
0

Interpolating with predefined max and min values

Asked by Florian
on 22 Sep 2015
Latest activity Edited by Matt J
on 25 Sep 2015
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

Sign in to comment.

3 Answers

Answer by Matt J
on 22 Sep 2015
Edited by 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

Thanks Matt. I tried it. Unfortunately it doesn't work since it a curve fitting routine rather than an interpolation routine. A curve is generated, but it doesn't pass through the min max values.
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.
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...

Sign in to comment.


Answer by WAT
on 22 Sep 2015
Edited by 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.

  1 Comment

Hey Wat, thanks for your answer. Good point with the cubic splines. Be assured its not some homework. I have a 2d surface generated with a simulation. The surface is not as smooth as I'd like it to be so I extracted the max and min points. The idea was then to interpolate them to generate smooth surface. griddata( X, Y, Z, Xq, Yq, 'cubic' ); already does a pretty good job, but still has some overshoots...I'll have to check some more.

Sign in to comment.


Answer by Stephen Cobeldick on 25 Sep 2015
Edited by Stephen Cobeldick on 25 Sep 2015

Easy, just use the method option 'pchip':
yi = interp1( x, y, xi, 'pchip' );
plot(x,y,'m.'); hold on; plot(xi,yi,'-')
ylim([-1 2])
Which creates this:
You might also be interested in the standalone pchip interpolation function.

  1 Comment

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.

Sign in to comment.