Placing restrictions on spline fits

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

 채택된 답변

José-Luis
José-Luis 2013년 5월 20일
편집: José-Luis 2013년 5월 20일

0 개 추천

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

Henry McCabe
Henry McCabe 2013년 5월 20일
편집: Henry McCabe 2013년 5월 20일
Thanks for the help, I never thought of that.
José-Luis
José-Luis 2013년 5월 20일
No worries, hope it helps.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by