Runge-Kutta using Numerical Recipes

  • Context: Fortran 
  • Thread starter Thread starter Vrbic
  • Start date Start date
  • Tags Tags
    Numerical Runge-kutta
Click For Summary

Discussion Overview

The discussion revolves around implementing the Runge-Kutta method for solving a system of ordinary differential equations (ODEs) using code from "Numerical Recipes." Participants seek clarification on how to input specific differential equations into the provided framework and how to structure their code accordingly.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant expresses confusion about how to implement differential equations in the Runge-Kutta method, specifically regarding the input of initial conditions and the structure of the code.
  • Another participant suggests posting the code for better assistance.
  • A participant mentions they have a system of ODEs in mind and seeks guidance on how to input the functions for these equations.
  • One participant explains that a subroutine named 'derivs' should be created to calculate the right-hand sides of the equations for the derivatives, detailing how to structure the dependent variables.
  • Another participant confirms understanding after the explanation about the 'derivs' subroutine, indicating that they initially misunderstood its purpose.

Areas of Agreement / Disagreement

Participants generally agree on the need to define a 'derivs' subroutine for calculating derivatives, but there is no consensus on the best way to implement the overall system of ODEs, as some participants are still seeking clarification.

Contextual Notes

Some participants express constraints in their ability to provide further assistance, and there are references to specific pages in the source material that may contain relevant information.

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   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

Similar threads

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