Runge-Kutta using Numerical Recipes

  • Context: Fortran 
  • Thread starter Thread starter Vrbic
  • Start date Start date
  • Tags Tags
    Numerical Runge-kutta
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
8 replies · 3K views
Vrbic
Messages
400
Reaction score
18
Hello,
I try to solve a system of ODE's by Runge-Kutta method from here: https://websites.pmc.ucsc.edu/~fnimmo/eart290c_17/NumericalRecipesinF77.pdf, page 704 in the book (not pdf). Bellow is also a code. In function rk4dumb I don't understand how are implemented differential equations. Input are just initial conditions, steps, amount of equations and function for differentiation. How may I input some real problem there? I track subroutine rk4 it has to be in first input, but the value (v) is taken as a new variable in this subroutine. So I'm a bit confused. Please advise me :-)
Thank you all.
 
Physics news on Phys.org
First I would advise you to post the code to which you are referring ;)

[ use the insert (+) -> code tool, for best results]
 
lewando said:
First I would advise you to post the code to which you are referring ;)

[ use the insert (+) -> code tool, for best results]
The code is in pdf link which I add (pages 706-708). I don't understand how to use it. I rewrite it as subroutines and I have two functions on the paper and think how to implement it. I know Runge-Kutta but mostly there isn't derivative but function f(x,y) (dy/dx=f(y,x)). Now I'm lost.
 
lewando said:
Ahh-- nevermind, I see the code now in the link. Do you have a particular ODE in mind?
I would like to prepare generally. I have system let's say: dy/dx=f(x,y,z) and dz/dx=g(x,y,z). Where I input functions f and g?
 
I am constrained presently. See if this helps for now: https://epublications.bond.edu.au/cgi/viewcontent.cgi?article=1130&context=ejsie
 
Temporarlly out of my constraint (but not for long)-- so the basic concept is running the rk4 with dy/dx to get a Δy. Then do rk4 with dz/dx to get Δz. Now you have a new y and a new z. Repeat the process.
 
Vrbic said:
I have system let's say: dy/dx=f(x,y,z) and dz/dx=g(x,y,z). Where I input functions f and g?
See pages 706-707.

You need to write a subroutine named 'derivs' that calculates the right-hand sides of your equations for the derivatives. First, to make things less confusing, re-name your dependent variables so

dy/dx = f(x,y,z) ---> dy1/dx = f(x,y1,y2)
dz/dx = g(x,y,z) ---> dy2/dx = g(x,y1,y2)

Your subroutine 'derivs(x,y,dydx)' receives the value of x and the two y's at which the derivatives are calculated. Note that y and dydx are both arrays, with length 2. y(1) = y1 and y(2) = y2. The subroutine must calculate dydx(1) = dy1/dx = f(x,y1,y2) and dydx(2) = dy2/dx = g(x,y1,y2), which will be returned to the subroutine 'rk4'.
 
  • Like
Likes   Reactions: Vrbic and DrClaude
jtbell said:
See pages 706-707.

You need to write a subroutine named 'derivs' that calculates the right-hand sides of your equations for the derivatives. First, to make things less confusing, re-name your dependent variables so

dy/dx = f(x,y,z) ---> dy1/dx = f(x,y1,y2)
dz/dx = g(x,y,z) ---> dy2/dx = g(x,y1,y2)

Your subroutine 'derivs(x,y,dydx)' receives the value of x and the two y's at which the derivatives are calculated. Note that y and dydx are both arrays, with length 2. y(1) = y1 and y(2) = y2. The subroutine must calculate dydx(1) = dy1/dx = f(x,y1,y2) and dydx(2) = dy2/dx = g(x,y1,y2), which will be returned to the subroutine 'rk4'.
Thaaaaaank you very much, now I understand. I thought that "derivs" is an ordinary function for general derivatives.
Thank you all, solved.
 
  • Like
Likes   Reactions: jtbell