# adjusting the peak of a spline in curve fitting

조회 수: 6(최근 30일)
Salma fathi 2023년 2월 7일
댓글: Salma fathi 2023년 3월 27일
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);
##### 댓글 수: 1표시 없음숨기기 없음
Sarvesh Kale 2023년 2월 7일
Image is not attached

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

### 답변(1개)

John D'Errico 2023년 2월 7일
편집: John D'Errico 2023년 2월 7일
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.
##### 댓글 수: 4표시 이전 댓글 수: 3숨기기 이전 댓글 수: 3
Salma fathi 2023년 3월 27일
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.

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

### 범주

Find more on Get Started with Curve Fitting Toolbox 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!