# c#使用MathNet.Numeric求解微分方程组

MathNet.Numeric包安装：

NuGet Gallery | MathNet.Numerics 4.15.0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.OdeSolvers;

namespace GNC
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

Run();
}

public void Run()
{
int N = 1000000;
Vector<double> y0 = Vector<double>.Build.Dense(new[] { -7.0 / 4.0, 55.0 / 8.0 });
Func<double, Vector<double>, Vector<double>> der = DerivativeMaker();

Vector<double>[] res = RungeKutta.FourthOrder(y0, 0, 10, N, der);

double[] x = new double[N];
double[] y = new double[N];
for (int i = 0; i < N; i++)
{
double[] temp = res[i].ToArray();
x[i] = temp[0];
y[i] = temp[1];
}

//Test
Console.WriteLine(y[N / 10]); // gives 164,537981852489
Console.WriteLine(Math.Exp(-1) + 3 * Math.Exp(4) - 5.0 / 2 + 23.0 / 8); //gives 164,537329540604, which is y(1)

}

static Func<double, Vector<double>, Vector<double>> DerivativeMaker()
{
return (t, Z) =>
{
double[] A = Z.ToArray();
double x = A[0];
double y = A[1];

return Vector<double>.Build.Dense(new[] { x + 2 * y + 2 * t, 3 * x + 2 * y - 4 * t });

};
}
}
}


四阶龙格库塔RungeKutta.FourthOrder各个参数含义参见

RungeKutta - Math.NET Numerics Documentation

• 1
点赞
• 6
收藏
• 打赏
• 0
评论
05-29
11-26 16
04-01 2万+
03-17 1万+
01-10 29
05-15 8911
10-10 784
08-22 3656
01-15 6574

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。