Problem with Nonlinear Least Squares fitting

8 ビュー (過去 30 日間)
Jason Yee
Jason Yee 2022 年 6 月 13 日
コメント済み: Jason Yee 2022 年 7 月 4 日
I am trying to create a script that will fit some scientific data to the function
where a,b, and c are the fitting parameters. My problem is that the script does not seem to seek better parameters, but I can do the fitting in a different software. What is wrong with my code? My code is below, and I am very new to MatLab. Thank you.
x = A(389:401, 6);
y = A(389:401, 5);
axis([0 1010 0 1.6E-7])
% hold on
plot(x,y,'ro')
title('Data points')
% hold off
ft = fittype('(a/2)*(sin(b*pi/2)/(cosh(b*log(2*pi*x*c))+cos(b*pi/2)));');
fo = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[-1,-1,-1],...
'Upper',[1,1,1],...
'StartPoint',[5e-7 0.63 9e-5]);
[f2,gof,output]=fit(x,y,ft,fo);
f2
gof.rsquare
figure
plot(f2,x,y)

回答 (1 件)

Sam Chak
Sam Chak 2022 年 6 月 14 日
編集済み: Torsten 2022 年 6 月 14 日
I don't see anything technically wrong with code. Probably the choices of the lower and upper bounds are 'too vast'. Your data lies in this region?
axis([0 1010 0 1.6E-7])
Does your data look like these two curves?
x = linspace(-pi, pi, 3601);
y1 = (1/2)*(sin(1*pi/2)./(cosh(1*log(2*pi*x*1))+cos(1*pi/2)));
y2 = (-1/2)*(sin(-1*pi/2)./(cosh(-1*log(2*pi*x*-1))+cos(-1*pi/2)));
plot(x, [y1' y2'])
  1 件のコメント
Jason Yee
Jason Yee 2022 年 7 月 4 日
Hello
I tried narrowing the constraints on my starting values.
ft = fittype('(a/2)*(sin(b*pi/2)/(cosh(b*log(2*pi*x*c))+cos(b*pi/2)));');
fo = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[0,0,0],...
'Upper',[1e-6,1,1e-4],...
'StartPoint',[5.4e-7 0.67 9e-5]);
[f2,gof,output]=fit(x,y,ft,fo);
f2
gof.rsquare
figure
plot(f2,x,y)
Do you recommend the lsqcurvefit function? Thank you.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by