📄 外推法计算重积分.cpp
字号:
#include <iostream.h>
#include <math.h>
#define m 20
double f(double x,double y)
{
double f;
f=tan(pow(x,2)+pow(y,2));
return f;
}
double tfun(double a,double b,double c,double d,int q)
{
static int flag=0;
double n,h,k;
int i,j;
static double t;
double temp1=0,temp2=0,temp3=0,temp4=0,temp5=0;
n=pow(2,q);
h=(b-a)/n;
k=(d-c)/n;
if(flag==0)
{
t=h*k/4*(f(a,c)+f(a,c+k)+f(a+h,c)+f(a+h,c+k));
flag=1;
return(t);
}
for(i=0;i<=n-1;i++)
{
temp1=temp1+f(a+i*h+h/2,c)+f(a+i*h+h/2,d);
}
for(j=0;j<=n-1;j++)
{
temp2=temp2+f(a,c+j*k+k/2)+f(b,c+j*k+k/2);
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
j++;
temp3=temp3+f(a+i*h+h/2,c+j*k);
j--;
i++;
temp4=temp4+f(a+i*h,c+j*k+k/2);
i--;
temp5=temp5+f(a+i*h+h/2,c+j*k+k/2);
}
}
t=t/4+h*k*(temp1+temp2+temp3+temp4+2*temp5)/8;
return(t);
}
double rfun(double a,double b,double c,double d,double err)
{
int i,j,k,q;
double t[m][4]={0};
q=0;
t[0][0]=tfun(a,b,c,d,q);
for(i=1;i<=4;i++)
{
q++;
t[i][0]=tfun(a,b,c,d,q);
}
for(j=1;j<=3;j++)
{
for(i=0;i<=4-j;i++)
{
t[i][j]=(pow(2,2*j)*t[i+1][j-1]-t[i][j-1])/(pow(2,2*j)-1);
}
}
if(fabs(t[1][3]-t[0][3])<err)
{
return(t[1][3]);
}
i=4;
do
{
i++;
q=q+1;
t[i][0]=tfun(a,b,c,d,q);
for(j=1;j<=3;j++)
{
k=i-j;
t[k][j]=(pow(2,2*j)*t[k+1][j-1]-t[k][j-1])/(pow(2,2*j)-1);
}
}while(fabs(t[i-3][3]-t[i-4][3])>err);
return(t[i-3][3]);
}
void main()
{
double a,b,c,d,err,result;
a=0;
b=3.1415926/6;
c=0;
d=3.1415926/3;
err=0.5e-5;
result=rfun(a,b,c,d,err);
cout<<"积分结果为:"<<result<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -