lsqcurvefit Error using /

7 ビュー (過去 30 日間)
Chris Andoh
Chris Andoh 2019 年 8 月 18 日
編集済み: Chris Andoh 2019 年 8 月 18 日
Hi, I'm trying to use MATLAB to fit some data using nonlinear least square fitting. When I run my code:
xdata = NbO2_219_T'; %NbO2_219_T is a column vector (want a row vector)
ydata = NbO2_219_ln_rho'; %NbO2_219_ln_rho is a column vector
fun = @(x,xdata) (x(1)*(1 + (sqrt(1 + xdata./x(2)) - 1)/(xdata./x(2))))/sqrt((sqrt(1 + xdata./x(2)) - 1)) + x(3);
%x = [x(1) x(2) x(3)] Parameters to solve for
x0 = [5 7e-3 -30]; %Initial geusses for parameters
x = lsqcurvefit(fun,x0,xdata,ydata)
I get the following error:
Error using /
Matrix dimensions must agree.
Error in
nonlinearfit_rho_vs_T>@(x,xdata)(x(1)*(1+(sqrt(1+xdata/x(2))-1)/(xdata/x(2))))/sqrt((sqrt(1+xdata/x(2))-1))+x(3)
Error in lsqcurvefit (line 213)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in nonlinearfit_rho_vs_T (line 14)
x = lsqcurvefit(fun,x0,xdata,ydata)
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
Does anyone know what could be the potential cause of this? I tried replacing the '/' with './' but unfortunately it did not solve the issue.

採用された回答

Stephen23
Stephen23 2019 年 8 月 18 日
編集済み: Stephen23 2019 年 8 月 18 日
Replacing both of the mrdivide operators with rdivide operators gets rid of the error:
>> fun = @(x,xd) (x(1)*(1+(sqrt(1+xd./x(2))-1)./(xd./x(2))))./sqrt((sqrt(1+xd./x(2))-1))+x(3);
>> xdata = randi(9,1,7);
>> x0 = [5,7e-3,-30];
>> fun(x0,xdata(:))
ans =
-28.376
-28.678
-28.914
-28.376
-28.376
-28.979
-29.029
Only you can decide if this is correct.
  1 件のコメント
Chris Andoh
Chris Andoh 2019 年 8 月 18 日
編集済み: Chris Andoh 2019 年 8 月 18 日
Thanks, it worked! I completely forgot about the "/" (mrdivide) in between the first term.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSystems of Nonlinear Equations についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by