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> /// Método Runge Kutta de Segunda ordem para equações diferenciais /// </summary> /// <param name="y0">Valor Inicial</param> /// <param name="start">Tempo Inicial</param> /// <param name="end">Tempo Final</param> /// <param name="dydx">Equação diferencial a ser calculada</param> /// <returns></returns> public double RungeKutta2(double y0, double start, double end, Func <double, double, double> dydx) { return(RungeKutta.SecondOrder(y0, start, end, 10, dydx)[9]); }