Browse Source

Merge pull request #394 from yoon-gu/odesolver

ODE Solvers(RK2 & RK4)
netstandard
Christoph Ruegg 11 years ago
parent
commit
280d09f0f3
  1. 1
      src/Numerics/Numerics.csproj
  2. 156
      src/Numerics/OdeSolvers/OdeSolvers.cs
  3. 91
      src/UnitTests/OdeSolvers/OdeSolverTest.cs
  4. 1
      src/UnitTests/UnitTests.csproj

1
src/Numerics/Numerics.csproj

@ -112,6 +112,7 @@
<Compile Include="LinearAlgebra\MatrixExtensions.cs" />
<Compile Include="LinearAlgebra\VectorExtensions.cs" />
<Compile Include="LinearRegression\Options.cs" />
<Compile Include="OdeSolvers\OdeSolvers.cs" />
<Compile Include="Precision.Comparison.cs" />
<Compile Include="Precision.Equality.cs" />
<Compile Include="Distributions\Bernoulli.cs" />

156
src/Numerics/OdeSolvers/OdeSolvers.cs

@ -0,0 +1,156 @@
// <copyright file="OdeSolvers.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2016 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using MathNet.Numerics.LinearAlgebra;
using System;
namespace MathNet.Numerics.OdeSolvers
{
/// <summary>
/// ODE Solver Algorithms
/// </summary>
public static class RungeKutta
{
/// <summary>
/// Second Order Runge-Kutta method
/// </summary>
/// <param name="y0">initial value</param>
/// <param name="start">start time</param>
/// <param name="end">end time</param>
/// <param name="N">Number of subintervals</param>
/// <param name="f">ode function</param>
/// <returns>approximations</returns>
public static double[] SecondOrder(double y0, double start, double end, int N, Func<double, double, double> f)
{
double dt = (end - start) / (N - 1);
double k1 = 0;
double k2 = 0;
double t = start;
double[] y = new double[N];
y[0] = y0;
for (int i = 1; i < N; i++)
{
k1 = f(t, y0);
k2 = f(t + dt, y0 + k1 * dt);
y[i] = y0 + dt * 0.5 * (k1 + k2);
t += dt;
y0 = y[i];
}
return y;
}
/// <summary>
/// Fourth Order Runge-Kutta method
/// </summary>
/// <param name="y0">initial value</param>
/// <param name="start">start time</param>
/// <param name="end">end time</param>
/// <param name="N">number of subintervals</param>
/// <param name="f">ode function</param>
/// <returns>approximations</returns>
public static double[] FourthOrder(double y0, double start, double end, int N, Func<double, double, double> f)
{
double dt = (end - start) / (N - 1);
double k1 = 0;
double k2 = 0;
double k3 = 0;
double k4 = 0;
double t = start;
double[] y = new double[N];
y[0] = y0;
for (int i = 1; i < N; i++)
{
k1 = f(t, y0);
k2 = f(t + dt / 2, y0 + k1 * dt / 2);
k3 = f(t + dt / 2, y0 + k2 * dt / 2);
k4 = f(t + dt, y0 + k3 * dt);
y[i] = y0 + dt / 6 * (k1 + 2 * k2 + 2 * k3 + k4);
t += dt;
y0 = y[i];
}
return y;
}
/// <summary>
/// Second Order Runge-Kutta to solve ODE SYSTEM
/// </summary>
/// <param name="y0">initial vector</param>
/// <param name="start">start time</param>
/// <param name="end">end time</param>
/// <param name="N">number of subintervals</param>
/// <param name="f">ode function</param>
/// <returns>approximations</returns>
public static Vector<double>[] SecondOrder(Vector<double> y0, double start, double end, int N, Func<double, Vector<double>, Vector<double>> f)
{
double dt = (end - start) / (N - 1);
Vector<double> k1, k2;
Vector<double>[] y = new Vector<double>[N];
double t = start;
y[0] = y0;
for (int i = 1; i < N; i++)
{
k1 = f(t, y0);
k2 = f(t, y0 + k1 + dt);
y[i] = y0 + 0.5 * (k1 + k2);
t += dt;
y0 = y[i];
}
return y;
}
/// <summary>
/// Fourth Order Runge-Kutta to solve ODE SYSTEM
/// </summary>
/// <param name="y0">initial vector</param>
/// <param name="start">start time</param>
/// <param name="end">end time</param>
/// <param name="N">number of subintervals</param>
/// <param name="f">ode function</param>
/// <returns>approximations</returns>
public static Vector<double>[] FourthOrder(Vector<double> y0, double start, double end, int N, Func<double, Vector<double>, Vector<double>> f)
{
double dt = (end - start) / (N - 1);
Vector<double> k1, k2, k3, k4;
Vector<double>[] y = new Vector<double>[N];
double t = start;
y[0] = y0;
for (int i = 1; i < N; i++)
{
k1 = f(t, y0);
k2 = f(t + dt / 2, y0 + k1 * dt / 2);
k3 = f(t + dt / 2, y0 + k2 * dt / 2);
k4 = f(t + dt, y0 + k3 * dt);
y[i] = y0 + dt / 6 * (k1 + 2 * k2 + 2 * k3 + k4);
t += dt;
y0 = y[i];
}
return y;
}
}
}

91
src/UnitTests/OdeSolvers/OdeSolverTest.cs

@ -0,0 +1,91 @@
// <copyright file="OdeSolverTest.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2016 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using NUnit.Framework;
using System;
using MathNet.Numerics.OdeSolvers;
using System.Linq;
namespace MathNet.Numerics.UnitTests.OdeSolvers
{
/// <summary>
/// ODE Solver tests.
/// </summary>
[TestFixture, Category("OdeSolver")]
public class OdeSolverTest
{
/// <summary>
/// Runge-Kutta second order method for first order ODE.
/// </summary>
[Test]
public void RK2Test()
{
Func<double, double, double> ode = (t, y) => t + 2 * y * t;
Func<double, double> sol = (t) => 0.5 * (Math.Exp(t * t) - 1);
double ratio = double.NaN;
double error = 0;
double oldError = 0;
for (int k = 0; k < 4; k++)
{
double y0 = 0;
double[] y_t = RungeKutta.SecondOrder(y0, 0, 2, Convert.ToInt32(Math.Pow(2, k + 6)), ode);
error = Math.Abs(sol(2) - y_t.Last());
if (oldError != 0)
ratio = Math.Log(oldError / error, 2);
oldError = error;
Console.WriteLine(string.Format("{0}, {1}", error, ratio));
}
Assert.AreEqual(2, ratio, 0.01);// Check error convergence order
}
/// <summary>
/// Runge-Kutta fourth order method for first order ODE.
/// </summary>
[Test]
public void RK4Test()
{
Func<double, double, double> ode = (t, y) => t + 2 * y * t;
Func<double, double> sol = (t) => 0.5 * (Math.Exp(t * t) - 1);
double ratio = double.NaN;
double error = 0;
double oldError = 0;
for (int k = 0; k < 4; k++)
{
double y0 = 0;
double[] y_t = RungeKutta.FourthOrder(y0, 0, 2, Convert.ToInt32(Math.Pow(2, k + 6)), ode);
error = Math.Abs(sol(2) - y_t.Last());
if (oldError != 0)
ratio = Math.Log(oldError / error, 2);
oldError = error;
Console.WriteLine(string.Format("{0}, {1}", error, ratio));
}
Assert.AreEqual(4, ratio, 0.01);// Check error convergence order
}
}
}

1
src/UnitTests/UnitTests.csproj

@ -362,6 +362,7 @@
<Compile Include="LinearAlgebraTests\MatrixStorageCombinatorsTests.cs" />
<Compile Include="LinearAlgebraTests\VectorStorageCombinatorsTests.cs" />
<Compile Include="LinearAlgebraTests\VectorToStringTests.cs" />
<Compile Include="OdeSolvers\OdeSolverTest.cs" />
<Compile Include="Random\SystemRandomSourceTests.cs" />
<Compile Include="RootFindingTests\BisectionTest.cs" />
<Compile Include="PermutationTest.cs" />

Loading…
Cancel
Save