求高手指点,用MATLAB计算—最小二乘法及均方误差

2024-12-02 10:04:07
推荐回答(1个)
回答(1):

用fit函数或者nlinfit,nlinfit是最小平方估计

比如第一个
>> y= [19.0 32.3 49.0 73.3 97.8]

y =

19.0000 32.3000 49.0000 73.3000 97.8000

>> x=[19 25 31 38 44]

x =

19 25 31 38 44
>> fit(x',y','a+b*x^2')
Warning: Start point not provided, choosing random start point.
> In fit>handlewarn at 715
In fit at 315

ans =

General model:
ans(x) = a+b*x^2
Coefficients (with 95% confidence bounds):
a = 0.9726 (0.7579, 1.187)
b = 0.05004 (0.04986, 0.05021)

用nlinfit
建立m文件mymodel.m
function y=mymodel(beta,x)
y=beta(1)+beta(2)*x.^2;

console下输入:
nlinfit(x,y,@mymodel,[0 0])

ans =

0.9726 0.0500

第二个我不知道函数模型,没法拟合

x=[0 5 10 15 20 25 30 35 40 45 50 55];
y=[0 1.27 2.16 2.86 3.44 3.87 4.15 4.37 4.51 4.58 4.62 4.64];
建立m文件mymodel.m
function y=mymodel(beta,x)
y=beta(1)*exp(-beta(2)./x);

console下输入:
nlinfit(x,y,@mymodel,[1 1])
ans =

5.5031 8.7873