Placing restrictions on spline fits

5 views (last 30 days)
I am trying to use the spline function to get a fit for a set of data. My problem is that I need to specify the value for the local minima and I have not been able to find a way to accomplish this. An example code would be what is shown below:
x = -180:90:180;
y = [10 5 1 6 10]
cs = spline(x,[0 y 0]);
xx = linspace(-180,180,181);
yy = ppval(cs,xx);
plot(x,y,'o',xx,yy,'-');
I need to be able to specify that the local minima be 1, but the fit dips below that. I could do this by specifying the x-value for zero slope as well. Please point me in the direction of a function or tool that will allow me to do this. I have tried a few spline fits and pchip, but haven't gotten the curve fitting toolbox yet because I don't want to spend the money if it won't help. It must also be able to be periodic, that is the slope at the endpoints must also be zero.
Thanks

Accepted Answer

José-Luis
José-Luis on 20 May 2013
Edited: José-Luis on 20 May 2013
I don't think you can set a minimum for the y-values, at least not automagically. What you can do, however, is to specify a slope at the ends (left and right) of your spline. You could then split your data in two sets, one with x inferior to zero and one with x superior to zero and fit a spline on both sides, such that the slope to the left and right of zero is equal to zero. For your data, that should prevent it to dip below zero.
The problem would be that you would only have three points to which to fit the spline, and in this case the results look very much like straight lines.
x = -180:90:180;
y = [10 5 1 6 10]
cs = spline(x,[0 y 0]);
xx = linspace(-180,180,181);
yy = ppval(cs,xx);
plot(x,y,'o',xx,yy,'-'); hold on
test_left = csape(x(1:3),y(1:3),[0 0]);
test_right = csape(x(3:5),y(3:5),[0 0]);
y_left = ppval(test_left,xx(1:91));
y_right = ppval(test_right,xx(91:181));
plot(xx(1:91),y_left,'g--',xx(91:181),y_right,'g--');
And you would need the curve fitting toolbox, I'm afraid.
  2 Comments
Henry McCabe
Henry McCabe on 20 May 2013
Edited: Henry McCabe on 20 May 2013
Thanks for the help, I never thought of that.
José-Luis
José-Luis on 20 May 2013
No worries, hope it helps.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!