最小二乘法拟合指数函数(Matlab编程),着急,请指点!

拟合函数形式为:y=a×exp(-bt);数据比较少:y=[50 40 30 20 10 ]; t=[55 63 73 100 121],用matlab编程程序该怎么写啊,谢谢!
需要求出未知系数a和b的值

clc;
clear all;
y=[50 40 30 20 10 ]; x=[55 63 73 100 121];
a=[145.2345667367 -0.02061401470466 ];%设置初值,很关键。
f=@(a,x)a(1)*exp(-a(2)*x);
nlinfit(x,y,f,a)
%可以利用toolbox里面的拟合函数来确定初值:
(先在窗口输入x,y值)
command window输入cftool,然后再data里面选x,y数值。create data set。
Fitting里面选择new fit, type of fit 里面选择exponential,单击apply。
在result里面就可以看到a,b的值。(还有不同的组合)
然后你点击File,Generate M-file.
打开你生成的M-File,里面有st_就是matlab设置的初值。
希望可以帮到你。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-04-25
%方法一
y=[50 40 30 20 10 ]; t=[55 63 73 100 121];
yp=log(y);
p = polyfit(t,yp,1);
b=-p(1)
a=exp(p(2))
yf=a*exp(-b*t);
yf-y
plot(t,y,'r+',t,yf,'b-')
legend('原始点','拟合线')

%方法二
%% Fit: 'exp1'.
[xData, yData] = prepareCurveData( t, y );
% Set up fittype and options.
ft = fittype( 'exp1' );
opts = fitoptions( ft );
opts.StartPoint = [145.2 -0.3];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'y vs. t', 'exp1', 'Location', 'NorthEast' );
% Label axes
xlabel( 't' );
ylabel( 'y' );
fitresult
gof

%方法三
y=[50 40 30 20 10 ]';
yp=log(y);
t=[55 63 73 100 121]';
tl=ones(size(t));
t1=[tl t];
p=t1\yp;
b=-p(2)
a=exp(p(1))
yf=a*exp(-b*t);
yf-y
plot(t,y,'r+',t,yf,'b-')
legend('原始点','拟合线')
%方法四
regress追问

你好,能把那个曲线的未知系数求出来吗?程序,再次感谢

追答

你拟合的目的就是求出a和b,每个程序都能求出来的。
b =

0.0229

a =

172.2620

本回答被提问者采纳
相似回答