数值分析实验答案(共9页).doc
精选优质文档-倾情为你奉上实验0 截断误差与舍入误差#include "stdio.h"#include "math.h"const double ln2=0.6;const double e1=5e-6;void main() int sign; double s; long i; s=0.0; sign=1; i=1; while(fabs(ln2-s)>=e1) s+=(1.0/i)*sign; sign=-sign; i+; printf("n=%ldn",i-1); getch();实验1 拉格朗日插值法编写拉格朗日插值法通用子程序,并用以下函数表来上机求,。x0.00.10.1950.30.4010.5f (x)0.398940.396950.391420.381380.368120.35206#include <stdio.h>main() static float Lx10,Ly10; int n,i,j; float x,y,p; printf("enter n="); scanf("%d",&n); printf("enter xin"); for(i=0;i<n;i+) scanf("%f",&Lxi); printf("enter yin"); for(i=0;i<n;i+) scanf("%f",&Lyi); printf("enter x="); scanf("%f",&x); /* n=6; Lx0=0; Lx1=0.1; Lx2=0.195; Lx3=0.3; Lx4=0.401; Lx5=0.5; Ly0=0.39894; Ly1=0.39695; Ly2=0.39142; Ly3=0.38138; Ly4=0.36812; Ly5=0.35206; x=0.15; */ for( i=0;i<n;i+) p=1; for(j=0;j<n;j+) if(i!=j) p=p*(x-Lxj)/(Lxi-Lxj); y+=p*Lyi; printf("y=%fn",y); getch();实验2 最小二乘法测得铜导线在温度()时的电阻如下表,求电阻R与温度T的近似函数关系。i0123456()19.125.030.136.040.045.150.076.3077.8079.2580.8082.3583.9085.10#include <stdio.h>#include <math.h>float gs(float a2020,float b20,int n )int i,j,k,l; float s; k=1;while(k!=n+1)if(akk!=0)for(i=k+1;i<=n+1;i+)aik=aik/akk;bi=bi-aik*bk;for(j=k+1;j<=n+1;j+) aij=aij-aik*akj;k=k+1;for(k=n+1;k>=1;k-)s=0;for(l=k+1;l<=n+1;l+)s=s+akl*bl;bk=(bk-s)/akk; return 0;main()static float b20,Lx20,Ly20,c2020,ct2020,a2020;int m,n,i,j,k=0,l;float s,rtn;printf("enter m=");scanf("%d",&m); printf("enter n="); scanf("%d",&n);printf("enter xin"); for(i=1;i<=m;i+) scanf("%f",&Lxi);printf("enter yin"); for(i=1;i<=m;i+) scanf("%f",&Lyi);for( i=1;i<=m;i+) ci1=1; for(j=2;j<=n+1;j+) cij=Lxi*cij-1; for( i=1;i<=m;i+) for(j=1;j<=n+1;j+) ctji=cij; for( i=1;i<=n+1;i+) for(j=1;j<=n+1;j+) aij=0; for( i=1;i<=n+1;i+) bi=0; for( i=1;i<=n+1;i+) for(k=1;k<=n+1;k+) for(j=1;j<=m;j+)aik=aik+ctij*cjk; for(i=1;i<=n+1;i+) for(j=1;j<=m;j+) bi+=ctij*Lyj;gs(a,b,n);printf("nThe result is:");for(i=1;i<=n+1;i+) j=i-1; printf("na%d=%f",j,bi); 实验3 变步长复合梯形公式求 的近似值。(1)编写定步长复合梯形程序求解上式;(2)编写变步长复合梯形程序求解上式,使误差不超过10-6。#include"math.h"double f(double x) double f1; f1=4/(1+x*x);return(f1) ;double trapezia2(int k,double h,double a,double tn) double t2n,s=0; int i; t2n=tn/2; for(i=1;i<=pow(2,k);i+) s+=f(a+(2*i-1)*h); t2n=t2n+s*h; return t2n; void main()double a,b,h,Tn,T2n,e; int n,k; printf("please input a,b,n,en"); scanf("%lf%lf%d%lf",&a,&b,&n,&e); h=b-a; Tn=(b-a)*(f(a)+f(b)/2; for(k=0;k<n;k+) h=h/2; T2n=trapezia2(k,h,a,Tn); if(fabs(T2n-Tn)<e) break; printf("%lfn",Tn); Tn=T2n; printf("%lfn",Tn);实验3 定步长复合梯形公式double f(double x) return(4/(1+x*x) ;float trapezia(int n,float h,float a)float fk=0; int i; for(i=1;i<n;i+) fk+=f(a+i*h); return(fk*h);void main()float a,b,h,Tn; int n; printf("please input a,b,n"); scanf("%f%f%d",&a,&b,&n); h=(b-a)/n; Tn=(f(a)+f(b)*h/2; Tn+=trapezia(n,h,a); printf("%f",Tn) ;实验5 非线性方程求解编写Newton迭代法通用子程序。实现方程f(x)=x6-x-1=0的满足精度要求的解。要求求解过程中用一个变量I控制三种状态,其中:i=0表示求解满足给定精度的近似解;i=1表示f(x0)=0,计算中断;i=2表示迭代n次后精度要求仍不满足。#define N 1000#include<math.h>main() double x,x0,p=1e-6; double f(double x),g(double x); int i,j,k,n=0; printf("n enter x0:"); scanf("%lf",&x0); do x=x0-f(x0)/g(x0); printf("n %d %12.8lf %8.3le",n,x0,f(x0); if(n<N) n=n+1; x0=x; else printf("n FAILURE!"); break; while(fabs(f(x0)>p); printf("n the result is:%lf",x0);/*MAIN*/double f(double x) return(pow(x,6)-x-1);/*F(X)*/double g(double x) return(6*pow(x,5)-1);/*G(X)*/实验6 高斯消元法编写选列主元的高斯消去法。求出下列线性方程组Ax=b的解x。#include <math.h>#define N 10main()int i,ik,k=0,j,l,n,max;static float aNN,bN,t,s,min=1e-6;printf("n enter n:");scanf("%d",&n);printf("nenter A=(aij)(line first):");for(i=0;i<n;i+)for(j=0;j<n;j+)scanf("%f",&aij);printf("nenter b:");for(i=0;i<n;i+)scanf("%f",&bi);for(k=0;k<n-1;k+) max=0; for(i=k;i<n;i+) if (max-fabs(aik)<0) max=fabs(aik); ik=i; if(max<min) break;if(ik!=k)for(j=k;j<n;j+) t=aikj;aikj=akj;akj=t;t=bik;bik=bk;bk=t;for(i=k+1;i<n;i+) aik=aik/akk; for(j=k+1;j<n;j+)aij= aij- aik* akj; bi=bi-aik* bk; aik=0; bn-1=bn-1/an-1n-1; for(i=n-2;i>=0;i-) for(j=i+1;j<n;j+) bi= bi- aij* bj; bi= bi/aii; printf("nThe result is:");for(i=0;i<n;i+)j=n-i;printf("nx%d=%f",j,bi);实验7 改进欧拉法#include "math.h"#include "stdio.h"float f(float x, float y) return(2.0/x*y+x*x*exp(x); float g(float x) return(x*x*(exp(x)-exp(1); void main()float x,y,h,a,b,a0,yp,yc; printf("please input a,b,h and a0n"); scanf("%f%f%f%f",&a,&b,&h,&a0); x=a; y=a0; while(x<=b+h) printf("x=%f,y=%f,y(x)=%f,y(x)-y=%fn",x,y,g(x),fabs(y-g(x); yp=y+h*f(x,y); x+=h; yc=y+h*f(x,yp); y=0.5*(yp+yc); 实验7 龙格库塔法#include <conio.h>#define f(x,y) (x-y)main()float a,b,h,x,y,k1,k2,k3,k4,y0,x0;int n=0,i,j,N;clrscr();printf("n enter N:");scanf("%d",&N);printf("nenter a,b:");scanf("%f%f",&a,&b);printf("n enter primary data y(0): ");scanf("%f",&y0);h=(b-a)/N;x0=a;printf("n%10.5f%10.5f",x0,y0);dox=x0+h;k1=f(x0,y0);k2=f(x0+h/2),(y0+h/2*k1);k3=f(x0+h/2),(y0+h/2*k2);k4=f(x0+h),(y0+h*k3);y=y0+h/6*(k1+2*(k2+k3)+k4);printf("n%10.5f%10.5f",x,y);n=n+1;x0=x;y0=y;while(n<N);专心-专注-专业