lsqcurvefit Error using /

8 visualizaciones (últimos 30 días)
Chris Andoh
Chris Andoh el 18 de Ag. de 2019
Editada: Chris Andoh el 18 de Ag. de 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.

Respuesta aceptada

Stephen23
Stephen23 el 18 de Ag. de 2019
Editada: Stephen23 el 18 de Ag. de 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 comentario
Chris Andoh
Chris Andoh el 18 de Ag. de 2019
Editada: Chris Andoh el 18 de Ag. de 2019
Thanks, it worked! I completely forgot about the "/" (mrdivide) in between the first term.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Systems of Nonlinear Equations en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by