用龙贝格方法解个题目

来源:百度知道 编辑:UC知道 时间:2024/07/09 04:49:13
2. 用Romberg积分公式计算积分∫(sinx/x)dx 区间为[0,1] ,要求结果的误差限为 0.5*10^-5

// Romberg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

const double e = 1.0E-8;
const double end = 1.0E-6;

//积分上下限
const double a = 0.0;
const double b = 1.0;

//被积函数
double function(long double x)
{
if(abs(x)<e) return 1.0;
else return sin(x)/x;
}

int min(int x, int y)
{
return x>y?y:x;
}

int _tmain(int argc, _TCHAR* argv[])
{
double * temp;
double * t = new double[1];
int m = 1;
int j = 0;
double h;
t[0] = (b-a)/2*(function(a)+function(b));//t_0[0]
while(true)
{
temp = new double[m];//创建数组temp[m]
int k = sizeof(double)*min(4,m);
::memcpy(temp,t,k);//将t数组的数拷到temp数组
delete []t;
t = new double[m+1];//当前行比上一行多一个数
h = (b-a)/pow(2.0,m);//步长
t[0] = temp[0]/2;
for(int i = 1;i<=pow(2,m-1);i++)