⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 integrate.cpp

📁 使用梯形法求函数的积分
💻 CPP
字号:
//沈硕 自动化5班 3004203132

#include<iostream.h>
#include<iomanip.h>
#include<math.h>
#define Pi 3.1415926535

double fun(double x,int i=0)//被积函数
{
	if(i)cout<<"被积函数:[sin(x)]^(1/3)"<<endl;
	return pow((sin(x)),1.0/3.0);
}
class Ident
{
	friend double fun(double x,int i);
private:double a,b;//积分上限b,下限a
		double e;//迭代限制--循环结束条件
public:Ident(double a1,double b1,double e1)
	   {
		   a=a1;b=b1;e=e1;
	   }
	   double diedai()
	   {
		   double h,t1,t2;//步长h,积分值t1,t2
		   int k=1;
		   h=b-a;
		   t1=h/2*(fun(a)+fun(b));//初值
		   do{
			   cout<<k<<'\t'<<setprecision(7)<<t1<<endl;
			   double s,x;
			   s=a;x=a+h/2;
			   //累加求和
			   do{
				   s+=fun(x);
				   x+=h;
			   }while(x<b);

			   t2=0.5*(t1+h*s);

			   if((t2-t1)>=e)//判断条件
			   {
				   h/=2;t1=t2;k++;
			   }
			   else return t2;
		   }while(1);
	   }
	   void print()
	   {
		   fun(a,1);
		   cout<<"积分下限"<<a<<",上限"<<b<<endl;
	   }

};
void main()
{
	Ident aa(0,Pi,1e-5);
	aa.print();
	cout<<"结果是:"<<aa.diedai()<<endl;
}


		   


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -