Interpolating with predefined max and min values

50 次查看(过去 30 天)
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

回答(3 个)

Matt J
Matt J 2015-9-22
编辑:Matt J 2015-9-22
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 个评论
Matt J
Matt J 2015-9-24
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
WAT 2015-9-25
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
WAT 2015-9-22
编辑:WAT 2015-9-22
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 个评论
Florian
Florian 2015-9-22
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.

请先登录,再进行评论。


Stephen23
Stephen23 2015-9-25
编辑:Stephen23 2015-9-25
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 个评论
WAT
WAT 2015-9-25
编辑:WAT 2015-9-25
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by