您好,欢迎访问三七文档
习题二问题:1.编制通用子程序对n+1个节点xi及yi=f(xi)(i=1,…n)(1)n次拉格朗日插值计算公式;(2)n次牛顿向前插值计算公式;(3)n次牛顿向后插值计算公式;(一)程序流程图(1)拉格朗日插值程序流程图n=1z=y(0)i=0s=1.0j=0j=i来自主程序YNj=0s=s*(t-x[j])/(x[i]-x[j])z=z+s*y[i]NYi=n?i=i+1YN返回主程序,返回值为z(2)牛顿向前插值程序流程图z[0]=ti=0in-1来自主程序i=i+1NYi=0z[i]=z[i-1]*(t-i)/(i+1)c[i][0]=y[i]i=ni=i+1YNj=1i=0c[i][j]=c[i+1][j-1]-c[i][j-1]123123i=n-ji=i+1Yj=nj=j+1s=y[0]i=0NNs=s+z[i]*c[0][i+1]i=n-1i=i+1YNY返回主程序,返回值为s。(3)牛顿向后插值程序流程图z[0]=t来自主程序i=011z[i]=z[i-1]*(t+i)/(i+1)i=n-1i=i+1Yi=0c[i][0]=y[sn--]i=ni=i+1YNj=1Ni=0c[i][j]=c[i][j-1]-c[i+1][j-1]i=n-ji=i+1Yi=nj=j+1Y1N1i=0s=s+z[i]*c[0][i+1]i=ni=i+1YNs=y[n]返回主程序,返回值为s。(二)源程序见主程序清单问题:2.计算(1)已知f(x)=lnx,,[a,b]=[1,2],取h=0.1,xi=1+ih,i=0,1,…,10。用通用程序(1),(3)计算ln1.54及ln1.98的近似值;(一)程序清单/*programofquestion2.1,page61*/#includestdio.h#includemath.hmain(){inti,flag=0;doublez1,z2,x[11],y[11],t,s1,s2,z[10],c[11][11],log(double),ntb(),L();for(i=0;i=10;i++){x[i]=1+0.1*i;y[i]=log(x[i]);}printf(datax:\n);for(i=0;i=10;i++){flag++;printf(%11.6f,x[i]);if(flag%4==0)printf(\n);}printf(\ndatay:\n);flag=0;for(i=0;i=10;i++){flag++;printf(%11.6f,y[i]);if(flag%4==0)printf(\n);}printf(\nThetruevalue:\n);printf(ln1.54=%fln1.98=%f\n,log(1.54),log(1.98));z1=L(x,y,10,1.54);z2=L(x,y,10,1.98);t=(1.54-x[10])/0.1;s1=ntb(y,10,t,z,c);s2=ntb(y,10,t,z,c);t=(1.98-x[10])/0.1;printf(Theapproximatevalue:\n);printf(L(1.54)=%fL(1.98)=%f\n,z1,z2);printf(NTB(1.54)=%fNTB(1.98)=%f\n,s1,s2);}doubleL(doublex[],doubley[],intn,doublet){inti,k;doublez=0.0,s;if(n==1)z=y[0];for(k=0;k=n;k++){s=1.0;for(i=0;i=n;i++)if(i!=k)s=s*(t-x[i])/(x[k]-x[i]);z=z+s*y[k];}returnz;}doublentb(doubley[],intn,doublet,doublez[],doublec[][11]){inti,j,sn=n;doubles;z[0]=t;for(i=1;i=n-1;i++)z[i]=z[i-1]*(t+i)/(i+1);for(i=0;i=n;i++)c[i][0]=y[sn--];for(j=1;j=n;j++)for(i=0;i=n-j;i++)c[i][j]=c[i][j-1]-c[i+1][j-1];s=y[n];for(i=0;i=n-1;i++)s=s+z[i]*c[0][i+1];returns;}(二)运行结果datax:1.0000001.1000001.2000001.3000001.4000001.5000001.6000001.7000001.8000001.9000002.000000datay:0.0000000.0953100.1823220.2623640.3364720.4054650.4700040.5306280.5877870.6418540.693147Thetruevalue:ln1.54=0.431782ln1.98=0.683097Theapproximatevalue:L(1.54)=0.431782L(1.98)=0.683097NTB(1.54)=0.431782NTB(1.98)=0.683097问题:(2)f(x)=1/(1+25x2),|x|≤1取等距节点n=5和n=10,用通用程序(1),(2)依次计算x=-0.95+ih(i=0,1,…,19,h=0.1)处f(x)的近似值,并将其结果与其真实值相比较。(一)程序清单/*programofquestion2.2,page61*/#includestdio.h#includemath.hmain(){inti,flag;doublez1[20],z2[20],t[20],ty[20],x1[5],y1[5],x2[10],y2[10],z11[20],z22[20],n1[5],n2[10],c1[5][5],c2[10][10],m;doubleL(),ntf1(),ntf2();for(i=0;i=4;i++){x1[i]=-1+0.5*i;y1[i]=1.0/(1+25*x1[i]*x1[i]);}printf(Whenn=5:\ndatax:\n);for(i=0;i=4;i++)printf(%10.6f,x1[i]);printf(\ndatay;\n);for(i=0;i=4;i++)printf(%10.6f,y1[i]);for(i=0;i=19;i++){t[i]=-0.95+i*0.1;z1[i]=L(x1,y1,4,t[i]);m=(t[i]-x1[0])/0.1;z11[i]=ntf1(y1,4,m,n1,c1);}for(i=0;i=19;i++)ty[i]=1.0/(1+25*t[i]*t[i]);printf(\nThetruevalue:\n);flag=0;for(i=0;i=19;i++){flag++;printf(%10.6f,ty[i]);if(flag%5==0)printf(\n);}printf(Theapproximatevalue:\n);printf(1.Lagrange:\n);flag=0;for(i=0;i=19;i++){flag++;printf(%10.6f,z1[i]);if(flag%5==0)printf(\n);}printf(2.NewtonF:\n);flag=0;for(i=0;i=19;i++){flag++;printf(%12.4f,z11[i]);if(flag%4==0)printf(\n);}for(i=0;i=9;i++){x2[i]=-1+(2.0/9)*i;y2[i]=1.0/(1+25*x2[i]*x2[i]);}printf(\nWhenn=10:\ndatax:\n);for(i=0;i=9;i++){printf(%10.6f,x2[i]);if(i==4)printf(\n);}printf(\ndatay:\n);for(i=0;i=9;i++){printf(%10.6f,y2[i]);if(i==4)printf(\n);}for(i=0;i=19;i++){t[i]=-0.95+i*0.1;z2[i]=L(x2,y2,9,t[i]);m=(t[i]-x2[0])/0.1;z22[i]=ntf2(y2,9,m,n2,c2);}printf(\nTheapproximatevalue:\n);printf(1.Lagrange:\n);flag=0;for(i=0;i=19;i++){flag++;printf(%10.6f,z2[i]);if(flag%5==0)printf(\n);}printf(2.NewtonF:\n);flag=0;for(i=0;i=19;i++){flag++;printf(%12.4f,z22[i]);if(flag%4==0)printf(\n);}}doubleL(doublex[],doubley[],intn,doublet){inti,k;doublez=0.0,s;if(n==1)z=y[0];for(k=0;k=n;k++){s=1.0;for(i=0;i=n;i++)if(i!=k)s=s*(t-x[i])/(x[k]-x[i]);z=z+s*y[k];}returnz;}doublentf1(doubley[],intn,doublet,doublez[],doublec[][5]){inti,j;doubles;z[0]=t;for(i=1;in-1;i++)z[i]=z[i-1]*(t-i)/(i+1);for(i=0;i=n;i++)c[i][0]=y[i];for(j=1;j=n;j++)for(i=0;i=n-j;i++)c[i][j]=c[i+1][j-1]-c[j-1][j-1];s=y[0];for(i=0;i=n-1;i++)s=s+z[i]*c[0][i+1];returns;}doublentf2(doubley[],intn,doublet,doublez[],doublec[][10]){inti,j;doubles;z[0]=t;for(i=1;i=n-1;i++)z[i]=z[i-1]*(t-i)/(i+1);for(i=0;i=n;i++)c[i][0]=y[i];for(j=1;j=n;j++)for(i=0;i=n-j;i++)c[i][j]=c[i+1][j-1]-c[i][j-1];s=y[0];for(i=0;i=n-1;i++)s=s+z[i]*c[0][i+1];returns;}(二)运行结果Whenn=5:datax:-1.000000-0.5000000.0000000.5000001.000000datay;0.0384620.1379311.0000000.1379310.038462Thetruevalue:0.0424400.0524590.0663900.0864860.1167880.1649480.2461540.3902440.6400000.9411760.9411760.6400000.3902440.2461540.1649480.1167880.0864860.0663900.0524590.042440Theapproximatevalue:1.Lagrange:-0.159545-0.359479-0.356826-0.2152480.0095530.2698320.5258000.7456270.9054420.9893280.9893280.9054420.7456270.5258000.2698320.009553-0.215248-0.356826-0.359479-0.1595452.NewtonF:0.09440.18140.31820.60421.13882
本文标题:数值计算方法上机题
链接地址:https://www.777doc.com/doc-6095107 .html