How to put fitting constraints on smoothing splines
11 ビュー (過去 30 日間)
古いコメントを表示
Having a curve as the one shown in the image, and knowing for sure that the peak of this curve is the blue point, we would like to reconstruct it such that it has its peak at the blue point, so
I did some fitting for it using smoothing splines through the curve fiiting toolbox, and with significantly increasing the weight of the two end points and the peak point in blue I got the following
But this has two issues:
- The peak point of the spline is not exactly at the peak point that we want, it tries to approach it as much as possible but never be exactly at it.
- Increasing the weight of this point made the fiited curve leave the original track of the curve. trying to solve this, again, we visualy choose some knot point and increased the weight for them and got the following, (each plot shows the results for choosing different knot point).
But again, incresing the weight of some point would make the fitted curve lose its track for other points.
So what is a better way to choose the knot points other than just incresing the weight of some of them such that the fitted curve would pass through as much points as possible from the original curve without affecting its smoothness? and how can we set its maximum point to be exactly at the point that we want. Or would there be a better technique to use in our case rather than smoothing splines?
Attached is the data I am using for the plots.
2 件のコメント
Bruno Luong
2023 年 3 月 29 日
If you know the model has peak at the dark blue point, then clearly your data is incorrect.
Any fitting would not be accurate. This is NOT question of using spline or some other model.
採用された回答
Mathieu NOE
2023 年 3 月 29 日
hello
this is the poor's man suggestion, (no Curve Fitting toolbox, no spline smoothing)
try it and let me know
load data.mat
[myb,ib] = max(yb);
mxb = xb(ib);
xa = xb;
ya = yb;
xa(ib) = [];
ya(ib) = [];
% spline smoothing or simply with smoothdata ?
xs = xa;
ys = smoothdata(ya,'lowess',9);
% non linear y shift ?
% trying to create a y shift based on x distance from peak point (exp
% distribution)
dx = abs(xs-mxb);
dy = abs(myb-max(ys));
y_shift = dy*exp(-dx.^2/2000);
yss = ys + y_shift;
figure(1);
plot(ya,xa,'og');
hold on
plot(myb,mxb,'*b','Markersize',15);
plot(ys, xs);
plot(yss, xs,'g');
legend('raw data','peak point','smoothed raw data','streched smoothed data')
6 件のコメント
その他の回答 (1 件)
Bruno Luong
2023 年 3 月 29 日
移動済み: Bruno Luong
2023 年 3 月 29 日
load('data.mat')
[yp,imax] = max(yb);
xp = xb(imax);
xb(imax) = [];
yb(imax) = [];
% force fit pass through (xp,yp) with horizontal ta,gential (peak)
pntcon = struct('p',{0 1},'x',{xp xp},'v',{yp 0});
options = struct('pntcon', pntcon);
% https://www.mathworks.com/matlabcentral/fileexchange/25872-free-knot-spline-approximation
pp = BSFK(xb,yb,4,20,[],options);
close all
xi = linspace(min(xb),max(xb),600);
yi = ppval(pp,xi);
plot(xi,yi,'r',xb,yb,'ob',xp,yp,'r+');
grid on
Zoom yo make sure that peak is at the (xp,yp) coordinates
3 件のコメント
Alex Sha
2023 年 3 月 29 日
try the fitting function below:
Sum Squared Error (SSE): 1.1865504761041E23
Root of Mean Square Error (RMSE): 62890128427.9206
Correlation Coef. (R): 0.97834912925758
R-Square: 0.957167018719065
Parameter Best Estimate
--------- -------------
y0 372158039488.346
a 591488381059.073
xc 339.045269108107
w -41.8638567923805
p1 -462109113.796853
参考
カテゴリ
Help Center および 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!