《计算方法及程序实现(共4页).docx》由会员分享,可在线阅读,更多相关《计算方法及程序实现(共4页).docx(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上一、对分法1、#includemath.hmain()floata3=-3.0,1.0,0.0,b3=-2,0,1,c3;floatf(floatx);inti;for(i=0;i3;i+)doci=(ai+bi)/2.0;if(f(ci)=0)exit(0);elseif(f(ci)*f(ai)1e-5);ci=ai;printf(therootsare:);for(i=0;i3;i+)printf(%f,ci);printf(2.);floatf(floatx)floaty;y=x*x*x-2*x*x-4*x-7;return(y);3、对分部分函数调用(题目要求
2、如2)#includemath.hfloatf(floatx)floaty;y=x*x*x-2*x*x-4*x-7;return(y);floatf1(floata,floatb)floatc;doc=(a+b)/2;if(f(c)=0)exit(0);elseif(f(a)*f(c)1e-5);return(a);main()floata=3.0,b=4.0,s;s=f1(a,b);printf(therootis%f,s);2、用对分法求出方程x3-2x2-4x-7=0在区间【3,4】内的根,精度要求为105-。#includemath.hmain()floata=3.0,b=4.0,c;
3、floatf(floatx);doc=(a+b)/2.0;if(f(c)=0)printf(therootis%f,c);exit(0);/*找到方程的根*/elseif(f(c)*f(a)1e-5);printf(therootis%f,a);floatf(floatx)floaty;y=x*x*x-2*x*x-4*x-7;return(y);对分法的算法:扫描法的算法:4、对分法和扫描结合求方程x4-5x2+x+2=0的实根的上、下界,实现根的隔离,并用对分法求出所有的实根,精度要求为105-。此方程的4个实根分别为:root=2.root=0.root=0.root=2.#include
4、math.hfloatf(floatx)floaty;y=x*x*x*x-5*x*x+x+2;returny;main()floata,b,c;inti=0;floatx,h=0.001,p5,q5,n;scanf(%f%f,&a,&b)x=a;while(xb)if(f(x)*f(x+h)=0)pi=x;qi=x+h;i=i+1;printf(%f,%fn,pi,qi);x=x+h;n=i;for(i=0;i1e-5)c=(a+b)/2;if(f(c)=0)printf(%fn,c);exit(0);elseif(f(a)*f(c)0)b=c;elsea=c;printf(%fn,c)二、秦
5、九韶算法使用秦九韶算法计算多项式的值a0xn+a1x1-n+a1-nx+an例如计算3x2+2x+1,当x=-1时值为2。main()floataa20,y,x;inti,n;printf(inputduoxiangshicishun:n)scanf(%d,&n);printf(inputduoxiangshidexishun);for(i=0;i=n;i+)scanf(%f,&aai);printf(inputxn);scanf(%f,&x);y=aa0;for(i=1;i1e-5);printf(therootis%fn,x1);floatf(floatx)returnx*x*x-x*x-
6、2*x-3;floatfd(floatx)return3*x*x-2*x-2;2、用牛顿法求a的立方根,精度要求为0.。#includemath.hmain()doublex0,x1;inta;printf(inputan);scanf(%d,&a);if(a=0)printf(a=0n);exit(0);x1=a;dox0=x1;x1=x0-(x*x*x-a)/(3*x*x);while(fabs(x1-x0)=0.5e-5);printf(therootis%fn,x1);printf(root=%fn,x1);3、用牛顿法求方程x-ex-=0(x-exp(-x)=0)在1附近的根,精度要
7、求为0.00001。#includemath.hmain()floatx0,x1;x1=1.0;dox0=x1;x1=x0-(x-exp(-x)/(1+exp(-x);while(fabs(x1-x0)1e-5);printf(therootis%fn,x1);牛顿法的算法:4四、列主元高斯消元法(通用程序)#includemath.h#defineM3#defineN3main()floataMN+1,xN,temp,l,s;inti,j,k,r;printf(pleaseinputthedate:n);for(i=0;iM;i+)for(j=0;jN+1;j+)scanf(%f,&aij)
8、;for(k=0;kN-1;k+)r=k;for(i=k+1;ifabs(ark)r=i;if(ark=0)printf(QYn);exit(0);elseif(r!=k)for(j=k;j=N;j+)temp=akj;akj=arj;arj=temp;for(i=k+1;i=N-1;i+)l=aik/akk;for(j=k+1;j=0;k-)s=0.0;for(j=k+1;j=N-1;j+)s=s+akj*xj;xk=(akN-s)/akk;for(i=0;i=N-1;i+)printf(x%d=%fn,i,xi);列主元高斯消元法的算法:5五、LU分解法用LU分解法解线性方程组,系数矩阵由
9、二维数组a给出,右端由b数组给出。#includemath.hmain()inti,j,k,m,n=4;floatt;floatb4=1,1,-1,-1;floata44=4,3,2,1,3,4,3,2,2,3,4,3,1,2,3,4;for(k=0;kn;k+)for(j=k;jn;j+)t=0;for(m=0;m=k-1;m+)t=t+akm*amj;akj=akj-t;for(i=k+1;in;i+)t=0;for(m=0;m=k-1;m+)t=t+aim*amk;aik=(aik-t)/akk;for(i=0;in;i+)t=0;for(j=0;j=0;i-)t=0;for(k=i+1
10、;kn;k+)t=t+aik*bk;bi=(bi-t)/aii;for(i=0;in;i+)printf(%10.4f,bi);LU分解法的算法:6六、雅可比迭代法1、通用程序#defineN4#includemath.hfloatcha(x,y)floatxN,yN;floatz;inti,k;z=fabs(y0-x0);for(i=1;iz)z=fabs(yi-xi);return(z);main()floataNN,bN,xN,yN,h;inti,j,k;floatt=0,s;for(i=0;iN;i+)for(j=0;jN;j+)scanf(%f,&h);aij=h;for(i=0;i
11、N;i+)scanf(%f,&h);bi=h;yi=0;for(i=0;iN;i+)if(aii=0)printf(QIYI);exit(0);printf(Jacobin);for(k=1;k=30;k+)for(i=0;iN;i+)xi=yi;for(i=0;iN;i+)t=0;for(j=0;jN;j+)if(j!=i)t=t+aij*xj;yi=(bi-t)/aii;if(cha(x,y)1e-6)printf(xunhuancishu:k=%dn,k);for(i=0;i30)printf(fasannn);2、实验书第4题#includemath.hfloatcha(x,y)flo
12、atx3,y3;floatz;inti,k,n=3;z=fabs(y0-x0);for(i=1;iz)z=fabs(yi-xi);return(z);main()floata33=10,-2,-1,-2,10,-1,-1,-2,5;floatb3=3,15,10,y3=0,0,0,x3;floatt=0,s;inti,j,k,n=3;printf(Gauss-Seideln);for(k=1;k=30;k+)for(i=0;in;i+)xi=yi;for(i=0;in;i+)t=0;for(j=0;jn;j+)if(j!=i)t=t+aij*xj;yi=(bi-t)/aii;if(cha(x,
13、y)1e-6)printf(xunhuancishu:k=%dn,k);for(i=0;i30)printf(fasannn);6六、雅可比迭代法1、通用程序#defineN4#includemath.hfloatcha(x,y)floatxN,yN;floatz;inti,k;z=fabs(y0-x0);for(i=1;iz)z=fabs(yi-xi);return(z);main()floataNN,bN,xN,yN,h;inti,j,k;floatt=0,s;for(i=0;iN;i+)for(j=0;jN;j+)scanf(%f,&h);aij=h;for(i=0;i
14、N;i+)scanf(%f,&h);bi=h;yi=0;for(i=0;iN;i+)if(aii=0)printf(QIYI);exit(0);printf(Jacobin);for(k=1;k=30;k+)for(i=0;iN;i+)xi=yi;for(i=0;iN;i+)t=0;for(j=0;jN;j+)if(j!=i)t=t+aij*xj;yi=(bi-t)/aii;if(cha(x,y)1e-6)printf(xunhuancishu:k=%dn,k);for(i=0;i30)printf(fasannn);2、实验书第4题#includemath.hfloatcha(x,y)flo
15、atx3,y3;floatz;inti,k,n=3;z=fabs(y0-x0);for(i=1;iz)z=fabs(yi-xi);return(z);main()floata33=10,-2,-1,-2,10,-1,-1,-2,5;floatb3=3,15,10,y3=0,0,0,x3;floatt=0,s;inti,j,k,n=3;printf(Gauss-Seideln);for(k=1;k=30;k+)for(i=0;in;i+)xi=yi;for(i=0;in;i+)t=0;for(j=0;jn;j+)if(j!=i)t=t+aij*xj;yi=(bi-t)/aii;if(cha(x,
16、y)1e-6)printf(xunhuancishu:k=%dn,k);for(i=0;i30)printf(fasannn);8雅可比迭代法的算法:高斯-塞德尔迭代法的算法:八、牛顿基本插值公式(实验书第三题)main()inti,k,n;floatx20,y20,t,h,p;scanf(%d,&n);for(i=0;i=n;i+)scanf(%f%f,&xi,&yi);scanf(%f,&t);for(k=1;k=k;i-)yi=(yi-yi-1)/(xi-xi-k);printf(%10.5f,yi);printf(n);p=y0;h=1;for(i=1;i=n;i+)
17、h=h*(t-xi-1);p+=h*yi;printf(p=%fn,p);牛顿基本插值公式算法:9九、Lagarange全程插值算法(实验书第一题)main()inti,k,n;floatx20,y20,t,s,p;p=0;scanf(%d,&n);for(i=0;i=n;i+)scanf(%f%f,&xi,&yi);scanf(%f,&t);for(k=0;k=n;k+)s=1;for(i=0;i1e-5);10十一、多项式型经验公式算法设实验数据如下:Xi0.10.20.30.40.50.60.7Yi5.12345.30535.56845.93786.42707.07987.94493求其
18、二次拟合多项式#defineN7#definem2#includemath.hmain()inti,j,k;floatxN+1=0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,d,l;floatyN+1=0,5.1234,5.3053,5.5684,5.9378,6.427,7.079,7.9493;floattm+1,temp,s;floatam+1m+2=0;for(i=0;i=m;i+)for(k=1;k=N;k+)aim+1+=pow(xk,i)*yk;for(j=0;j=m;j+)for(k=1;k=N;k+)aij+=pow(xk,(i+j);for(k=0;km;k+
19、)d=akk;l=k;for(i=k+1;ifabs(d)d=aik;l=i;if(d=0)printf(QYn);exit(0);if(l!=k)for(j=k;j=m+1;j+)temp=alj;alj=akj;akj=temp;for(i=k+1;i=m;i+)l=aik/akk;for(j=k+1;j=0;k-)s=0;for(j=k+1;j=m;j+)s=s+akj*tj;tk=(akm+1-s)/akk;for(i=0;i0;i-)printf(%f*x%d,ti,i);if(ti-1=0)printf(+);printf(%f,t0);printf(n);d=0;for(i=1;i=N;i+)s=0;for(j=0;j=m;j+)s+=tj*pow(xi,j);d+=(s-yi)*(s-yi);printf(d=%f,d);专心-专注-专业