计算方法(二)用C#实现数值积分

时间:2022-05-23 01:06:03

在工程中,经常会遇到积分问题,这时原函数往往都是找不到的,因此就需要用计算方法的数值积分来求。

    public class Integral
{
/// <summary>
/// 梯形公式
/// </summary>
/// <param name="fun">被积函数</param>
/// <param name="up">积分上限</param>
/// <param name="down">积分下限</param>
/// <returns>积分值</returns> public static double TiXing(Func<double, double> fun, double up, double down)
{
return (up - down) / * (fun(up) + fun(down));
}
/// <summary>
/// 辛普森公式
/// </summary>
/// <param name="fun">被积函数</param>
/// <param name="up">积分上限</param>
/// <param name="down">积分下限</param>
/// <returns>积分值</returns>
public static double Simpson(Func<double, double> fun, double up, double down)
{
return (up - down) / * (fun(up) + fun(down) + * fun((up + down) / ));
}
/// <summary>
/// 科特克斯公式
/// </summary>
/// <param name="fun">被积函数</param>
/// <param name="up">积分上限</param>
/// <param name="down">积分下限</param>
/// <returns>积分值</returns>
public static double Cotes(Func<double, double> fun, double up, double down)
{
double C = (up - down) / * ( * fun(up) + * fun(down) + * fun((up + * down) / )
+ * fun((up + down) / ) + * fun(( * up + down) / ));
return C;
} /// <summary>
/// 复化梯形公式
/// </summary>
/// <param name="fun">被积函数</param>
/// <param name="N">区间划分快数</param>
/// <param name="up">积分上限</param>
/// <param name="down">积分下限</param>
/// <returns>积分值</returns>
public static double FuHuaTiXing(Func<double, double> fun, int N, double up, double down)
{
double h = (up - down) / N;
double result = ;
double x = down;
for (int i = ; i < N - ; i++)
{
x += h;
result += fun(x);
}
result = (fun(up) + result * + fun(down)) * h / ;
return result;
} /// <summary>
/// 复化辛浦生公式
/// </summary>
/// <param name="fun">被积函数</param>
/// <param name="N">区间划分快数</param>
/// <param name="up">积分上限</param>
/// <param name="down">积分下限</param>
/// <returns>积分值</returns>
public static double FSimpson(Func<double, double> fun, int N, double up, double down)
{
double h = (up - down) / N;
double result = ;
for (int n = ; n < N; n++)
{
result += h / * (fun(down) + * fun(down + h / ) + fun(down + h));
down += h;
}
return result;
}
/// <summary>
/// 复化科特斯公式
/// </summary>
/// <param name="fun">被积函数</param>
/// <param name="N">区间划分快数</param>
/// <param name="up">积分上限</param>
/// <param name="down">积分下限</param>
/// <returns>积分值</returns>
public static double FCotes(Func<double, double> fun, int N, double up, double down)
{
double h = (up - down) / N;
double result = ;
for (int n = ; n < N; n++)
{
result += h / * ( * fun(down) + * fun(down + h / ) + * fun(down + h / ) +
* fun(down + * h / ) + * fun(down + h));
down += h;
}
return result;
}
/// <summary>
/// 龙贝格算法
/// </summary>
/// <param name="fun">被积函数</param>
/// <param name="e">结果精度</param>
/// <param name="up">积分上限</param>
/// <param name="down">积分下限</param>
/// <returns>积分值</returns>
public static double Romberg(Func<double, double> fun, double e, double up, double down)
{
double R1 = , R2 = ;
int k = ; //2的k次方即为N(划分的子区间数)
R1 = ( * C(fun, * (int)Math.Pow(, k), up, down) - C(fun, (int)Math.Pow(, k++), up, down)) / ;
R2 = ( * C(fun, * (int)Math.Pow(, k), up, down) - C(fun, (int)Math.Pow(, k++), up, down)) / ;
while (Math.Abs(R2 - R1) > e)
{
R1 = R2;
R2 = ( * C(fun, * (int)Math.Pow(, k), up, down) - C(fun, (int)Math.Pow(, k++), up, down)) / ;
}
return R2;
}
private static double S(Func<double, double> fun, int N, double up, double down)
{
return ( * FuHuaTiXing(fun, * N, up, down) - FuHuaTiXing(fun, N, up, down)) / ;
}
private static double C(Func<double, double> fun, int N, double up, double down)
{
return ( * S(fun, * N, up, down) - S(fun, N, up, down)) / ;
}
}