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 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