步骤:
1 安装和使用NuGet插件
教程链接:https://blog.csdn.net/qq_36456952/article/details/55252913
2 安装 C# 科学计算库 Math.NET Numerics
教程链接: https://blog.csdn.net/heray1990/article/details/72467304
注意:在安装 C# 科学计算库 Math.NET Numerics可能会出现:
Install-Package : “MathNet.Numerics”的架构版本与 NuGet 的版本 2.0.30625.9003 不兼容。请通过 http://go.microsoft.com/fwlink/?LinkId=213942 将 NuGet 升级到最新版本。
这时我们需要去到NuGet的官网:https://www.nuget.org/
I:在官网的搜索框中输入: MathNet.Numerics
如图所示:
II 在搜索出来的结果中,选择第一个,如下图:
III 打开后选择一个低一点的版本,我选择的版本 是2.3.0,然后复制Install-Package MathNet.Numerics -Version 2.3.0到vs程序包管理器控制台
如下图:
3 完成上面的步骤,就可以进行编码操作了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathNet.Numerics.Integration; //需要添加的命名空间
namespace 求积分
{
class Program
{
static void Main(string[] args)
{
double result;
//计算x的平方,积分下限时0,积分上限是10,不指定误差
result = Integrate.OnClosedInterval(x => x * x, 0, 10);
Console.WriteLine("积分不指定误差的结果是:"+result);
//指定误差,误差为10的-2次方
double result1;
result1 = Integrate.OnClosedInterval(x => x * x, 0, 10, 1e-2);
Console.WriteLine("积分指定误差的结果是"+result1);
}
}
}
运行结果: