zoukankan      html  css  js  c++  java
  • 【原创】开源Math.NET基础数学类库使用(08)C#进行数值积分

                   本博客所有文章分类的总目录:【总目录】本博客博文总目录-实时更新 

    开源Math.NET基础数学类库使用总目录:【目录】开源Math.NET基础数学类库使用总目录

    前言

      在数值计算的需求中,数值积分也是比较常见的一个。我们也知道像Matlab,Mathematics等软件的积分求解功能非常高大上,不仅能求解定积分,还能求解不定积分,甚至多重积分等等。而Math.NET这个组件没有如此高级的功能,目前也只提供了比较件的闭区间上的定积分求解功能。今天就一起来看看,因为不定积分涉及到符号计算,因此其背后的原理和实现要复杂得多。就连Matlab这种软件暂时也不支持混编编程求解符号计算相关的功能。

      如果本文资源或者显示有问题,请参考 本文原文地址http://www.cnblogs.com/asxinyu/p/4301017.html 

    1.定积分

      很多人可能已经淡忘了定积分的概念,当然需要用到的朋友看到这里,也基本不用看本段的内容,比较简单,高等数学已经是10多年前学过的东西了,虽然以前很精通,现在也只能凭印象理解和网络来对这个概念稍微进行整理,可能有些不完整或小错误,还请谅解。

      数学定义:如果函数f(x)在区间[a,b]上连续,用分点xi将区间[a,b]分为n 个小区间,在每个小区间[xi-1,xi]上任取一点ri(i=1,2,3„,n) ,作和式f(r1)+...+f(rn) ,当n趋于无穷大时,上述和式无限趋近于某个常数A,这个常数叫做y=f(x) 在区间上的定积分. 记作/ab f(x) dx 即 /ab f(x) dx =limn>00 [f(r1)+...+f(rn)], 这里,a 与 b叫做积分下限与积分上限,区间[a,b] 叫做积分区间,函数f(x) 叫做被积函数,x 叫做积分变量,f(x)dx 叫做被积式。

      几何定义:可以理解为在 Oxy坐标平面上,由曲线y=f(x)与直线x=a,x=b以及x轴围成的曲边梯形的面积值(一种确定的实数值)。

    详细的可以参考以下链接:

            定积分的计算公式和性质http://www.shuxuecheng.com/gaosuzk/content/lljx/wzja/5/5-2.htm

    2.Math.NET关于定积分的实现

       Math.NET中对定积分的实现都在MathNet.Numerics.Integration命名空间以及Integrate.cs中,Integrate静态类其实是对Integration命名空间下几个近似积分方法的实现。Math.NET定积分的近似求解主要是用到了“梯形法则”,详细的内容可以参考以下:链接,其原理非常简单。这里我们只介绍经常用到的Integrate静态类的实现,很简单,其他内部实现过程可以查源码:

     1 using System;
     2 using MathNet.Numerics.Integration;
     3 
     4 namespace MathNet.Numerics
     5 {
     6     /// <summary>
     7     /// 数值积分类
     8     /// </summary>
     9     public static class Integrate
    10     {
    11         /// <summary>      
    12         /// 近似解析光滑函数在闭区间上的定积分
    13         /// </summary>
    14         /// <param name="f">The analytic smooth function to integrate.</param>
    15         /// <param name="intervalBegin">Where the interval starts, inclusive and finite.</param>
    16         /// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param>
    17         /// <param name="targetAbsoluteError">The expected relative accuracy of the approximation.</param>
    18         /// <returns>Approximation of the finite integral in the given interval.</returns>
    19         public static double OnClosedInterval(Func<double, double> f, double intervalBegin, double intervalEnd, double targetAbsoluteError)
    20         {
    21             return DoubleExponentialTransformation.Integrate(f, intervalBegin, intervalEnd, targetAbsoluteError);
    22         }
    23 
    24         /// <summary>   
    25         ///  近似解析光滑函数在闭区间上的定积分
    26         /// </summary>
    27         /// <param name="f">The analytic smooth function to integrate.</param>
    28         /// <param name="intervalBegin">Where the interval starts, inclusive and finite.</param>
    29         /// <param name="intervalEnd">Where the interval stops, inclusive and finite.</param>
    30         /// <returns>Approximation of the finite integral in the given interval.</returns>
    31         public static double OnClosedInterval(Func<double, double> f, double intervalBegin, double intervalEnd)
    32         {
    33             return DoubleExponentialTransformation.Integrate(f, intervalBegin, intervalEnd, 1e-8);
    34         }
    35     }
    36 }

      下面的例子就是直接调用该类进行的。  

    3.C#使用Math.NET求解定积分的例子

       使用比较简单,直接看源码:

     1 // 1. Integrate x*x on interval [0, 10]
     2 Console.WriteLine(@"1.函数 x*x 在闭区间 [0, 10] 上的积分");
     3 var result = Integrate.OnClosedInterval(x => x * x, 0, 10);
     4 Console.WriteLine(result);
     5 Console.WriteLine();
     6 
     7 // 2. Integrate 1/(x^3 + 1) on interval [0, 1]
     8 Console.WriteLine(@"2.函数 1/(x^3 + 1) 在闭区间 [0, 1] 上的积分");
     9 result = Integrate.OnClosedInterval(x => 1 / (Math.Pow(x, 3) + 1), 0, 1);
    10 Console.WriteLine(result);
    11 Console.WriteLine();
    12 
    13 // 3. Integrate f(x) = exp(-x/5) (2 + sin(2 * x)) on [0, 10]
    14 Console.WriteLine(@"3.函数 f(x) = exp(-x/5) (2 + sin(2 * x)) 在 [0, 10]上的积分");
    15 result = Integrate.OnClosedInterval(x => Math.Exp(-x / 5) * (2 + Math.Sin(2 * x)), 0, 100);
    16 Console.WriteLine(result);
    17 Console.WriteLine();
    18 
    19 // 4. Integrate target function with absolute error = 1E-4
    20 Console.WriteLine(@"4. 对目标函数进行积分,绝对误差= 1E-4 ,区间 [0, 10]");
    21 Console.WriteLine(@"public static double TargetFunctionA(double x)
    22 {
    23 return Math.Exp(-x / 5) * (2 + Math.Sin(2 * x));
    24 }");
    25 result = Integrate.OnClosedInterval(TargetFunctionA, 0, 100, 1e-4);
    26 Console.WriteLine(result);
    27 Console.WriteLine();

    参数主要有3个:函数,积分下限,积分上限,其他的就是附带一个绝对误差了,看看运行结果:

    1.函数 x*x 在闭区间 [0, 10] 上的积分
    333.333333333332
    
    2.函数 1/(x^3 + 1) 在闭区间 [0, 1] 上的积分
    0.835648848264702
    
    3.函数 f(x) = exp(-x/5) (2 + sin(2 * x)) 在 [0, 10]上的积分
    10.4950494839272
    
    4. 对目标函数进行积分,绝对误差= 1E-4 ,区间 [0, 10]
    public static double TargetFunctionA(double x)
    {
        return Math.Exp(-x / 5) * (2 + Math.Sin(2 * x));
    }
    10.4950494839276

    4.资源

      源码下载:http://www.cnblogs.com/asxinyu/p/4264638.html

      如果本文资源或者显示有问题,请参考 本文原文地址http://www.cnblogs.com/asxinyu/p/4301017.html

  • 相关阅读:
    「HDU3640」I,Zombie
    气温变化折线图/matplotlib中文显示
    基本折线图
    根据统计数据画直方图
    matplotlib直方图绘制(hist)
    豆瓣Top250电影数据爬取学习
    pd.set_option参数设置
    django安装
    字典公共键
    字典排序
  • 原文地址:https://www.cnblogs.com/asxinyu/p/dotnet_Opensource_MathNet_NumericalIntegration_8.html
Copyright © 2011-2022 走看看