之前帮朋友利用实验数据画图,发现MatLab的确是画图的好工具,用它画的图比Excel光滑、精确。利用一组数据要计算出这组数据对应的函数表达式从而得到相应图像,MatLab的程序如下:
x=[1 5 10 20 30 40 60 80]
y=[15.4 33.9 42.2 50.5 56 62.7 72 81.1]
plot(x,y,'r*');
legend('实验数据(xi,yi)')
xlabel('x'),ylabel('y'),
title('数据点(xi,yi)的散点图')
syms a1 a2 a3
x=[1 5 10 20 30 40 60 80];
fi=a1.*x.^2+a2.*x+a3
y=[15.4 33.9 42.2 50.5 56 62.7 72 81.1]
fi =[a1+a2+a3,25*a1+5*a2+a3, 100*a1+10*a2+a3, 400*a1+20*a2+a3, 900*a1+30*a2+a3, 1600*a1+40*a2+a3, 3600*a1+60*a2+a3, 6400*a1+80*a2+a3];
fy=fi-y;fy2=fy.^2;J=sum(fy.^2)
syms a1 a2 a3
J =(a1+a2+a3-77/5)^2+(25*a1+5*a2+a3-339/10)^2+(100*a1+10*a2+a3-211/5)^2+(400*a1+20*a2+a3-101/2)^2+(900*a1+30*a2+a3-56)^2+(1600*a1+40*a2+a3-627/10)^2+(3600*a1+60*a2+a3-72)^2+(6400*a1+80*a2+a3-811/10)^2;
Ja1=diff(J,a1);Ja2=diff(J,a2);Ja3=diff(J,a3);
Ja11=simple(Ja1),Ja21=simple(Ja2),Ja31=simple(Ja3)
A=[114921252,1656252,26052;1656252,26052,492;26052,492,16];
B=[9542429/5,166129/5,4138/5];
C=B/A,f=poly2sym(C)
xi=[1 5 10 20 30 40 60 80];
y=[15.4 33.9 42.2 50.5 56 62.7 72 81.1];
n=length(xi);
f=-0.0086.*xi.^2+1.3876.*xi+23.1078;
x=1:1/10:80;
F=-0.0086.*x.^2+1.3876.*x+23.1078;
fy=abs(f-y);fy2=fy.^2;Ew=max(fy),
E1=sum(fy)/n,E2=sqrt((sum(fy2))/n)
plot(xi,y,'r*'),hold on,plot(x,F,'b-'),hold off
legend('数据点(xi,yi)','拟合曲线 f(x)= -0.0086x^2+1.3876x+23.1078'),
xlabel('x'),ylabel('y'),
title('实验数据点(xi,yi)及拟合曲线f(x)')
下图是这个程序运行后的图像: