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

Answer by Matt J
on 22 Sep 2015

Edited by Matt J
on 22 Sep 2015

Florian
on 22 Sep 2015

Matt J
on 24 Sep 2015

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...

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.

Florian
on 22 Sep 2015

Sign in to comment.

Answer by Stephen Cobeldick
on 25 Sep 2015

Edited by Stephen Cobeldick
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:

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.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.