计算方法及程序实现.pdf
一、对分法 1、#include main()float a3=,b3=-2,0,1,c3;float f(float x);int i;for(i=0;i3;i+)!do ci=(ai+bi)/;if(f(ci)=0)exit(0);else if(f(ci)*f(ai)1e-5);ci=ai;printf(the roots are:);for(i=0;i3;i+)printf(%f,ci);printf();float f(float x)float y;y=x*x*x-2*x*x-4*x-7;return(y);、3、对分部分函数调用(题目要求如 2)#include float f(float x)float y;y=x*x*x-2*x*x-4*x-7;return(y);float f1(float a,float b)float c;do c=(a+b)/2;if(f(c)=0)exit(0);else if(f(a)*f(c)1e-5);return(a);main()float a=,b=,s;s=f1(a,b);printf(the root is%f,s);2、用对分法求出方程 x3-2x2-4x-7=0 在区间【3,4】内的根,精度要求为105。#include main()float a=,b=,c;float f(float x);do c=(a+b)/;if(f(c)=0)printf(the root is%f,c);exit(0);/*找到方程的根*/else if(f(c)*f(a)1e-5);printf(the root is%f,a);|float f(float x)float y;y=x*x*x-2*x*x-4*x-7;return(y);对分法的算法:扫描法的算法:4、对分法和扫描结合 求方程 x4-5x2+x+2=0 的实根的上、下界,实现根的隔离,并用对分法求出所有的实根,精度要求为 105。此方程的 4 个实根分别为:root=root=root=root=#include -float f(float x)float y;y=x*x*x*x-5*x*x+x+2;return y;main()float a,b,c;int i=0;float x,h=,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);else if(f(a)*f(c)0)b=c;else a=c;printf(%fn,c)二、秦九韶算法 使用秦九韶算法计算多项式的值a0 xn+a1x1n+a1nx+an 例如计算 3x2+2x+1,当 x=-1 时值为 2。main()float aa20,y,x;int i,n;printf(input duo xiang shi ci shu n:n )scanf(%d,&n);printf(input duo xiang shi de xi shun);for(i=0;i=n;i+)scanf(%f,&aai);printf(input xn);scanf(%f,&x);y=aa0;for(i=1;i1e-5);printf(the root is%fn,x1);,float f(float x)return x*x*x-x*x-2*x-3;float fd(float x)return 3*x*x-2*x-2;2、用牛顿法求 a 的立方根,精度要求为。#include main()double x0,x1;int a;printf(input an);scanf(%d,&a);/if(a=0)printf(a=0n);exit(0);x1=a;do x0=x1;x1=x0-(x*x*x-a)/(3*x*x);while(fabs(x1-x0)=;printf(the root is%fn,x1);printf(root=%fn,x1);3、用牛顿法求方程 x-ex=0(x-exp(-x)=0)在 1 附近的根,精度要求为。#include main()float x0,x1;x1=;do x0=x1;x1=x0-(x-exp(-x)/(1+exp(-x);while(fabs(x1-x0)1e-5);printf(the root is%fn,x1);牛顿法的算法:4 四、列主元高斯消元法(通用程序)#include#define M 3#define N 3 main()float aMN+1,xN,temp,l,s;int i,j,k,r;printf(please input the date:n);for(i=0;iM;i+)for(j=0;jN+1;j+):scanf(%f,&aij);for(k=0;kN-1;k+)r=k;for(i=k+1;ifabs(ark)r=i;if(ark=0)printf(QYn);exit(0);else if(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=;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 分解法解线性方程组,系数矩阵由二维数组 a 给出,右端由 b 数组给出。#include main()int i,j,k,m,n=4;float t;float b4=1,1,-1,-1;float a44=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;kn;k+)t=t+aik*bk;bi=(bi-t)/aii;for(i=0;in;i+)printf(%,bi);LU 分解法的算法:6 六、雅可比迭代法 1、通用程序#define N 4#include float cha(x,y)float xN,yN;float z;int i,k;¥z=fabs(y0-x0);for(i=1;iz)z=fabs(yi-xi);return(z);main()float aNN,bN,xN,yN,h;int i,j,k;float t=0,s;for(i=0;iN;i+)for(j=0;jN;j+)scanf(%f,&h);aij=h;for(i=0;iN;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题#include float cha(x,y)float x3,y3;float z;int i,k,n=3;z=fabs(y0-x0);for(i=1;iz)z=fabs(yi-xi);return(z);main()float a33=10,-2,-1,-2,10,-1,-1,-2,5;float b3=3,15,10,y3=0,0,0,x3;float t=0,s;int i,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,y)1e-6)printf(xunhuancishu:k=%dn,k);for(i=0;i30)printf(fasannn);6 、六、雅可比迭代法 1、通用程序#define N 4#include float cha(x,y)float xN,yN;float z;int i,k;z=fabs(y0-x0);for(i=1;iz)z=fabs(yi-xi);return(z);main()float aNN,bN,xN,yN,h;int i,j,k;float t=0,s;for(i=0;iN;i+)for(j=0;jN;j+)scanf(%f,&h);aij=h;for(i=0;iN;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题#include float cha(x,y)float x3,y3;float z;int i,k,n=3;z=fabs(y0-x0);for(i=1;iz)z=fabs(yi-xi);return(z);main()(float a33=10,-2,-1,-2,10,-1,-1,-2,5;float b3=3,15,10,y3=0,0,0,x3;float t=0,s;int i,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,y)1e-6)printf(xunhuancishu:k=%dn,k);for(i=0;i30)printf(fasannn);8 雅可比迭代法的算法:高斯-塞德尔迭代法的算法:八、牛顿基本插值公式(实验书第三题)main()int i,k,n;float x20,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(%,yi);printf(n);p=y0;h=1;for(i=1;i=n;i+)h=h*(t-xi-1);p+=h*yi;printf(p=%fn,p);牛顿基本插值公式算法:、9 九、Lagarange 全程插值算法(实验书第一题)main()int i,k,n;float x20,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 十一、多项式型经验公式算法 设实验数据如下:Xi Yi 求其二次拟合多项式#define N 7#define m 2#include main()int i,j,k;float xN+1=0,d,l;float yN+1=0,;float tm+1,temp,s;float am+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+)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);