3
$\begingroup$

I am trying to use the Runge-Kutta 4 method to solve the nonlinear ODE, using the following Mathematica code:

s = NDSolve[{v'[t] + (v'[t])^3 - (t - 1) == 0, v[0] == 1}, {v}, {t, 0, 1}]

1) I don't know if RK4 is an option for NDSolve[] or not?

2) I need the values of $v$ at $v_k\in\{0.0, 0.1, \dots, 1.0\}$, that is with a step size of $h=0.1$.

$\endgroup$
6
$\begingroup$

The built-in "ExplicitRungeKutta" method of order 4 is an "embedded" method (i.e., with an embedded error estimation method). Here is the classical method (with code cribbed from this answer by J.M.):

ClassicalRungeKuttaCoefficients[4, prec_] := 
  With[{amat = {{1/2}, {0, 1/2}, {0, 0, 1}}, 
    bvec = {1/6, 1/3, 1/3, 1/6}, cvec = {1/2, 1/2, 1}}, 
   N[{amat, bvec, cvec}, prec]];

Here is a comparison with the built-in method:

NDSolve`EmbeddedExplicitRungeKuttaCoefficients[4, MachinePrecision]
ClassicalRungeKuttaCoefficients[4, MachinePrecision]
(*
{{{0.4`},
  {-0.15`, 0.75`},
  {0.4318181818181818`, -0.3409090909090909`, 0.9090909090909091`},
  {0.1527777777777778`, 0.3472222222222222`, 0.3472222222222222`, 0.1527777777777778`}},
 {0.1527777777777778`, 0.3472222222222222`, 0.3472222222222222`, 0.1527777777777778`, 0.`},
 {0.4`, 0.6`, 1.`, 1.`},
 {0.013269665336144196`, -0.06634832668072098`, 0.06634832668072098`,
  0.14596631869758617`, -0.15923598403373035`}}

{{{0.5`},
  {0.`, 0.5`},
  {0.`, 0.`, 1.`}},
 {0.16666666666666666`, 0.3333333333333333`, 0.3333333333333333`, 0.16666666666666666`},
 {0.5`, 0.5`, 1.`}}
*)

Here's a way to get the solution steps, based on some of the links I put in a comment above:

vf = v /. 
   First@NDSolve[{v'[t] + (v'[t])^3 - (t - 1) == 0, v[0] == 1}, {v}, {t, 0, 1}, 
     Method -> {"FixedStep", 
       Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 4, 
         "Coefficients" -> ClassicalRungeKuttaCoefficients}}, 
     StartingStepSize -> 1/10];

Transpose@Flatten[vf[{"Coordinates", {"ValuesOnGrid"}}], 1]
TableForm[%, TableHeadings -> {Range[0, 10], {t, v}}]
(*
  {{0., 1.}, {0.1, 0.933905}, {0.2, 0.872309}, {0.3, 0.815591}, {0.4, 0.764202},
   {0.5, 0.718679}, {0.6, 0.679664}, {0.7, 0.647912}, {0.8, 0.624276},
   {0.9, 0.609622}, {1., 0.604647}}
*)

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.