Nonlinear regression using lsqcurvefit

Here is my code, along with my error message.
T = V22050100Emat;
A = table2array(T);
f = A(389:402, 6);
y = A(389:402, 5);
% axis([0 1010 0 1.6E-7])
% hold on
plot(f,y,'ro')
title('Data points')
% hold off
F = @(x,f)(x(1)./2)*(sin(x(2).*pi/2)./(cosh(x(2).*log(2*pi*f*x(3).).)+cos(x(2).*pi/2)));
x0 = [1e-6 0.16 0.04];
size(f)
size(F)
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,f,y);
hold on
plot(f,F(x,f))
hold off
My error message
Error using lsqcurvefit
Function value and YDATA sizes are not equal.
Error in nonlinfit (line 20)
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,f,y);
I have also tried the 'fit' function with mixed results. Thank you.

 採用された回答

Torsten
Torsten 2022 年 7 月 4 日
編集済み: Torsten 2022 年 7 月 4 日

0 投票

F = @(x,f) x(1)/2 * sin(x(2)*pi/2)./(cosh(x(2)*log(2*pi*f*x(3)))+cos(x(2)*pi/2));
instead of
F = @(x,f)(x(1)./2)*(sin(x(2).*pi/2)./(cosh(x(2).*log(2*pi*f*x(3).).)+cos(x(2).*pi/2)));
What do you get if you type
size(f)
size(y)
?
Does f and/or y have Inf or NaN values ?

5 件のコメント

Jason Yee
Jason Yee 2022 年 7 月 4 日
f and y are finite arrays of number values. With your suggestions i get
f = A(389:402, 6);
y = A(389:402, 5);
% axis([0 1010 0 1.6E-7])
% hold on
plot(f,y,'ro')
title('Data points')
% hold off
size(f)
size(y)
F = @(x,f)(x(1)./2)*(sin(x(2).*pi/2)./(cosh(x(2).*log(2*pi*f*x(3).).)+cos(x(2).*pi/2)));
x0 = [1e-6 0.16 0.04];
size(F)
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,f,y);
hold on
plot(f,F(x,f))
hold off
and an output with error message
ans =
14 1
ans =
14 1
ans =
1 1
Error using lsqcurvefit
Function value and YDATA sizes are not equal.
Error in nonlinfit (line 21)
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,f,y);
>> nonlinfit
File: nonlinfit.m Line: 16 Column: 67
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check
for mismatched delimiters.
Thank you.
Torsten
Torsten 2022 年 7 月 5 日
Why do you still use the faulty F definition although I provided the correct one ?
f = [3;4];
F = @(x,f)(x(1)./2)*(sin(x(2).*pi/2)./(cosh(x(2).*log(2*pi*f*x(3).).)+cos(x(2).*pi/2)));
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
F([1 4 7],f)
Jason Yee
Jason Yee 2022 年 7 月 5 日
My bad, i missread you answer. I now have this
T = V22050100Emat;
A = table2array(T);
f = A(389:402, 6);
y = A(389:402, 5);
% axis([0 1010 0 1.6E-7])
% hold on
plot(f,y,'ro')
title('Data points')
% hold off
size(f)
size(y)
F = @(x,f) x(1)/2 * sin(x(2)*pi/2)./(cosh(x(2)*log(2*pi*f*x(3)))+cos(x(2)*pi/2));
x0 = [1e-6 0.63 9e-5];
size(F)
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,f,y);
hold on
plot(f,F(x,f))
hold off
My output looks like this
Initial point is a local minimum.
Optimization completed because the size of the gradient at the initial point
is less than the value of the optimality tolerance.
<stopping criteria details>
Optimization completed: The final point is the initial point.
The first-order optimality measure, 2.210162e-07, is less than
options.OptimalityTolerance = 1.000000e-06.
My thinking is to decrease my optimality tolerance.
Torsten
Torsten 2022 年 7 月 5 日
編集済み: Torsten 2022 年 7 月 5 日
Tighten the tolerances (FunctionTolerance and StepTolerance) in the options structure for lsqcurvefit.
And/or multiply F by a large number.
Jason Yee
Jason Yee 2022 年 7 月 6 日
Thank you. This has solved my problem.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeNonlinear Optimization についてさらに検索

製品

リリース

R2022a

質問済み:

2022 年 7 月 4 日

コメント済み:

2022 年 7 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by