From 40e32aaf576c59423f5fc2d4ad63331846688f10 Mon Sep 17 00:00:00 2001 From: Yoonku Hwang Date: Tue, 17 May 2016 16:58:48 +0900 Subject: [PATCH] add Adams-Bashforth First Order(same as Euler method) solver and test --- src/Numerics/Numerics.csproj | 1 + src/Numerics/OdeSolvers/AdamsBashforth.cs | 78 +++++++++++++++++++++++ src/UnitTests/OdeSolvers/OdeSolverTest.cs | 20 ++++++ 3 files changed, 99 insertions(+) create mode 100644 src/Numerics/OdeSolvers/AdamsBashforth.cs diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index eb71bc10..2306ffa1 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -112,6 +112,7 @@ + diff --git a/src/Numerics/OdeSolvers/AdamsBashforth.cs b/src/Numerics/OdeSolvers/AdamsBashforth.cs new file mode 100644 index 00000000..1efee322 --- /dev/null +++ b/src/Numerics/OdeSolvers/AdamsBashforth.cs @@ -0,0 +1,78 @@ +// +// 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. +// +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MathNet.Numerics.OdeSolvers +{ + public static class AdamsBashforth + { + + /// + /// First Order AB method(same as Forward Euler) + /// + /// Initial value + /// Start Time + /// End Time + /// Number of subintervals + /// ode model + /// + public static double[] FirstOrder(double y0, double start, double end, int N, Func f) + { + double dt = (end - start) / (N - 1); + double t = start; + double[] y = new double[N]; + y[0] = y0; + for (int i = 1; i < N; i++) + { + y[i] = y0 + dt * f(t, y0); + t += dt; + y0 = y[i]; + } + return y; + } + + public static double[] SecondOrder() + { + return null; + } + + public static double[] ThirdOrder() + { + return null; + } + + public static double[] FourthOrder() + { + return null; + } + } +} diff --git a/src/UnitTests/OdeSolvers/OdeSolverTest.cs b/src/UnitTests/OdeSolvers/OdeSolverTest.cs index d8b3542b..3d1b18ba 100644 --- a/src/UnitTests/OdeSolvers/OdeSolverTest.cs +++ b/src/UnitTests/OdeSolvers/OdeSolverTest.cs @@ -40,6 +40,26 @@ namespace MathNet.Numerics.UnitTests.OdeSolvers [TestFixture, Category("OdeSolver")] public class OdeSolverTest { + [Test] + public void AB1Test() + { + Func ode = (t, y) => t + 2 * y * t; + Func 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 = AdamsBashforth.FirstOrder(y0, 0, 2, Convert.ToInt32(Math.Pow(2, k + 8)), 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(1, ratio, 0.01);// Check error convergence order + } /// /// Runge-Kutta second order method for first order ODE. ///