title: 2 Dimension Runge-Kutta Method on Mathematica 8
body: <p>I have a problem while programing in Mathematica 8, here is my code:</p>
  
  <pre><code>f[t_, y_] := {y, y};
  
  RungeKutta3[a_, b_, Alpha_, n_, f_] := 
    Module[{h, j, k1, k2, k3}, 
      h = (b - a)/n; 
      Y = T = Table[0, {100 + 1}]; 
      Y[[1]] = Alpha; 
      T[[1]] = a; 
      For[j = 1, j &lt;= n, ++j, 
        k1 = f[T[[j]], Y[[j]]]; 
        k2 = f[T[[j]] + h/2, Y[[j]] + k1*h/2]; 
        k3 = f[T[[j]] + h, Y[[j]] + (-k1 + 2 k2)h]; 
        Y[[j + 1]] = Y[[j]] + h/6(k1 + 4 k2 + k3); 
        (* Print[j, "-----&gt;", Y[[j]]];*) 
        T[[j + 1]] = T[[j]] + h;
     ];]; 
  
  RungeKutta3[0., 1., {300., 500}, 2, f];
  </code></pre>
  
  <p>The thing is, I'm trying to implement a Runge-Kutta method. And I was successful actually, but only with a function <code>f[x_]</code> that had 1 dimension. This code is for 2 dimensions, but it simply doesn't work and I don't know why. Here is an example for a code with 1 dimension only (notice that I have to change the first line to define the function and the last line, when I call "RungeKutta3").</p>
  
  <pre><code>f[t_, y_] := y;
  
  RungeKutta3[a_, b_, Alpha_, n_, f_] := 
    Module[{h, j, k1, k2, k3}, 
      h = (b - a)/n;  
      Y = T = Table[0, {100 + 1}];  
      Y[[1]] = Alpha; 
      T[[1]] = a;  
      For[j = 1, j &lt;= n, ++j,   
        k1 = f[T[[j]], Y[[j]]];   
        k2 = f[T[[j]] + h/2, Y[[j]] + k1*h/2];   
        k3 = f[T[[j]] + h, Y[[j]] + (-k1 + 2 k2)*h];   
        Y[[j + 1]] = Y[[j]] + h/6*(k1 + 4 k2 + k3);   
        (* Print[j, "-----&gt;", Y[[j]]];*)     
        T[[j + 1]] = T[[j]] + h;
    ];]; 
  
  RungeKutta3[0., 1., 300., 100, f];
  </code></pre>
  
  <p>To sum up, how do I implemented the Runge-Kutta method for a function with 2 dimensions??</p>
  
  <p>If you could help me out I would be grateful.</p>
  
  <p>Thanks in advance!</p>
  
  <p>PS: the Runge-Kutta method is order 3</p>
  
  <p><strong>----------------------</strong></p>
  
  <p><strong>Problem solved!</strong> Check the code, if anybody needs help with anything, just ask!</p>
  
  <pre><code>f[t_, y1_, y2_] := 3 t*y2 + Log[y1] + 4 y1 - 2 t^2 * y1 - Log[t^2 + 1] - t^2;
  F[t_, {y1_, y2_}] := {y2, f[t, y1, y2]}; 
  RungeKutta3[a_, b_, [Alpha]_, n_, f_] :=
   Module[{h, j, k1, k2, k3, Y, T, R},
    h = (b - a)/n;
    Y = T = Table[0, {n + 1}];
    Y[[1]] = [Alpha]; T[[1]] = a;
    For[j = 1, j &lt;= n, ++j,
     k1 = f[T[[j]], Y[[j]]];
     k2 = f[T[[j]] + h/2, Y[[j]] + k1*h/2];
     k3 = f[T[[j]] + h, Y[[j]] + (-k1 + 2 k2)*h];
     Y[[j + 1]] = Y[[j]] + h/6*(k1 + 4 k2 + k3);
     T[[j + 1]] = T[[j]] + h;
     ];
    R = Table[0, {n + 1}]; 
    For[j = 1, j &lt;= n + 1, j++, R[[j]] = Y[[j]][[1]]];
    Print[ListPlot[Transpose[{T, R}]]]
    ];
  
  RungeKutta3[0., 1, {1., 0.}, 1000, F];
  </code></pre>
  
  <p>I know basically have a mathematica program that can solve ANY 2nd order equation! Through Runge-Kutta method. just insert your function on</p>
  
  <pre><code>f[t_, y1_, y2_]:= [Insert your function here]
  </code></pre>
  
  <p>where t is the independent value, y1 is the function itself y(t), y2 is y'(t).
  Call the function through:</p>
  
  <pre><code>RungeKutta3[a, b, [Alpha], n, F];
  </code></pre>
  
  <p>where <strong>a</strong> is the initial "t" value, <strong>b</strong> the final "t" value, <strong>[Alpha]</strong> the initial value of your function and the first derivative (given in the form {y1(a),y2(a0)}), <strong>n</strong> the number of points equally spaced you want to represent. <strong>F</strong> is the function you have to insert despite of the function you give to <strong>f</strong></p>
  
  <p>Any questions feel free to ask!!
  PS: The Runge-Kutta problem solves differential equations with problems of initial values, i used this program as a base to solve a problem of boundary values, if you want it just text me!</p>