【原创】开源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
相关文章
- 【原创】开源Math.NET基础数学类库使用(14)C#生成安全的随机数
- 【原创】开源Math.NET基础数学类库使用(13)C#实现其他随机数生成器
- C#数据结构与算法揭秘17
- .NET(C#) Dapper Oracle(ODP.NET)或SQL Server 执行多条查询(select)语句的方法代码
- C#.NET常见问题(FAQ)-如何判断某个字符是否为汉字
- C#.NET常见问题(FAQ)-如何让listView如何选中一行
- C#.NET常见问题(FAQ)-listView如何显示网格线
- C#.NET常见问题(FAQ)-浮点数如何四舍五入
- .NET (C#) Internals: ASP.NET 应用程序与页面生命周期(意译)
- asp.net中C#对象与方法 属性详解
- C#[Serializable]在C#中的作用-NET 中的对象序列化
- C# .Net基础知识点解答
- C# 中使用System.Net.Http.HttpClient 模拟登录博客园 (GET/POST)
- 在C#代码中应用Log4Net(五)将Log4Net正确地封装在自己的类库中并进行调用
- C# Json反序列化 数据协定类型 无法反序列化 因为未找到必需的数据成员
- C# Protobuf-Net 序列化
- C#.Net操作注册表RegistryKey
- 【原创】开源Math.NET基础数学类库使用(17)C#计算矩阵条件数
- 【原创】开源Math.NET基础数学类库使用(15)C#计算矩阵行列式
- 【原创】开源Math.NET基础数学类库使用(14)C#生成安全的随机数
- 【原创】开源Math.NET基础数学类库使用(10)C#进行基本数据统计
- 【原创】开源Math.NET基础数学类库使用(04)C#解析Matrix Marke数据格式
- 【5.1送礼】国内第一部Matlab和C#.Net混合编程视频教程【免费】
- Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc
- Atitit.跨语言反射api 兼容性提升与增强 java c#。Net php js
- atitit.GUI图片非规则按钮跟动态图片切换的实现模式总结java .net c# c++ web html js
- Atitit.实现继承的原理and方法java javascript .net c# php ...
- C#基础学习之装箱,拆箱
- (八十三)c#Winform自定义控件-导航菜单(扩展)-HZHControls
- 资深C++程序员学习C#要点
- C#开源爬虫NCrawler源代码解读以及将其移植到python3.2(2)
- Tesseract .Net SDK C# OCR 2022.1
- 【CLR C#】面向面试的.Net的GC(垃圾回收)机制及其整体流程