lsqcurvefit help

7 ビュー (過去 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 Center および File ExchangeGet Started with Curve Fitting Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by