public static void Main() { double A = 0.0; double T = 0.0; ODE myODE = new ODE(A, T); myODE.InitialCondition = 1.0; myODE.Expiry = 1.0; // Getting values of coefficients double t = 0.5; Console.WriteLine("[a(t), f(t)] values: [{0}, {1}]", myODE.a(t), myODE.f(t)); // Calculate the FD scheme int N = 140; // Number of steps ExplicitEuler myFDM = new ExplicitEuler(N, myODE); myFDM.calculate(); double val = myFDM.Value; Console.WriteLine("fdm value: {0}", val); }
public static E RungeKutta4 <E>(ODE <E> ode, float time) where E : ODEData <E> { E initial = ode.Initial; E[] derivatives = new E[4]; derivatives[0] = ode.Evaluate(initial, initial, time, 0.0f); derivatives[1] = ode.Evaluate(initial, derivatives[0], time, 0.5f * Time.fixedDeltaTime); derivatives[2] = ode.Evaluate(initial, derivatives[1], time, 0.5f * Time.fixedDeltaTime); derivatives[3] = ode.Evaluate(initial, derivatives[2], time, Time.fixedDeltaTime); return(initial.calculateNextState(derivatives)); }
public static void RungeKutta4(ODE ode, double ds) { var s = ode.IndependentVariable; var q = ode.GetAllDependentVariables(); var dq1 = ode.GetRightHandSide(s, q, q, ds, 0.0); var dq2 = ode.GetRightHandSide(s + .5 * ds, q, dq1, ds, .5); var dq3 = ode.GetRightHandSide(s + .5 * ds, q, dq2, ds, .5); var dq4 = ode.GetRightHandSide(s + ds, q, dq3, ds, 1.0); ode.IndependentVariable = s + ds; for (var i = 0; i < ode.NumberOfEquationsToSolve; i++) { q[i] = q[i] + (dq1[i] + 2.0 * dq2[i] + 2.0 * dq3[i] + dq4[i]) / 6.0; ode.SetDependentVariable(i, q[i]); } }
protected double delta_T; // Step length public OneStepFDM(int NSteps, ODE ode) { this.NSteps = NSteps; this.ode = ode; vOld = ode.InitialCondition; vNew = vOld; mesh = new double[NSteps + 1]; mesh[0] = 0.0; mesh[NSteps] = ode.Expiry; delta_T = (mesh[NSteps] - mesh[0]) / NSteps; for (int n = 1; n < NSteps; n++) { mesh[n] = mesh[n - 1] + delta_T; Console.Write(", {0}", mesh[n]); } }
private void LyricSeter() { if (ODE.Ust != null) { string output = TB_CLyric.Text; if (output != "") { string[] newlyric = output.Split(new char[] { ' ' }); int i, j, end1 = newlyric.Length, end2 = ODE.Ust.Count; for (i = 0, j = 0; i < end2 && j < end1; i++) { if (ODE.Ust[i].CanEdit) { ODE.Ust[i].ClearlyLyric = newlyric[j]; j++; } } ODE.ReDraw(); } } }
// Fourth-order Runge-Kutta ODE solver. public static void RungeKutta4(ODE ode, double ds) //ds represent the increment (e.g., time) // Define some convenience variables to make the // code more readable { int j; int numEqns = ode.NumEqns; double s; double[] q; double[] dq1 = new double[numEqns]; double[] dq2 = new double[numEqns]; double[] dq3 = new double[numEqns]; double[] dq4 = new double[numEqns]; // Retrieve the current values of the dependent // and independent variables. s = ode.S; q = ode.GetAllQ(); // Compute the four Runge-Kutta steps, The return // value of getRightHandSide method is an array of // delta-q values for each of the four steps. dq1 = ode.GetRightHandSide(s, q, q, ds, 0.0); dq2 = ode.GetRightHandSide(s + 0.5 * ds, q, dq1, ds, 0.5); dq3 = ode.GetRightHandSide(s + 0.5 * ds, q, dq2, ds, 0.5); dq4 = ode.GetRightHandSide(s + ds, q, dq3, ds, 1.0); // Update the dependent and independent variable values // at the new dependent variable location and store the // values in the ODE object arrays. ode.S = s + ds; for (j = 0; j < numEqns; ++j) { q[j] = q[j] + (dq1[j] + 2.0 * dq2[j] + 2.0 * dq3[j] + dq4[j]) / 6.0; ode.SetQ(q[j], j); } return; }
// Fourth-order Runge-Kutta ODE solver. public static void RungeKutta4(ODE ode, double ds) { // 방정식 풀이를 위한 변수들 정의 int j; int numEqns = ode.NumEqns; double s; double[] q; double[] dq1 = new double[numEqns]; double[] dq2 = new double[numEqns]; double[] dq3 = new double[numEqns]; double[] dq4 = new double[numEqns]; // 현재 종속 변수와 독립 변수를 찾음 s = ode.S; //독립변수 q = ode.GetAllQ(); // 종속 변수 // 4개의 Runge-Kutta 단계를 계산합니다. // getRightHandSide 메서드의 반환 값은 각 4단계에 대한 델타-q 값의 배열입니다. // 0.5, 1.0 등은 가중치를 나타내고 있음 dq1 = ode.GetRightHandSide(s, q, q, ds, 0.0); dq2 = ode.GetRightHandSide(s + 0.5 * ds, q, dq1, ds, 0.5); dq3 = ode.GetRightHandSide(s + 0.5 * ds, q, dq2, ds, 0.5); dq4 = ode.GetRightHandSide(s + ds, q, dq3, ds, 1.0); // 새 종속 변수 위치에서 종속 변수 및 독립 변수 값을 업데이트하고 // 값을 ODE 객체 배열에 저장합니다. ode.S = s + ds; for (j = 0; j < numEqns; ++j) { q[j] = q[j] + (dq1[j] + 2.0 * dq2[j] + 2.0 * dq3[j] + dq4[j]) / 6.0; ode.SetQ(q[j], j); } return; }
public void SolveExplicit_Compare_With_Example() { //Arrange Equation func = delegate(double x, double[] y) { return(-y[0] + x * x + 2 * x + 2); }; ODE[] ode = new ODE[] { new ODE(func, 0, 2) }; double a = 0; double b = 1; int N = 4; double h = (b - a) / N; double[][] expected = new double[N + 1][]; double step = a; expected[0] = new double[] { 2 }; expected[1] = new double[] { 2 }; expected[2] = new double[] { 2.140625 }; expected[3] = new double[] { 2.41796875 }; expected[4] = new double[] { 2.8291015625 }; //Act double[][] actual = EulerMethod.SolveExplicit(ode, a, b, N); //Assert for (int i = 0; i <= N; i++) { for (int j = 0; j < ode.Length; j++) { Assert.AreEqual(expected[i][j], actual[i][j]); } } }
private void BTN_ZoomOut_Click(object sender, EventArgs e) { ODE.ZoomOut(); }
private static void test01() //****************************************************************************80 // // Purpose: // // TEST01 tests ODE. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 15 November 2012 // // Author: // // John Burkardt // { int i; int[] iwork = new int[5]; const int neqn = 2; const int step_num = 12; Console.WriteLine(""); Console.WriteLine("TEST01"); Console.WriteLine(" ODE solves a system of ordinary differential"); Console.WriteLine(" equations."); Console.WriteLine(""); Console.WriteLine(" T Y(1) Y(2)"); Console.WriteLine(""); const double abserr = 0.00001; const double relerr = 0.00001; int iflag = 1; double t = 0.0; double[] y = new double[neqn]; y[0] = 1.0; y[1] = 0.0; Console.WriteLine(" " + t.ToString(CultureInfo.InvariantCulture).PadLeft(8) + " " + y[0].ToString(CultureInfo.InvariantCulture).PadLeft(14) + " " + y[1].ToString(CultureInfo.InvariantCulture).PadLeft(14) + ""); double[] work = new double[100 + 21 * neqn]; for (i = 1; i <= step_num; i++) { double tout = i * 2.0 * Math.PI / step_num; ODE.ode(f01, neqn, y, ref t, tout, relerr, abserr, ref iflag, ref work, ref iwork); if (iflag != 2) { Console.WriteLine(""); Console.WriteLine("TEST01 - Fatal error!"); Console.WriteLine(" ODE returned IFLAG = " + iflag + ""); break; } Console.WriteLine(" " + t.ToString(CultureInfo.InvariantCulture).PadLeft(8) + " " + y[0].ToString(CultureInfo.InvariantCulture).PadLeft(14) + " " + y[1].ToString(CultureInfo.InvariantCulture).PadLeft(14) + ""); } }
public ExplicitEuler(int NSteps, ODE ode) : base(NSteps, ode) { }