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

📄 龙贝格求积法.cpp

📁 数值分析 求积分算法源码
💻 CPP
字号:
// 龙贝格求积法.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <iostream>
#include <math.h>
#include <conio.h>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	//float a=0,b=2,e=0.000001,result;
	//float a=3.14159265/2,b=3.14159265*3/4,e=0.000001,result;
	float a=2,b=3,e=0.000001,result;

	float f(float x);
	float romberg(float (*f)(float),float a,float b,float e);

	cout<<"【龙贝格求积法计算定积分】"<<endl<<endl;	
	result=romberg(f,a,b,e);
	cout<<"运算结果是:"<<result<<endl;
	cout<<endl<<"按任意键继续...";
	getch();
	return 0;
}
//龙贝格算法函数
float romberg(float (*f)(float),float a,float b,float e)
{
	float t1,t2,s1,s2,c1,c2,r1=0,r2=e;
	float s,h,x,k=1;
	h=b-a,t1=h*(f(a)+f(b))/2;
	while(r2-r1>=e || r1-r2>=e)
	{
		for(s=0,x=a+h/2;x<b;x+=h)
		s+=f(x);
		t2=(t1+h*s)/2;
		s2=t2+(t2-t1)/3;
		if(k==1)
		{
			k++,h/=2;
			t1=t2,s1=s2;
			continue;
		}c2=s2+(s2-s1)/15;
		if(k==2)
		{
			k++,h/=2;
			t1=t2,s1=s2,c1=c2;
			continue;
		}r2=c2+(c2-c1)/63;
		r1=r2,c1=c2;
		t1=t2,s1=s2;
		k++,h/=2;
	}
	return r2;
}

//积分函数
float f(float x)
{
	//return x*x*exp(-x*x);
	//return 1/tan(x);
	return 1/(x*x-1);

}

⌨️ 快捷键说明

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