matlab用最小二乘法求一形如y=t/(at+b)(a和b为待定系数)的多项式,使之与下列数据相拟合

数据如下t=[1 2 3 4 5 6 7 8] y=[4.00 6.40 8.00 8.80 9.22 9.50 9.70 9.68]

1.使用非线性最小二乘拟合函数lsqcurvefit拟合


t=[1 2 3 4 5 6 7 8];

y=[4.00 6.40 8.00 8.80 9.22 9.50 9.70 9.68];

fun=@(b,x)x./(b(1)*x+b(2));

x0=[0.1 0.1];

b=lsqcurvefit(fun,x0,t,y)


结果为:

b =


    0.0811    0.1468

即a=0.0811  b=0.1468


2.绘图

plot(t,y,'ko');

hold on

plot(t,fun(b,t),'r-');

legend('Original data','fitted curve ')

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-11-28
First, write a file to return the value of F (F has n components).function F = myfun(x,xdata)
F = x(1)*exp(x(2)*xdata);Next, invoke an optimization routine:% 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] % Starting guess
[x,resnorm] = lsqcurvefit(@myfun,x0,xdata,ydata);At the time that lsqcurvefit is called, xdata and ydata are
assumed to exist and are vectors of the same size. They must be the
same size because the value F returned by fun must
be the same size as ydata.