lsqcurvefit help

조회 수: 23 (최근 30일)
zhengz
zhengz 2011년 5월 19일
답변: MUMATZ QURESHI 2019년 4월 18일
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.

채택된 답변

Andrew Newell
Andrew Newell 2011년 5월 19일
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
zhengz
zhengz 2011년 5월 19일
You are the man! I tried both ways. They all work.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

MUMATZ QURESHI
MUMATZ QURESHI 2019년 4월 18일
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 2013년 2월 17일
Solved mine to, thanks

Daniela Garza Niño de Rivera
Daniela Garza Niño de Rivera 2019년 4월 4일
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

카테고리

Help CenterFile Exchange에서 Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by