lsqcurvefit help

23 visualizaciones (últimos 30 días)
zhengz
zhengz el 19 de Mayo de 2011
Respondida: MUMATZ QURESHI el 18 de Abr. de 2019
I am absolutely new to MATLAB. I have 15 data sets and want to do a curve fitting to extract some parameters. Seems lsqcurvefit can do the job. First I tried to run the lsqcurvefit example in MATLAB. I copy and paste the code from the help file to a .m file like this:
function F = myfun(x,xdata)
F = x(1)*exp(x(2)*xdata);
% Assume you determined xdata and ydata experimentally
xdata = [0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = [455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
x0 = [100; -1];
[x] = lsqcurvefit(@myfun,x0,xdata,ydata);
When I trie to run the code, it says something like this in the command windows:
??? Input argument "x" is undefined.
Error in ==> test1 at 3 F = x(1)*exp(x(2)*xdata);
What do I miss here? I know I need to get a book to study MATLAB. But I need to solve the problem as soon as possible. Thanks for help.

Respuesta aceptada

Andrew Newell
Andrew Newell el 19 de Mayo de 2011
Maybe you have all of the code in one file? The function myfun should be in a separate file. Or you could use an anonymous function:
myfun = @(x,xdata) x(1)*exp(x(2)*xdata);
% Assume you determined xdata and ydata experimentally
xdata = [0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = [455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
x0 = [100; -1];
[x] = lsqcurvefit(myfun,x0,xdata,ydata);
  1 comentario
zhengz
zhengz el 19 de Mayo de 2011
You are the man! I tried both ways. They all work.

Iniciar sesión para comentar.

Más respuestas (3)

MUMATZ QURESHI
MUMATZ QURESHI el 18 de Abr. de 2019
function myfun
xdata = [0.9 1.5 13.8 19.8 24.1 28.2 35.2 60.3 74.6 81.3];
ydata = [455.2 428.6 124.1 67.3 43.2 28.1 13.1 -0.4 -1.3 -1.5];
x0 = [100, -1]; % Starting guess
x = lsqcurvefit(@myfun1,x0,xdata,ydata)
times = linspace(xdata(1),xdata(end));
figure;plot(xdata,ydata,'ko',times,myfun1(x,times),'b-')
legend('Data','Fitted exponential')
title('Data and Fitted Curve')
end
function F = myfun1(x,xdata)
F= x(1).*exp(x(2).*xdata);
end

Milad
Milad el 17 de Feb. de 2013
Solved mine to, thanks

Daniela Garza Niño de Rivera
Daniela Garza Niño de Rivera el 4 de Abr. de 2019
my code is not working is says :
" Error in Problem5M2>FunToFitM2 (line 12)
A=p(1);B=p(2);C=p(3);D=p(4);
Error in lsqcurvefit (line 213)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in Problem5M2 (line 6)
[P]=lsqcurvefit(@FunToFitM2,P0,T,muP);
Caused by:
Failure in initial objective function evaluation. LSQCURVEFIT cannot continue.
>> ""
how can i fix it?
here is the code:
function[P]=Problem5M2()
V= [275,10.52;277.2,8.702;279.4,6.887;281.7,5.071;283.9,3.705;286.1,3.164;288.3,2.603;290.6,2.053;292.8,1.502;295,1.306];
T=V(:,1); muP=V(:,2);
P0=[1;1];
[P]=lsqcurvefit(@FunToFitM2,P0,T,muP);
A=P(1);B=P(2);C=P(3);D=P(4);
P2=[A;B;C;D];
end
function[muP]=FunToFitM2(p,T)
A=p(1);B=p(2);C=p(3);D=p(4);
muP=(10).^(A+B./(T+C)+(D.*(T)));
end

Categorías

Más información sobre Least Squares en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by