Fortran Understanding Subroutine Derivs in Runge-Kutta 4: Solving Numerical Recipes

  • Thread starter Thread starter Vrbic
  • Start date Start date
AI Thread Summary
The discussion revolves around understanding the implementation of the Runge-Kutta method, specifically the role of the "derivs" subroutine in calculating derivatives. Key points include the clarification that "derivs" computes all derivatives in a single call, eliminating the need for looping over dependent variables, which may differ. The user must define how "derivs" recognizes the size of the "yt" vector, as it is typically hard-coded for specific implementations of coupled ordinary differential equations (ODEs). The conversation emphasizes that the derivative is not derived from "yt" directly, but rather from the differential equation governing the system, which is implemented in the "derivs" subroutine. An example is provided where the subroutine calculates the derivative based on the function f(x, y), reinforcing the idea that the Runge-Kutta method is designed to solve systems of ODEs rather than deriving individual points. The discussion concludes with an acknowledgment of the complexities involved in seemingly simple calculations.
Vrbic
Messages
400
Reaction score
18
I have a problem to understand some parts of numerical recipe for runge-kutta. I can made it by myself but I am very curious how they do that:
upload_2017-10-17_21-52-37.png


Here are my questions:
1) Why "call derivs" is not in DO routine? What is only "yt"? I would expect "yt(i)".
2) How does subroutine "derivs" look like?? How can I make derivative of "yt"? It is one value. I don't know value in further points so how can I derive it?

Thank you for help.
 
Technology news on Phys.org
Those little do loops are over the elements in the y vector (the dependent variables), they are not over the steps in the independent variable (x).

derivs computes all the derivatives in the vector in a single call so you do not need to loop over it. The derivatives of the different dependent variables might be different, so looping over them would not make sense.
 
Dr. Courtney said:
derivs computes all the derivatives in the vector in a single call so you do not need to loop over it. The derivatives of the different dependent variables might be different, so looping over them would not make sense.
Ok, so the loop is inside 'derivs'. Yes? And how 'derivs' find out what is dimension of yt? There is not a input for it.
 
Dr. Courtney said:
Those little do loops are over the elements in the y vector (the dependent variables), they are not over the steps in the independent variable (x).
I still I don't get it. In practice one 1st order ode ##\frac{dy}{dx}=f(x)##, initial value ##{y(x0)=y0}## step ##h##:
0) ##x=x0,y_1=y0## - y is array and subscript means order in array
1) ##yt_1=y_1+\frac{h}{2}f(x0)## - First step in the book
2) call derivs(x+h,yt,dyt) - Second step ... how can I produce derivative from one point ##y_1## ? I believe it is right but I'm missing something essential.
 
Vrbic said:
Ok, so the loop is inside 'derivs'. Yes? And how 'derivs' find out what is dimension of yt? There is not a input for it.
Read the comment in the subroutine: "the user supplies the subroutine derivs". It is up to the user to figure out how derivs is to know the size of yt. (Note that since derivs is a specific implementation of a system of coupled ODEs, the size of yt is generally hard-coded.)

Vrbic said:
0) ##x=x0,y_1=y0## - y is array and subscript means order in array
No, the subscript is the step. ##y_n## is a vector at point ##x_n = x_0 + nh##.
 
Vrbic said:
I know it is not general recipe, but what is else in this ( https://www.nsc.liu.se/~boein/f77to90/rk.html ) procedure? I mean accuracy, computing time, ...?
It simply looks like a pseudo-code representation of RK4.
 
DrClaude said:
Read the comment in the subroutine: "the user supplies the subroutine derivs". It is up to the user to figure out how derivs is to know the size of yt. (Note that since derivs is a specific implementation of a system of coupled ODEs, the size of yt is generally hard-coded.)No, the subscript is the step. ##y_n## is a vector at point ##x_n = x_0 + nh##.
Yes I know, I meant, it is my definition of subscript for this particular case (I suppose one ODE so I changed meanig). But look at the question. "yt" is single point and I have to produce derivative. There is my main problem.

DrClaude said:
It simply looks like a pseudo-code representation of RK4.
What do you mean by pseudo-code? Is it wrong or what is different?

And thank you for you time and response.
 
Vrbic said:
What do you mean by pseudo-code?
Pseudocode is an informal notation that looks like a simplified version of a programming language. It's used for describing an algorithm in a way that doesn't require the reader to know any specific language such as C, FORTRAN, etc.

https://en.wikipedia.org/wiki/Pseudocode
 
  • #10
jtbell said:
Pseudocode is an informal notation that looks like a simplified version of a programming language. It's used for describing an algorithm in a way that doesn't require the reader to know any specific language such as C, FORTRAN, etc.

https://en.wikipedia.org/wiki/Pseudocode
Ah...sorry I didn't know it is general term. I would google it. But I can write this formula to my code. And it works. Does it work same?
 
  • #11
Vrbic said:
But look at the question. "yt" is single point and I have to produce derivative. There is my main problem.
You don't derive yt, you have a differential equation that tells you what the derivative looks like.

I think we need to take a step back. The point of a method like Runge-Kutta is to solve a set of coupled ordinary differential equations. The precise equations to be solved depend on the problem at hand. The point of the subroutine derivs is to implement these equations.
 
  • #12
DrClaude said:
You don't derive yt, you have a differential equation that tells you what the derivative looks like.

I think we need to take a step back. The point of a method like Runge-Kutta is to solve a set of coupled ordinary differential equations. The precise equations to be solved depend on the problem at hand. The point of the subroutine derivs is to implement these equations.
Ok. Could you be precise and apply it to practical example. Let's say: ##\frac{dy}{dx}=y'=f(x,y)## with initial value ##y(x0)=y0##. What is in calculation of first point next to ##x0## the ##dyt##?
 
  • #13
Vrbic said:
Let's say: ##\frac{dy}{dx}=y'=f(x,y)##
In that case, the subroutine derivs is simply an implementation of ##f(x,y)##.

##\texttt{call derivs(xh,yt,dyt)}##
Set ##x = \texttt{xh}## and ##y = \texttt{yt}## and return ##\texttt{dyt} = f(x,y)##.
 
  • #14
DrClaude said:
In that case, the subroutine derivs is simply an implementation of ##f(x,y)##.

##\texttt{call derivs(xh,yt,dyt)}##
Set ##x = \texttt{xh}## and ##y = \texttt{yt}## and return ##\texttt{dyt} = f(x,y)##.
The easiest things are mostly the hardest one :-)
Thank you for all and patience.
 
  • Like
Likes DrClaude

Similar threads

Replies
8
Views
4K
Replies
2
Views
3K
Replies
8
Views
3K
Replies
1
Views
3K
Replies
15
Views
3K
Replies
59
Views
11K
Replies
7
Views
2K
Replies
4
Views
8K
Back
Top