lsqcurvefit Error using /

7 views (last 30 days)
Chris Andoh
Chris Andoh on 18 Aug 2019
Edited: Chris Andoh on 18 Aug 2019
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.

Accepted Answer

Stephen23
Stephen23 on 18 Aug 2019
Edited: Stephen23 on 18 Aug 2019
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 Comment
Chris Andoh
Chris Andoh on 18 Aug 2019
Edited: Chris Andoh on 18 Aug 2019
Thanks, it worked! I completely forgot about the "/" (mrdivide) in between the first term.

Sign in to comment.

More Answers (0)

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!