adjusting the peak of a spline in curve fitting
6 views (last 30 days)
Show older comments
I am trying to fit a part of a curve using smothing splines. I am using three main points. two end points and one peak point as shown in the image below. I would like to have the peak of the constructed polynomial to concide exactly on the peak point (marked in the blue circle), and not just for the fitted curve to pass through it. with the assumption that the two end point will still be fixed. any idea how this can be done?
Here i the code I am using with the data xx and yy attached
k=3;
sp = spapi(k,xx,yy);
fnplt(sp);
Answers (1)
John D'Errico
on 7 Feb 2023
Edited: John D'Errico
on 7 Feb 2023
Um, you are not being very clear here, probably because you don't totally appreciate what you are asking. Certainly you have not been clear.
If the function must pass exactly through that peak, AND it must attain its maximum at that point, then you know unequivocably that the derivative of the function is zero at that point.
It also appears, since you have given us only a file with three data points, that the curve must also pass exactly through those other points too? So then what does this have to do with a smoothing spline? For example, it is trivial to find a cubic polynomial that satisfies your goals.
load ws.mat
format long g
[xx,yy]
That is all the information you have shown us.
First, you cannot force a smoothing spline, as built by spapi to have the properties you wish. However, it is trivial to find the unique cubic polynomial that does have those properties. If a polynomial is defined as:
y = a*x^3 + b*x^2 + c*x + d
then you can simply formulate a set of 4 linear equations in those four unknowns. We can write it as:
A = [[xx.^3,xx.^2,xx,ones(3,1)];[3*xx(2).^2,2*xx(2),1,0]];
rhs = [yy;0];
abcd = (A\rhs)' % solve using backslash
Those are the coefficients of the unique cubic polynimal that passes through those three points, AND has a maximum at the center point. We can plot the points and that function to show you it does as required.
xint = linspace(xx(1),xx(3),500);
plot(xx,yy,'bo',xint,polyval(abcd,xint),'r-')
No smoothing spline required. Of course, my guess is, you really have more data, and you have decided to give us only three points. Even so, you still cannot force spapi to do what you wish it to do.
4 Comments
John D'Errico
on 9 Feb 2023
Edited: John D'Errico
on 9 Feb 2023
No. A cubic polynomial will not fit the entire curve. But you did not ask for that. You gave us 3 data points, and indicated what you wanted to happen.
Can you nudge the curve to pass through that data point by significantly increasing the weight? Yes. But that will not insure the derivative will be zero at that point.
Can you get what you want, using other tools? For example, using my SLM toolbox, you could force the curve to pass exactly through the given point, as well as force the curve to have a zero derivative at that point. This would insire a local maximum there. You woud need to decide how to treat those other outliers of course, as at each end of the curve there seem to be fairly noisy points. They will drag the curve around greatly.
You have not attached your data, only a picture of it, so I cannot show how to set those requirements, but they are not difficult. You can download SLM from the file exchange.
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!