【发布时间】:2015-02-14 23:37:29
【问题描述】:
这里有人可以帮助我解决以下问题吗? 以下代码计算适合给定数据集的最佳多项式,即;指定次数的多项式。
不幸的是,无论数据集是什么,通常是 6 级或更高,MATLAB 完全不适合。通常拟合曲线完全远离数据以一种向下看的指数方式。 (参见示例:度数 = 8)。
x=[1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5] % experimental x-values
y=[4.3 6.2 10.1 13.5 19.8 22.6 24.7 29.2] % experimental y-values
degree=8; % specify the degree
A = zeros(length(x),degree);
for exponent=0:degree;
for data=1:length(x);
A(data,exponent+1)=x(data).^exponent; % create matrix A
end;
end;
a=inv((transpose(A)*A))*transpose(A)*y'; % a are the coëfficients of the polynom
a=flipud(a);
fitpolynoom=polyval(a,x);
error=sum((y-fitpolynoom).^2); % calculates the fit-error using the least-squares method
fitpolynoom=polyval(a,x);
figure;
plot(x,y,'black*',x,fitpolynoom,'g-');
error % displays the value of the fit-error in the Matlab command window
提前致谢。
【问题讨论】:
标签: matlab least-squares curve-fitting