复化梯形公式
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
namespace ConsoleApplication1 {
public class lbg
{
const int MAXRepeat = 100;
public double f(double x)//被积函数,根据自己的需要手工输入
{
double s;
s = 4.0 / (1 + x * x);
return s;
}
public double Romberg(double a, double b, double epsion)
{
int m = 1;
int n = 1;
int k;
double h;
double ep;
double p1;
double xk;
double p2;
double q = 0.0;
h = b - a;
p1 = 0.5 * h * (f(a) + f(b));
ep = epsion + 1.0;
while (ep >= epsion)
{
p1 = 0.0;
for (k = 0; k < n; k++)
{
xk = a + (k + 0.5) * h; // n-1
p2 = p2+ f(xk); //计算?f(xk+h/2),T
} // k=0
p2 = (p1 + h * p) / 2.0; //T`m`(h/2),变步长梯形求积公式
ep = Math.Abs(p2-p1);
m++;
p1 = p2;
n *= 2; // 2 4 8 16
h /= 2.0;
}
return q;
}
}
class Program
{
static void Main(string[] args)
{
lbg l = new lbg();
Console.Write("请输入下界值:");
double x = double.Parse(Console.ReadLine());
Console.Write("请输入上界值:");
double y = double.Parse(Console.ReadLine());
double s = l.Romberg(x, y, 0.000001);
Console.Write("积分值为:{0}", s.ToString("0.000000000"));
Console.ReadLine();
}
}
}