📄 1.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)
{
double n,h,k;
int i,j;
double t=0;
n=pow(2,q);
h=(b-a)/n;
k=(d-c)/n;
for(i=0;i<=n-1;i++)
for(j=0;j<=n-1;j++)
t+=h*k*(f(a+i*h,c+j*k)+f(a+i*h,c+(j+1)*k)+f(a+(i+1)*h,c+j*k)+f(a+(i+1)*h,c+(j+1)*k))/4;
return(t);
}
void rfun(double a,double b,double c,double d,double err)
{
int i,j,k,q;
double t[m][4]={0};
q=0;
for(i=0;i<=4;i++)
{
t[i][0]=tfun(a,b,c,d,q);
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)
{
cout<<"积分结果为:"<<t[1][3]<<endl;
cout<<"所需节点数为:"<<17<<endl;
}
i=4;
do
{
i++;
t[i][0]=tfun(a,b,c,d,q);
q=q+1;
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);
cout<<"积分结果为:"<<t[i-3][3]<<endl;
cout<<"所需节点数为:"<<pow(2,i)+1<<endl;
}
void main()
{
double a,b,c,d,err;
a=0;
b=3.1415926/6;
c=0;
d=3.1415926/3;
err=0.5e-5;
rfun(a,b,c,d,err);
}
/*积分结果为:0.33652
所需节点数为:65*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -