Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

4th order Runge Kutta method for 2nd order ODE

  1. Jul 19, 2010 #1
    i have a bit of a problem with uderestanding how exactly we use RK4 method for solving 2nd order ODE.
    And last conversation with my proffesor only added up to my confiusion.
    Further more i couldn't find any example dealing with this problem if any1 could provide link explaining
    this problem along with solving example that would be very helpfull.

    so lets consider very basic 2nd order ODE
    x''(t) = Ax'(t) + Bx(t) + C
    i know that step i need to take is to make some assignments
    [tex]x(t) = x_1[/tex]
    [tex]x'(t) = x_2[/tex]
    which allows me to deal with two 1st order ODE
    [tex]x'(t) = x_2[/tex]
    [tex]x''(t) = A x_2 + B x_1 + C[/tex]
    but i'm confused how exact runge kutta steps will look like
    that's my idea totaly not shure if correct:
    [tex]k_1 = x_{2,n}[/tex]
    [tex]l_1 = f(t, x_{1,n}, x_{2,n}) = A x_{2,n} + B x_{1,n} + C[/tex]

    [tex]k_2 =x_{2,n} + \frac 1 2 l_1[/tex]
    [tex]l_2 = f(t + \frac 1 2 h, x_{1,n} + \frac 1 2 k_1, x_{2,n} + \frac 1 2 l_1)[/tex]

    [tex]k_3 =x_{2,n} + \frac 1 2 l_2[/tex]
    [tex]l_3 = f(t + \frac 1 2 h, x_{1,n} + \frac 1 2 k_2, x_{2,n} + \frac 1 2 l_2)[/tex]

    [tex]k_4 =x_{2,n} + l_3[/tex]
    [tex]l_4 = f(t + h, x_{1,n} + k_3, x_{2,n} + l_3)[/tex]

    [tex]x_{1,n+1} = x_{1,n} + \frac 1 6 h(k_1 + 2k_2 + 2k_3 + k_4)[/tex]
    [tex]x_{2,n+1} = x_{2,n} + \frac 1 6 h(l_1 + 2l_2 + 2l_3 + l_4)[/tex]

    I'm preticiulary interested how numerical integration looks like for first element x'(t) = x_2

    Proffesors idea was that it should be in all K_1 = , K_2 = .... there should be L_ instead of K_
    [tex]k_2 =x_{2,n} + \frac 1 2 k_1[/tex]
    and so on... ending with
    [tex]k_4 =x_{2,n} + k_3[/tex]
    [tex]l_4 = f(t + h, x_{1,n} + k_3, x_{2,n} + l_3)[/tex]

    Please help me and correct possible errors... Help me Physics Forum you are my only hope ;-)

    edit: since few hours passed with above 70 views
    some1 knowing the solution but maybe not having a time write it down just say if it's good or bad please.
    Last edited: Jul 19, 2010
  2. jcsd
  3. Jul 20, 2010 #2
    Hello Cypeq,
    I don't get what u mean by "x2,n"
    But..what u say seems correct(I too just recently fell into this stuff)

    The thing "I think" u r looking for .. I try to tell my way as follows..
    once u get the two 1st order ODE's...assign increment step with a variable
    like k with x1, l with x2
    if ur ODE involves both x1,x2 it'll be x1+k(or k/2) llly x2 + l ,etc
    Then calc successive coefficients...

    I hope that was helpful...
    sorry if it was irrelevant...:confused:
  4. Jul 20, 2010 #3
    I'm having a little trouble reading understanding the confusion, so sorry if I miss the mark.

    You should first separate the 2nd order equation into 2 equations, just like you have done. Then you apply your solution technique (in this case Runge-Kutta) to the highest order one (your second one), and solve for it (basically get the "acceleration"). Now use its value to solve the first one (your "velocity").

    Your velocity should be equal to its original value plus acceleration*step_size. Actually, I think you are leaving that (the accleration*step_size) out in your equation for x_prime -- maybe that is the problem?

    Then repeat, over and over and over.
  5. Jul 20, 2010 #4
    Thx for your answers

    @nanuath: x2,n is ofcourse value of x2 at n-th step meaning current velocity.
    if i made some confusion by my writing ki are solving value of x(t) = x1 which can be seen as possition
    and the li are solving first derrivative of possition x'(t) = x2 which can be seen as velocity.
    A, B, C are constant coefficients their value is meaningless here.

    @diggy: the confusion is about the way my prof. said the k1... k4 values should be obtained.
    i'm still not 100% if i'm aplying runge kutta here the right way if so tell me :P.

    I left the multiplication by step size [tex] h [/tex] to the end of equation where i get weighted results after all steps.
    I'm not shure if it changes anything.. I think you should add it (h) in final equation or in all four steps of algorythm.
    For example with added step size imo k2 would look like:
    [tex]k_2 =( x_{2,n} + \frac 1 2 l) h [/tex]
    but than i woudn't add h in x1,n+1 = ... part
    Last edited: Jul 20, 2010
  6. Jul 20, 2010 #5
    The only reason I wouldn't factor out the h early, is that you are using the h to determine where the other guesses (k2, k3,k4 )come from (they should all be ~h away). If you are making giant steps (without using h) for your estimates then they won't make any sense.

    Think of the steps as t+[tex]\Delta[/tex]t/2, x+[tex]\Delta[/tex]x/2 in the RK estimates.

    Otherwise I think your ordering looks right, i.e. get each RK estimate 1, get each RK estimate 2, ... step both values up using 4 estimates, start over.
  7. Jul 20, 2010 #6
    Read over your post again, and yes, every RK term should have the h in there like you do above, not at the end like you have in the original post.

    Edited: And yes, then you don't need another h in the n+1=n part. They are already included.
  8. Apr 18, 2012 #7
    Not so sure whether will there be anyone paying attention to this post as it's been for quite some time. But anyway here goes, hopefully I am able to get some assistance from PF.

    Apparently I came across the following steps which is the solution for the initial question in the post,

    x" = dv/dt = 10 - 4x - v = f(t, x, v)
    x'= dx/dt = v = g(t, x, v)
    subject to initial conditions x(0) = 1, x'(0) = 0
    Using a time step of h = 1 second

    We start with t = 0, x = 1, v = 0
    kf is the step for v while kg is the step for x
    kf1 = f(t, x, v) = f(0, 1, 0) = 6
    kg1 = g(t,x,v) = g(0, 1, 0) = 0

    kf2 = f(t+h/2, x+kg1*h/2, v+kf1*h/2)
    = f(0.5, 1, 3) = 3
    kg2 = g(t+h/2, x+kg1*h/2, v+kf1*h/2)
    = g(0.5, 1, 3) = 3

    kf3 = f(t+h/2, x+kg2*h/2, v+kf2*h/2)
    = f(0.5, 2.5, 1.5) = -1.5
    kg3 = g(t+h/2, x+kg2*h/2, v+kf2*h/2)
    = g(0.5, 2.5, 1.5) = 1.5

    kf4 = f(t+h, x+kg3*h, v+kf3*h)
    = f(1, 2.5, -1.5) = 1.5
    kg4 = g(t+h, x+kg3*h, v+kf3*h)
    = g(1, 2.5, -1.5) = -1.5

    At the next time value, t = 1
    x(1) = x(0) + h/6 * (kg1 + 2kg2 + 2kg3 + kg4)
    = 2.25
    x'(1) = y(0) + h/6 * (kf1 + 2kf2 + 2kf3 + kf4)
    = 1.75

    I have a question, that is if my equation is to be;
    x" = dv/dt = 10 - 4x - 0v

    would all my values in kg1, kg2, kg3 and kg4 be 0 (zero)? Or, do I only multiply the 0 with x' (velocity or 'v' in the above equation) only when I am calculating for x" ?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook