Fortran Runge-Kutta using Numerical Recipes

AI Thread Summary
The discussion centers on implementing the Runge-Kutta method for solving a system of ordinary differential equations (ODEs). The user initially expresses confusion about how to input specific functions for differentiation into the provided code from a numerical recipes resource. They clarify their system of equations, dy/dx = f(x,y,z) and dz/dx = g(x,y,z), and seek guidance on structuring their code. Key advice includes creating a subroutine named 'derivs' to calculate the derivatives based on the dependent variables. The user is instructed to rename their variables for clarity and to ensure that the 'derivs' subroutine receives the necessary inputs to compute the derivatives accurately. This involves using arrays to manage the dependent variables and their derivatives. The user ultimately resolves their confusion regarding the purpose of the 'derivs' subroutine, realizing it is essential for calculating the right-hand sides of their equations.
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.
 
Technology 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]
 
Ahh-- nevermind, I see the code now in the link. Do you have a particular system of ODEs in mind?
 
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 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 jtbell

Similar threads

Replies
2
Views
3K
Replies
15
Views
3K
Replies
7
Views
2K
Replies
8
Views
4K
Replies
6
Views
3K
Replies
5
Views
2K
Replies
4
Views
1K
Back
Top