天天看点

用多项式拟合曲线,估计曲线值

【写在前面】

今天要用matlab处理一些数据

%distance=sort(distance);
%distance=distance';

%hight=sort(hight);
%hight=hight';%预处理数据hight为y轴 distance为x轴 均为一阶行矩阵

plot(distance,hight,'b*-','LineWidth',2,'MarkerSize',15);
%画出原始数据点 蓝色折线星点

coeffs=polyfit(distance,hight,5);%计算拟合 5表示用五次多项式拟合
fittedX=linspace(min(distance),max(distance),2000);
%x轴 2000个数据点
fittedY=polyval(coeffs,fittedX);
%y轴
hold on;
plot(fittedX,fittedY,'r-','LineWidth',3);
%用红色连续线描点

x_o=5:1:100;
y=polyval(coeffs,x_o);
%用拟合出来的多项式估计5~100内 每隔1所输出的值
csvwrite('distance_coe.txt',y);
%按照csv格式输出估计的值

x=linspace(0,150,2000);
figure;
plot(x,polyval(coeffs,x))
%画出0~150范围内的拟合的曲线 观察超出150的数据的情况
           

继续阅读