急急急!!!matlab非线性拟合x=xm/(1+1/100*exp(-r*t)*(xm-100))

来源:百度知道 编辑:UC知道 时间:2024/06/02 18:18:02
我有一批数据拟合x=xm/(1+1/100*exp(-r*t)*(xm-100)),可是运行时出错,请高手帮我看一下
我的代码:
clc;clear;
t=[0 1 2 3 4 5 6];
x=[105.50 113.10 131.65 153.50 174.84 186.03 190.12];
f=inline('beta(1)/(1+1/100*exp(-beta(2)*t)*(beta(1)-100))','beta','t');
xs=nlinfit(t,x,f,[0.5,0.5]);
xm=xs(1),r=xs(2)
%test the model
tt=min(t):max(t);
xx=xm/(1+1/100*exp(-r*t)*(xm-100));
plot(t,x,'o',tt,xx,'r')
运行时:
??? Error using ==> nlinfit
The inline model function generated the following error:
Error using ==> inlineeval
Error in inline expression ==> beta(1)/(1+1/100*exp(-beta(2)*t)*(beta(1)-100))
??? Error using ==> mrdivide
Matrix dimensions must agree.
请高手指点

首先,应注意 / 和 ./
f=inline('beta(1)./(1+1/100*exp(-beta(2)*t)*(beta(1)-100))','beta','t');
xx=xm./(1+1/100*exp(-r*tt)*(xm-100));

其次,你的初值取得好像不好,我用matlab6.5,初值用[0.5 0.5]时得到xm=158.2077,r=227.6978。拟合误差很大;
初值用[20,20]时得xm=235.0791,r=0.3110,拟合效果较好。

你可以自己试一下。你的代码我稍微改动了点:

clear;
t=[0 1 2 3 4 5 6];
x=[105.50 113.10 131.65 153.50 174.84 186.03 190.12];
f=inline('beta(1)./(1+0.01*exp(-beta(2)*t)*(beta(1)-100))','beta','t');
xs=nlinfit(t,x,f,[20 20]); %调整初值可能导致不同结果。
xm=xs(1)
r=xs(2)
%test the model
tt=min(t):0.1:max(t);
xx=xm./(1+0.01*exp(-r*tt)*(xm-100));
plot(t,x,'ro',tt,xx,'b')