matlab的最小二乘法编程,请直接发代码上来

来源:百度知道 编辑:UC知道 时间:2024/06/09 10:23:01
数据是:
x=0.0
0.2
0.4
0.6
0.8
1.0
1.2

y=0.9
1.9
2.8
3.3
4.0
5.7
6.5

function hh
xdata=[0.0 0.2 0.4 0.6 0.8 1.0 1.2]
ydata=[0.9 1.9 2.8 3.3 4.0 5.7 6.5]
b=polyfit(xdata,ydata,1)
[a,RESNORM,RESIDUAL,EXITFLAG] = lsqcurvefit(@myfun, [1 1], xdata, ydata)
plot(xdata,ydata,'o',xdata,polyval(b,xdata))
function F = myfun(a,xdata)
F = a(1)*xdata+a(2);

结果:
b =

4.5714 0.8429

Optimization terminated: first-order optimality less than OPTIONS.TolFun,
and no negative/zero curvature detected in trust region model.

a =

4.5714 0.8429

RESNORM =

0.4829

RESIDUAL =

-0.0571 -0.1429 -0.1286 0.2857 0.5000 -0.2857 -0.1714

EXITFLAG =