# adjusting the peak of a spline in curve fitting

10 views (last 30 days)
Salma fathi on 7 Feb 2023
Commented: Salma fathi on 27 Mar 2023
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);
Sarvesh Kale on 7 Feb 2023
Image is not attached

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.
format long g
[xx,yy]
ans = 3×2
1.0e+00 * 255 584000000000 329.315887451172 823639000000 375 607000000000
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
abcd = 1×4
1.0e+00 * -503430.643193734 416559781.304904 -110569928527.231 10040104276379
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.
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.
Salma fathi on 27 Mar 2023
May you please show me how these requirements could be set using the tool you are suggesting, you can find attached some data points that we would like to fit. we would like to have the maximum point of the fitted curve at the point of index=6, i.e. x=3.293158874511719e+02.

### Categories

Find more on Spline Postprocessing in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!