1、实验一:迭代法#include#include #define x0 2#define maxrept 1000;#define G(x) pow(x+1/2.0,1.0/3) void main()int i;double x_k=x0,x_k1=x0;printf( k xkn);for (i=0;i10;i+)printf( %d %gn,i,x_k1);x_k1=G(x_k);x_k=x_k1;printf(迭代 10 后的结果是:gn,x_k);kwk口211,357212 1.229193 1.200284 1.193555 1.191976 1_19167 1,191518 L1
2、91499 1.19 149迭弋10后的结杲是: 1. 19149Press any key t-o continue实验一:牛顿法#include#include #define x0 0#define maxrept 1000;#define G(x) 2*x*x*x-1 void main()int i;double x_k=x0,x_k1=x0;printf( k xkn);for (i=0;i10;i+)printf( %d %gn,i,x_k1);x_k1=G(x_k);x_k=x_k1;printf(迭代 10 后的结果是:gn,x_k);k 012 34 56xk0-1-3-5
3、5-332751-7. 36865e+016-8. 00192e+050-1. 02474e+153-1. #工师L4工NF迭代1。后的结果是:-L ttINF Press any key to continue#include#include #define x0 0#define MAXREPT 1000#define EPS 0.5E-5#define G(x) x-(x*x*x-x-1)/(3*x*x-1)void main()int i;double x_k=x0,x_k1=x0;printf( k xkn);for (i=0;iMAXREPT;i+)printf( %d %gn,i
4、,x_k1);x_k1=G(x_k);if(fabs(x_k1-x_k)EPS) printf(The root is x=%g,k=%dn,x_k1,i); return;x_k=x_k1;?2345e7s9。三lc = NQ ni_L&一32. O3S-4G-1, 3028-Q.1 ie 12O_ 3 45O2S1, 42775一d. Q42 4 工。O_ 4Q4949一 I. 70691. 15STG5 &94192O. 7 4N4942. 7B 13 B273工.1. 35V2G1. 3Z5661. 324720口七 工= :k=ZL_ 32472,siriy K 曰 y to u o
5、ri_ 七 5.#include#include #define x0 1.5#define MAXREPT 1000#define EPS 0.5E-5#define G(x) x-(x*x*x-x-1)/(3*x*x-1)void main()int i;double x_k=x0,x_k1=x0;printf( k xkn);for (i=0;iMAXREPT;i+)printf( %d %gn,i,x_k1);x_k1=G(x_k);if(fabs(x_k1-x_k)EPS)11Q47QOO1QOCOQ1QOj17OO1.i ZThe root is x= 1. 32472, k=3P
6、ress any key to continueir-arprintf(The root is x=%g,k=%dn,x_k1,i); return;x k=x k1;实验二:Gauss消元法#include#include#define N 4 static doubleaaNN+1=1,-1,1,-4,2,5,-4,3,12,4,2,1,1,11,3,2,-17-1,0;int gauss(double aN+2,double x);void putout(double a口N+2);void main()int i,j,det;double aN+1N+2,xN+1;for(i=1;i=
7、N;i+)for(j=1;j=N+1;j+)aij=aai-1j-1;det=gauss(a,x);if (det!=0)for (i=1;i=N;i+)printf(x%d=%g,i,xi);int gauss(double aN+2,double x口)int i,j,k,r;double c;putout(a);for (k=1;k=N-1;k+)if(fabs(akk)1e-17)printf(n pivot element is 0. fail! n);return(0);for(i=k+1;i=N;i+)c=aik/akk;for(j=k;j=N+1;j+)aij=aij-c*ak
8、j;putout(a);if(fabs(aNN)=1;k-)xk=akN+1;for(j=k+1;j=N;j+)xk=xk-akj*xj; xk=xk/akk;return(1);void putout(double a口N+2)for(int i=1;i=N;i+)for(int j=1;j=N+1;j+)printf(%15g,a皿);printf(n); printf(n);il=2.71739x2=l. 11594 x 3 =-0,654589x4=-0,263285Press any key to continue实验三:拉格朗日插值法#include#include#define
9、f(x) (1.0/(1+25*x*x)#define N 4double xN+1,yN+1;void main()double a=0.03,b=1.3;double x=b-(b-a)/(2*N);/计算最后两个节点的中间节点double L(double xx);double lagBasis(int k , double xx);void putdata(double a , double b , int n);void output();putdata(a, b , N);output();printf(n精确值 f(%g)=%g,x,f(x);printf(n近似值 L(%g)=
10、%gn,x,L(x);double lagBasis(int k,double xx)double lb=1;int i;for(i=0;i=N;i+)if(i!=k)lb*=(xx-xi)/(xk-xi); return lb;double L(double xx)double s=0;int i;for(i=0;i=N;i+)s +=lagBasis(i,xx)*yi;return s;void putdata ( double a, double b , int n)double h;int i;h=(b-a)/n;/* for(i=0;i=n;i+)xi = a + i * h;yi
11、= f(xi);*/for(i=0;i=N;i+)switch(i)case 0:x0=0.03;y0=f(x0); break;case 1:x1=0.17;y1=f(x1);break;case 2:x2=0.65;y2=f(x2);break;case 3:x3=1.1;y3=f(x3); break;case 4:x4=1.3;y4=f(x4); break;void output()nxi:);int i;printf(n各节点信息:for(i=0;i=N;i+)printf(%7.4g ,xi);printf(nyi:);for(i=0;i=N;i+)printf(%7.4g ,y
12、i);&节点4言息一 xi :O. O3Vi:O.978精碘值f Cl. 近f以值LC1. . 17O. 65O_ 5806 O- 0364914125)=O. 029796214125)=。. 02924441. 11. 3 - 032 O- 02312Fr ess anv k ev to con.-tin.i_ie实验三:牛顿插值法#include #include#define f(x) 1/(1+(25*x*x)#define M 4double xM+1,yM+1;void main()double a=0.03,b=1.3;double x=b-(b-a)/(2*M);double
13、 N(double xx);void putdata(double a,double b,int n);void output();putdata(a,b,M);output();printf(n近似值 N(%g)=%g”,x,N(x);printf(n精确值 f(%g)=%gn,x,f(x);double N(double xx)double s=y0,d=1;int i,j;double dfM+1M+1;for(i=0;i=M;i+)dfi0=yi;for(j=1;j=M;j+)for(i=j;i=M;i+)dfij=(dfij-1-dfi-1j-1)/(xi-xi-j);printf(
14、n x f(x);for(j=1;j=M;j+)printf(%10d阶”,j);for(i=0;i=M;i+)printf(n%5.2g%7.4g,xi,yi);for(j=1;j=i;j+)printf( %7.4g ”,dfij);for(i=1;i=M;i+)d *=(xx-xi-1);s +=dfii*d;return s;void putdata(double a,double b,int n)double h;int i;h=(b-a)/n;/*for(i=0;i=n;i+)xi=a+i*h;yi=f(xi);*/for(i=0;i=n;i+)switch(i)case 0:x0
15、=0.03;y0=f(x0);break;case 1:x1=0.17;y1=f(x1);break;case 2:x2=0.65;y2=f(x2);break;case 3:x3=1.1;y3=f(x3);break;case 4:x4=1.3;y4=f(x4);break;void output()int i;printf(n 各节点信息:nxi:);for(i=0;i=M;i+)printf(%7.4g,xi);printf(nyi:);for(i=0;i=M;i+)printf(%7.4g ,yi);Jress any key to continue实验四:辛普森公式#include#
16、include#define f(x) (x+sin(x/3)void main()double a=0.3,b=1.5;double S,h,x;int n,i;printf( please input Even n :);scanf(%d,&n);h=(b-a)/n;x=a;S=0;for(i=1;in;i+)x+=h;if(i%2=0)S+=2*f(x);else S+=4*f(x);S=(f(a)+S+f(b)*h/3;printf(S(%d)=%gn,n,S);printf( The exact value is %gn,1.37486642915362);Please inpu实验
17、四:复合梯形公式#include#include#define f(x) (sin (x)/3)+x)/* 被积函数 */void main()double a=0.3,b=1.5;/* 积分区间*/double T,h,x;/*用T来存放复合梯形积分结果*/int n,i; printf(please input n:);scanf(%d,&n);/*输入等分的区间数*/ h=(b-a)/n;x=a;T=0;for(i=1;i=n;i+) x=x+h; T=T+f(x);T=(f(a)+2叮+f(b)*h/2;/*计算复合梯形积分结果 */printf(T(%d)=%gn,n,T);/*输出精确值*/printf(The exact value is %gn,(0.5*1.5*1.5- cos(1.5)/3)-(0.5*0.3*0.3-cos(0.3)/3);lease input n:600 (600)=1. 37853he exact value is 1. 37487 ress any key to continue