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

📄 变步长求积分.cpp

📁 用C++实现的多个算法
💻 CPP
字号:
#include<iostream.h>
#include<math.h>
#include<iomanip.h>
double infe(double e,int n);
double f(double x);
int i;
double s,tn,t2n,x,h,a,b;
double infe(double e,int n)
{
	s=(f(a)+f(b))/2.0;
	h=(b-a)/n;
	x=a+h;
	for(i=1;i<=n;i++)
	{
		s=s+f(x);
		x=x+h;
	}
	t2n=s*h;
	do{
		tn=t2n;
		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;
	}while(fabs(t2n-tn)>e);
		return t2n;
}
double f(double x)
{
    double y;
	y=sin(x)/x;
	return y;
}

void main()
{
	double p,e;
	int n;
	cout<<"请输入区间的上限b,下限a,精度e和n的值:"<<endl;
	cout<<"a=";cin>>a;
	cout<<"b=";cin>>b;
	cout<<"e=";cin>>e;
	cout<<"n=";cin>>n;
	p=infe(e,n);
	cout<<"函数f(x)=sinx/x的变步长积分近似值为:"<<setprecision(8)<<p<<endl;
}

⌨️ 快捷键说明

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