1、程序 1 美国 1920 年到 1970 的人口调查表年份 1920 1930 1940 1950 1960 1970人口(千人) 105711 123203 131669 150697 179323 203212用数据构造一个 5 次拉格朗日插值多项式,并用此估计 1910,1965,和 2002年的人口。在 1920 年的实际人口约为:91772000,请判断差值得到的1965 和 2002 年的人口数据准确性是多少?解:用 matlab 编写的拉格朗日插值 M 文件如下:function m,y=cz5(x)y1=105711;y2=123203;y3=131669;y4=150697;
2、y5=179323;y6=203212;x1=1920; x2=1930; x3=1940; x4=1950; x5=1960; x6=1970;a1=(x-x2)*(x-x3)*(x-x4)*(x-x5)*(x-x6)/(x1-x2)*(x1-x3)*(x1-x4)*(x1-x5)*(x1-x6);a2=(x-x1)*(x-x3)*(x-x4)*(x-x5)*(x-x6)/(x2-x1)*(x2-x3)*(x2-x4)*(x2-x5)*(x2-x6);a3=(x-x1)*(x-x2)*(x-x4)*(x-x5)*(x-x6)/(x3-x1)*(x3-x2)*(x3-x4)*(x3-x5)*(
3、x3-x6);a4=(x-x1)*(x-x2)*(x-x3)*(x-x5)*(x-x6)/(x4-x1)*(x4-x2)*(x4-x3)*(x4-x5)*(x4-x6);a5=(x-x2)*(x-x3)*(x-x4)*(x-x1)*(x-x6)/(x5-x1)*(x5-x3)*(x5-x2)*(x5-x4)*(x5-x6);a6=(x-x2)*(x-x3)*(x-x4)*(x-x5)*(x-x1)/(x6-x1)*(x6-x2)*(x6-x3)*(x6-x4)*(x6-x5);实际值大约为: y=y1*a1+y2*a2+y3*a3+y4*a4+y5*a5+y6*a6;if x=1910m=91
4、772;估计值大约为:elsem=(91772-31872)/31872*y+y; 估计值大约为:End输入如下代码:m,y=cz5(1910)m,y=cz5(1965)m,y=cz5(2002)结果为:ans =实际值大约为:ans =估计值大约为:m =91772y =31872ans =实际值大约为:ans =估计值大约为:m =5.559574703016167e+005y =1.930815117187500e+005ans =实际值大约为:ans =估计值大约为:m =7.526371798548073e+004y =2.613874841600098e+004程序 2 用牛顿插值
5、估计(1) 1965 年的人口数(2) 2002 年的人口数解:用matlab编写的牛顿插值M文件如下:function y=niuden5(x)y1=105711 123203 131669 150697 179323 203212;x0=1920 1930 1940 1950 1960 1970;for i=2:6y2(i)=(y1(1)-y1(i)/(x0(1)-x0(i);endfor i=3:6y3(i)=(y2(2)-y2(i)/(x0(2)-x0(i);endfor i=4:6y4(i)=(y3(3)-y3(i)/(x0(3)-x0(i);endfor i=5:6y5(i)=(y
6、4(4)-y4(i)/(x0(4)-x0(i);endy6(6)=(y5(5)-y5(6)/(x0(5)-x0(6);y=y1(1)+y2(2)*(x-x0(1)+y3(3)*(x-x0(1)*(x-x0(2)+y4(4)*(x-x0(1)*(x-x0(2)*(x-x0(3)+y5(5)*(x-x0(1)*(x-x0(2)*(x-x0(3)*(x-x0(4)+y6(6)*(x-x0(1)*(x-x0(2)*(x-x0(3)*(x-x0(4)*(x-x0(5);输入如下代码:niuden5(1965),niuden5(2002)结果为:ans =1.930815117187500e+005ans
7、 =2.613874841600133e+004程序 3 用复化辛普生积分和复化梯形积分计算去积分节点Xi ;i=0,1,2,N,并对 N= ,i=1,2,12.比较比较两种方法的误差。解:matica程序为:复合Simpson法则:clearf,k,s,h;fx_ ;= sinx;s=Nintegraetsinx,x,1,5,14;Fork=0,k #include #include using namespace std;int main()int N,row,colum; /定义方程组的阶数row=0; /列主元所在行colum=0; /主元所在列int L1=0; /int L2=0;
8、double max; double L0=1; /系数 coutN;double *B=new doubleN+1;double *A=new double*N; /定义增广矩阵for (int i=0;iAij;max=A00;cout“增广矩阵 A 为:“endl;for ( i=0;iN;i+)for (int j=0;jN+1;j+)coutsetw(5)Aij;coutn;for (L1=0;L1N-1;L1+)for (i=L1;iN;i+) /选取列主元if(maxAiL1) /L2 换成 L1max=AiL1; /L2 换成 L1row=i; /列主元所在行,L1 表示初始列
9、元所在行for (int j=0;jN+1;j+) /换行Bj=Arowj;Arowj=AL1j;AL1j=Bj;cout“选取主元后的增广矩阵 A 为:“endl;for ( i=0;iN;i+)for (int j=0;jN+1;j+)coutsetw(15)Aij;coutn;/消主元for ( i=L1+1;iN;i+)L0=AiL1/AL1L1; /L0=AiL2/AL1L2;for (int j=0;jN+1;j+)Aij=Aij-AL1j*L0;cout“第“L1+1“一次消元后的矩阵为“endl;for ( i=0;iN;i+)for (int j=0;jN+1;j+)coutsetw(15)Aij;coutn;return 0;