本科课程参见:《软件学院那些课》
将积分区间[a,b]划分n等分,步长,求积节点,在每个小区间上应用梯形公式
double f(double x){ if(x==0) return 1; else return (sin(x)/x); } double FuhuaTixing(int n,double a,double b){ double h = (b-a)/n; double x = a; double s = 0; for(int k=0; k< n-1; k++){ x += h; s += f(x); } double T = (f(a)+s*2+f(b))*h/2; return T; } int main(){ char ans='n'; do{ cout<<"请输入积分区间(a,b):"<<endl; double a; double b; cin>>a>>b; cout<<"请输入等分份数n: "<<endl; int n; cin>>n; cout<<"由复化梯形公式球的结果:"<<FuhuaTixing(n,a,b)<<endl; cout<<"是否要继续?(y/n)"; cin>>ans; }while(ans == 'y'); return 0; }
double f(double x){ if (x==0) return 1; else return (sin(x)/x); } double Xinfusheng(double a,double b,int n){ double h = (b-a)/n; double x = a+1/2*h; double s = 4*f(x); for(int k=1;k<n;k++){ x += 1/2*h; s += 4*f(x); x += 1/2*h; s += 2*f(x); } double T=(f(a)+s+f(b))*h/6; return T; } int main(){ char ans='n'; do{ cout<<"请输入积分区间(a,b):"<<endl; double a; double b; cin>>a>>b; cout<<"请输入等分份数n: "<<endl; int n; cin>>n; cout<<"由复化梯形公式球的结果:"<<Xinfusheng(a,b,n)<<endl; cout<<"是否要继续?(y/n)"; cin>>ans; }while(ans == 'y'); return 0; }