lsqcurvefit Error using /

7 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Systems 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