用MATLAB解决最小二乘解的题
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/11 08:58:13
用MATLAB解决最小二乘解的题
有一组实测数据如下表,而且已知该数据可能满足的原型函数为
y(x)=a*x+b*x^2*e^(-cx)+d,试求满足下面数据的最小二乘解a,b,c,d的值
x= 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
与x相对应的y=2.3201 2.6470 2.9707 3.2885 3.6008 3.9090 4.2147 4.5191 4.8232 5.1275
我老师说要先进行建立M脚本.这个怎么回事啊?
有一组实测数据如下表,而且已知该数据可能满足的原型函数为
y(x)=a*x+b*x^2*e^(-cx)+d,试求满足下面数据的最小二乘解a,b,c,d的值
x= 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
与x相对应的y=2.3201 2.6470 2.9707 3.2885 3.6008 3.9090 4.2147 4.5191 4.8232 5.1275
我老师说要先进行建立M脚本.这个怎么回事啊?
clear
>> f=inline('a(1)*x+a(2)*x.^2.*exp(-a(3)*x)+a(4)','a','x');
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
y=[2.3201 2.6470 2.9707 3.2885 3.6008 3.9090 4.2147 4.5191 4.8232 5.1275];
[xx,res]=lsqcurvefit(f,ones(1,4),x,y);
xx',res
要建立也是可以的.就是把上面那个inline弄成.m
如下:
在Matlab下输入:edit zhidao_15.m,然后将下面两行百分号之间的内容,复制进去,保存
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y=zhidao_15(a,x)
y=a(1)*x+a(2)*x.^2.*exp(-a(3)*x)+a(4);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
在Matlab下面输入:
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
y=[2.3201 2.6470 2.9707 3.2885 3.6008 3.9090 4.2147 4.5191 4.8232 5.1275];
[xx,res]=lsqcurvefit('zhidao_15',ones(1,4),x,y);
xx',res
>> f=inline('a(1)*x+a(2)*x.^2.*exp(-a(3)*x)+a(4)','a','x');
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
y=[2.3201 2.6470 2.9707 3.2885 3.6008 3.9090 4.2147 4.5191 4.8232 5.1275];
[xx,res]=lsqcurvefit(f,ones(1,4),x,y);
xx',res
要建立也是可以的.就是把上面那个inline弄成.m
如下:
在Matlab下输入:edit zhidao_15.m,然后将下面两行百分号之间的内容,复制进去,保存
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function y=zhidao_15(a,x)
y=a(1)*x+a(2)*x.^2.*exp(-a(3)*x)+a(4);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
在Matlab下面输入:
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1];
y=[2.3201 2.6470 2.9707 3.2885 3.6008 3.9090 4.2147 4.5191 4.8232 5.1275];
[xx,res]=lsqcurvefit('zhidao_15',ones(1,4),x,y);
xx',res