急求用maple编写最小二乘法的程序。

来源:百度知道 编辑:UC知道 时间:2024/05/30 10:15:16
可以是C语言或者是MATLAB写的程序也行,谢谢了

其实最小二乘法也可以写得不像我原来的那么麻烦:
fid=fopen('input.txt','r+');
U=fscanf(fid,'%f',a);
fclose(fid);
fid1=fopen('output1.txt','r+');
Y=fscanf(fid1,'%f',a);
fclose(fid1);
U1=U(1:a-1);
U2=U(1:a-2);
Y1=Y(1:a-1);
Y2=Y(1:a-2);
D1=-1*[0;Y1];
D2=-1*[0;0;Y2];
D3=[0;U1];
D4=[0;0;U2];
D=[D1 D2 D3 D4];
Q=inv(D'*D)*D'*Y
下面是递推最小二乘的算法:
m=input('m=')
fid=fopen('input.txt','r+');
U=fscanf(fid,'%f',a);
fclose(fid);
fid1=fopen('output1.txt','r+');
Y=fscanf(fid1,'%f',a);
fclose(fid1);
U1=U(1:m-1);
U2=U(1:m-2);
Y1=Y(1:m-1);
Y2=Y(1:m-2);
Y3=Y(1:m);
D1=-1*[0;Y1];
D2=-1*[0;0;Y2];
D3=[0;U1];
D4=[0;0;U2];
D=[D1 D2 D3 D4];
Q=inv(D'*D)*D'*Y3;
P=inv(D'*D);
%以上程序是用最小二乘法计算的初值,取前m个数据%
for i=m:a-1;
x=[-1*Y(i);