adjusting the peak of a spline in curve fitting

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)

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

4 Comments

Alright let me make thigns a bit clear, I am trying find a model that would give me the best fit for my dataset. Uisng the curve fitting app, I got the follwoing results when applying smoothing splines and they are good for an extent, but still not what I desire.
The issue in this is the following, we need to force the spline to always pass through the point in red and preferably to attain its maximum at that point, this would be considered as the best fit for our curve.
Now to achieve this, there is a similar problem stated in this documentation https://www.mathworks.com/help/curvefit/fitting-a-spline-to-titanium-test-data.html, that is somehow similar to what I want. So what I was trying to do above is to define some knot points that would show the splline its way through the curve, and got the following (assuming that I am only concerned with the x-values in (~200,~400) for the time being)
but agian this good but not good enough, so I was trying to look and see if there is any way that would help us adjust the fitting as we want.
For how I stated my problem initially, a third degree polynomial looks perfect but I think when extending it to the full curve it wont give us the best fit.
I have tried other curve fittig techniques that gave me similar results as the smoothing splines one of them is Gaussian of order 3, but again I would face the same issue. So I am not sure if there is a better method to attain this problem.
Thank you always for your help.
attached above is the full curve data if it would help.
You might have some progress with spap2 instead of spapi, if you include your "desired red point", and use the weight-input, to increase the impact of that point relative to the other points. But that is a step you should be able to justify to your audience.
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.
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.
Thank you in advance.

Sign in to comment.

Categories

Products

Release

R2022b

Asked:

on 7 Feb 2023

Commented:

on 27 Mar 2023

Community Treasure Hunt

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

Start Hunting!