Fortran Runge-Kutta using Numerical Recipes

Click For Summary
SUMMARY

The forum discussion focuses on implementing the Runge-Kutta method for solving systems of ordinary differential equations (ODEs) using the Numerical Recipes in Fortran 77. The user seeks clarification on how to input specific functions for differentiation into the provided code, particularly in the context of a system defined by dy/dx = f(x,y,z) and dz/dx = g(x,y,z). The solution involves creating a subroutine named 'derivs' that calculates the derivatives based on the dependent variables, which are passed as arrays to the Runge-Kutta subroutine.

PREREQUISITES
  • Understanding of ordinary differential equations (ODEs)
  • Familiarity with the Runge-Kutta method
  • Basic knowledge of Fortran programming
  • Ability to work with subroutines and arrays in programming
NEXT STEPS
  • Study the implementation of the Runge-Kutta method in Fortran 77
  • Learn how to define and use subroutines in Fortran
  • Explore numerical methods for solving systems of ODEs
  • Review examples of derivative functions in computational programming
USEFUL FOR

Mathematicians, physicists, and engineers working with numerical simulations, as well as programmers implementing numerical methods for solving ODEs.

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 ·
Replies
2
Views
3K
Replies
13
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 65 ·
3
Replies
65
Views
8K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K