数值分析实验代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

埃特金
#include <iostream.h>
#include <math.h>
float f(float x)
{float y;
y=exp(-x)
return y;}
float dd(float x0,float e,float n)
{float x1,x2;
int k;
for(k=1;k<=n;k++)
{x1=f(x0);
x2=f(x1);
x2=x2-(x2-x1)*(x2-x1)/(x2-2*x1+x0);
if(fabs(x2-x0)<e)
return x2;
cout<<k<<" "<<x2<<endl;
x0=x2;}
cout<<"error"<<endl;}
void main()
{float x0,e,n,y;
cin>>x0>>e>>n;
cout<<endl;
y=dd(x0,e,n);
cout<<y<<endl;
}变步长梯形积分
#include <stdio.h>
#include <iostream.h>
#include <math.h>
double a,b,h;
double f(double x)
{double y;
y=4/(1+x*x);
return y;}
double infe(double e,int n)
{int i;
double s,tn,t2n,x;
s=(1.0+f(b))/2.0;
h=(b-a)/n;
x=a+h;
for(i=1;i<=n-1;i++)
{s=s+f(x);
x=x+h;}t2n=s*h;
do
x=a+h/2.0;
for(i=1;i<=n;i++)
{s=s+f(x);
x=x+h;}
n=2*n;
h=h/2.0;
t2n=s*h;
cout<<t2n<<endl;
}while(fabs(t2n-tn)>=e);
return t2n;
}void main()
{ double p,e;
int n;
cin>>a>>b>>n>>e;
cout<<endl;
p=infe(e,n);
cout<<p<<endl;
}变步长新浦生积分
#include <stdio.h>
#include <iostream.h>
#include <math.h>
float a,b,e;
int n;
float f(float x)
{float y;
y=4/(1+x*x);
return y;}
float simpson(float e,int n) {int i;
float s,sn,s2n,p,x,h;
h=(b-a)/n;
s=f(a)+f(b);
p=0;
x=a+h;
for(i=1;i<=n-1;i++)
{if((i%2)!=0)
{p=p+4*f(x);
x=x+h;}
else{s=s+2*f(x);
x=x+h;} }
s=s+p;
s2n=s*h/3;
do
x=a+h/2;
s=s-p/2;
p=0;
for(i=1;i<=n;i++)
{p=p+4*f(x);
x=x+h;}
s=s+p;
n=n*2;
h=h/2;
s2n=s*h/3;
}while(fabs(s2n-sn)>e);
cout<<n<<endl;
return s2n;}
void main()
{float t;
cin>>a>>b>>e>>n;
cout<<endl;
t=simpson(e,n);
cout<<t<<endl;}迭代
#include <iostream.h>
#include <math.h>
float f(float x)
{float y;
y=exp(-x);
return y;}
float dd(float x0,float e,float n)
{float x1;
int k;
for(k=1;k<=n;k++)
{x1=f(x0);
if(fabs(x1-x0)<e)
return x1;
cout<<k<<" "<<x1<<endl;
x0=x1;}
cout<<"error"<<endl;
}void main()
{float x0,e,n,y;
cin>>x0>>e>>n;
cout<<endl;
y=dd(x0,e,n);
cout<<y<<endl;}定步长提醒积分#include <stdio.h>
#include <iostream.h>
#include <math.h>
float fna(float x);
void main()
{float a,b,s,h;
int n,i,j,k,right;
float y[100];
cin>>a>>b>>n>>right;
cout<<endl;
h=(b-a)/n;
if(right==0)
{s=(fna(a)+fna(b))/2;
for(i=1;i<=n-1;i++)
s=s+fna(a+i*h);}
else
{for(j=0;j<=n;j++)
cin>>y[j];
s=(y[0]+y[n])/2;
for(k=1;k<=n-1;k++)
s=s+y[k];}、
s=s*h;
cout<<s;}
float fna(float x)
{float y;
y=x;
return y;}二分法
#include <iostream.h>
float f(float x)
{float y;
y=(x*x*x)-x-1;
return y;}
float fot(float a,float b,float eps) {float x,y,y0;
do
{y0=f(a);
x=(a+b)/2;
y=f(x);
if(y*y0>0)
a=x;
else
b=x;}while(b-a>eps);
return x;}
void main(){
float a,b,eps,x;
cout<<"输入范围和误差范围:";
cin>>a>>b>>eps;
cout<<endl;
x=fot(a,b,eps);
cout<<x<<endl;
}改进欧拉
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
double f(double x,double y)
{double w;
w=y-2*x/y;
return w;}
double fr(double x,double y)
{double w;
w=pow(1+x*2,y);
return w;}
void main()
{double x,y,h,yp,yc,y0;
int i,n=10;
x=0;
y=1;
h=0.1;
cout<<" x:"<<" y:"<<" y0:"<<endl;
for(i=1;i<=n;i++)
{yp=y+h*f(x,y);
yc=y+h*f(x+h,yp);
y=(yp+yc)/2;
x=x+h;
y0=fr(x,0.5);
cout<<setw(5)<<setprecision(4)<<x;
cout<<setw(10)<<setprecision(7)<<y;
cout<<setw(10)<<setprecision(7)<<y0<<endl;} }高斯赛德尔
#include <iostream.h>
#include <math.h>
float a[100][100];
float b[100];
float x[100];
void main()
{int n,i,j;
cout<<"输入n:";
cin>>n;
cout<<endl;
cout<<"输入a[n][n]"<<endl;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cin>>a[i][j];
cout<<"输入b[n]"<<endl;
for(i=1;i<=n;i++)
cin>>b[i];
for(i=1;i<=n;i++)
{x[i]=0;
for(j=1;j<=n;j++)
cout<<a[i][j]<<" ";
cout<<b[i];
cout<<endl;}
float t,e,error,q;
cout<<"输入e:"<<endl;
cin>>e;
cout<<endl;
do
{error=0;
for(i=1;i<=n;i++)
{t=x[i];
q=0;
for(j=1;j<=n;j++)
if(j!=i)
q=q+a[i][j]*x[j];
x[i]=(b[i]-q)/a[i][i];
if(fabs(x[i]-t)>error)
error=x[i]-t;}}while(error>e);
for(i=1;i<=n;i++)
cout<<"x["<<i<<"]="<<x[i]<<endl;}加速迭代#include <stdio.h>
#include <iostream.h>
#include <math.h>
float f(float x);
float fr(float x);
void main()
{float x0,x1,e,q;
cin>>x0>>e;
x1=x0;
do
{x0=x1;
q=fr(x0);
x1=f(x0)+q/(1-q)*(f(x0)-x0);
}while(fabs(x1-x0)>=e);
cout<<x1<<endl;}
float f(float x)
{float y;
x=log(x)+89.62092;
y=exp(1.0/3.0*log10(x));
return y;}
float fr(float x)
{float y;
x=log(x)+89.62092;
y=1.0/3.0*exp(-2.0/3.0*log10(x));
return y;}科特斯
#include <iostream.h>
#include <math.h>
void main()
{double x,a,b,h,s;
int k,n;
cin>>a>>b>>n;
h=(b-a)/n;
s=(1-sin(b)/b)*7.0;
x=a;
for(k=1;k<=n;k++)
{x=x+h/4.0;
s=s+32*sin(x)/x;
x=x+h/4.0;
s=s+12*sin(x)/x;
x=x+h/4.0;
s=s+32*sin(x)/x;
x=x+h/4.0;
s=s+14*sin(x)/x;}
s=s*h/90.0;
cout<<s<<endl;}拉格朗日插值
#include <iostream.h>
float x1[100],y1[100];
float lagrange(int n,float x)
{ float y,t;
int k,j;
y=0;
for(k=0;k<=n;k++)
{ t=1;
for(j=0;j<=n;j++)
if(j!=k)
t=t*((x-x1[j])/(x1[k]-x1[j]));
y=y+t*y1[k]; }
return y;}
void main()
{ int i,n;
float y,x;
cout<<"请输入N:";
cin>>n;
cout<<endl;
cout<<"请输入X1和Y1:";
for(i=0;i<=n;i++)
cin>>x1[i]>>y1[i];
cout<<endl;
cout<<"请输入X:";
cin>>x;
cout<<endl;
y=lagrange(n,x);
cout<<y<<endl;
}龙贝格
#include <iostream.h>
#include <math.h>
float a,b,e;
float f(float x)
{float y;
y=4/(1+x*x);
return y;}
float romberg(float a,float b,float e) {float t1,t2,s1,s2,c1,c2,r1,r2;
float x,h,s;
int k;
h=b-a;
t2=(f(a)+f(b))/2*h;
k=0;
s2=0;
c2=0;
r2=0;
do{t1=t2;
s1=s2;
c1=c2;
r1=r2;
k=k+1;
x=a+h/2;
s=0;
while(x<b)
{s=s+f(x);
x=x+h;}
h=h/2;
t2=t1/2+s*h;
s2=t2+(t2-t1)/3;
if(k>=2)
c2=s2+(s2-s1)/15;
if(k>=3)
r2=c2+(c2-c1)/63;
}while((k<=3)||(fabs(r2-r1)>=e));
return r2;}
void main()
{float w;
cin>>a>>b>>e;
cout<<endl;
w=romberg(a,b,e);
cout<<w<<endl;
}牛顿切线
#include <stdio.h>
#include <iostream.h>
#include <math.h>
float f(float x);
float fr(float x);
void main()
{float x0,x1,e;
cin>>x0>>e;
x1=x0-f(x0)/fr(x0);
while(fabs(x1-x0)>=e)
{x0=x1;
printf("x=%f",x0);
x1=x0-f(x0)/fr(x0);
printf("=20.7%f",x1);
cout<<endl;}}
float f(float x)
{float y;
y=x*x-115;
return y;}
float fr(float x)
{float y;
y=2*x;
return y;}欧拉
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
double f(double x,double y) {double w;
w=y-2*x/y;
return w;}
double fr(double x,double y)
{double w;
w=pow(1+x*2,y);
return w;}
void main()
{double x,y,h,y0;
int i,n=10;
x=0;
y=1;
h=0.1;
cout<<" x:"<<" y:"<<" y0:"<<endl;
for(i=1;i<=n;i++)
{y=y+h*f(x,y);
x=x+h;
y0=fr(x,0.5);
cout<<setw(5)<<setprecision(4)<<x;
cout<<setw(10)<<setprecision(7)<<y;
cout<<setw(10)<<setprecision(7)<<y0<<endl;}}抛物插值
#include <iostream.h>
void main()
{int n,i,t;
float x1[100],y1[100],x,y;
cout<<"请输入N:";
cin>>n;
cout<<endl;
cout<<"请输入X1和Y1:";
for(i=0;i<=n;i++)
cin>>x1[i]>>y1[i];
cout<<endl;
cout<<"请输入X:";
cin>>x;
cout<<endl;
if(x<=x1[1])
t=1;
if(x>x1[n-1])
t=n-1;
for(i=1;i<=n-1;i++)
{if(x1[i-1]<x&&x<=x1[i])
{if((x-x1[i-1])>(x1[i]-x))
t=i;
if((x-x1[i-1])<=(x1[i]-x))
t=i-1;}}
y=((x-x1[t])*(x-x1[t+1])*(y1[t-1])/(x1[t-1]-x1[t])/(x1[t-1]-x1[t+1]) +(x-x1[t-1])*(x-x1[t+1])*(y1[t])/(x1[t]-x1[t-1])/(x1[t]-x1[t+1])
+(x-x1[t-1])*(x-x1[t])*(y1[t+1])/(x1[t+1]-x1[t-1])/(x1[t+1]-x1[t]));
cout<<y<<endl;
}秦九韶
#include <iostream.h>
void main()
{int n,i;
float a[100],x,v;
cin>>n;
cout<<endl;
for(i=1;i<=n;i++)
{cin>>a[i]; }
cout<<endl;
for(i=1;i<=n;i++)
{cout<<a[i];}
cout<<endl;
cin>>x;
cout<<endl;
v=a[n];
for(i=1;i<n;i++)
{v=x*v+a[n-i];}
cout<<v;}梯形积分递推法
#include <iostream.h>
#include <math.h>
float f(float x)
{float y;
y=sin(x)/x;
return y;}
void main()
{float a,b,e,h,t1,t2,s,x;
cin>>a>>b>>e;
cout<<endl;
h=b-a;
t2=(1+f(b))*h/2;
do
{t1=t2;
s=0;
x=a+h/2;
while(x<b){
s=s+f(x);
x=x+h;}
h=h/2;
t2=t1/2+s*h;
}while(fabs(t2-t1)>=e);
cout<<t2<<endl;
}线性插值
#include <iostream.h>
void main()
{int n,i,t;
float x1[100],y1[100],x,y;
cout<<"请输入N:";
cin>>n;
cout<<endl;
cout<<"请输入X1和Y1:";
for(i=0;i<=n;i++)
cin>>x1[i]>>y1[i];
cout<<endl;
cout<<"请输入X:";
cin>>x;
cout<<endl;
if(x<x1[0])
{t=1;}
if(x>=x1[n])
{t=n;}
for(i=0;i<=n;i++)
{if(x1[i]<=x&&x<x1[i+1])
t=i+1;}
y=y1[t]+(y1[t]-y1[t-1])*(x-x1[t])/(x1[t]-x1[t-1]);
cout<<y<<endl;
}新浦生
#include <iostream.h>
#include <math.h>
void main()
{double x,a,b,h,s;
int k,n;
cin>>a>>b>>n;
h=(b-a)/n;
s=1-sin(b)/b;x=a;
for(k=1;k<=n;k++)
{
x=x+h/2.0;
s=s+4.0*sin(x)/x;
x=x+h/2.0;
s=s+2.0*sin(x)/x;
}
s=s*h/6.0;
cout<<s<<endl;
}。

相关文档
最新文档