• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

    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) / 2 * (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) / 6 * (fun(up) + fun(down) + 4 * fun((up + down) / 2));
        }
        /// <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) / 90 * (7 * fun(up) + 7 * fun(down) + 32 * fun((up + 3 * down) / 4)
                     + 12 * fun((up + down) / 2) + 32 * fun((3 * up + down) / 4));
            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 = 0;
            double x = down;
            for (int i = 0; i < N - 1; i++)
            {
                x += h;
                result += fun(x);
            }
            result = (fun(up) + result * 2 + fun(down)) * h / 2;
            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 = 0;
            for (int n = 0; n < N; n++)
            {
                result += h / 6 * (fun(down) + 4 * fun(down + h / 2) + 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 = 0;
            for (int n = 0; n < N; n++)
            {
                result += h / 90 * (7 * fun(down) + 32 * fun(down + h / 4) + 12 * fun(down + h / 2) +
                        32 * fun(down + 3 * h / 4) + 7 * 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 = 0, R2 = 0;
            int k = 0; //2的k次方即为N(划分的子区间数)
            R1 = (64 * C(fun, 2 * (int)Math.Pow(2, k), up, down) - C(fun, (int)Math.Pow(2, k++), up, down)) / 63;
            R2 = (64 * C(fun, 2 * (int)Math.Pow(2, k), up, down) - C(fun, (int)Math.Pow(2, k++), up, down)) / 63;
            while (Math.Abs(R2 - R1) > e)
            {
                R1 = R2;
                R2 = (64 * C(fun, 2 * (int)Math.Pow(2, k), up, down) - C(fun, (int)Math.Pow(2, k++), up, down)) / 63;
            }
            return R2;
        }
        private static double S(Func<double, double> fun, int N, double up, double down)
        {
            return (4 * FuHuaTiXing(fun, 2 * N, up, down) - FuHuaTiXing(fun, N, up, down)) / 3;
        }
        private static double C(Func<double, double> fun, int N, double up, double down)
        {
            return (16 * S(fun, 2 * N, up, down) - S(fun, N, up, down)) / 15;
        }
    }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#静态成员和方法的学习小结发布时间:2022-07-14
下一篇:
C#枚举运用&quot;位&quot;操作和&quot;或&quot;操作发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap