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

  • Fortran
  • Thread starter Vrbic
  • Start date
In summary: The point of a method like Runge-Kutta is to solve a set of coupled ordinary differential equations.I see, so derivs is not actually solving the equation, it is just providing a representation of it?The point of the subroutine derivs is to implement these... equations.
  • #1
Vrbic
407
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #6
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##.
 
  • #7
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.
 
  • #8
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.
 
  • #9
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

1. What is the Runge-Kutta 4 method?

The Runge-Kutta 4 method is a numerical method used to solve ordinary differential equations. It is a popular and widely used method due to its accuracy and efficiency.

2. How does the Runge-Kutta 4 method work?

The Runge-Kutta 4 method works by using a series of intermediate values to better approximate the solution of an ordinary differential equation at each step. It involves calculating four different slopes and combining them to get the final solution.

3. What are the advantages of using Runge-Kutta 4 method?

One of the main advantages of using the Runge-Kutta 4 method is its accuracy. It is a fourth-order method, meaning that it can provide a more precise solution compared to lower-order methods. Additionally, it is relatively easy to implement and can handle a wide range of differential equations.

4. What are the limitations of the Runge-Kutta 4 method?

The Runge-Kutta 4 method may not be suitable for solving stiff equations, which are equations with rapidly changing solutions. It also requires a relatively small step size to maintain accuracy, which can make it computationally expensive for long-term simulations.

5. How is the Runge-Kutta 4 method different from other numerical methods?

The Runge-Kutta 4 method differs from other numerical methods, such as Euler's method or the midpoint method, in its higher-order accuracy and use of multiple intermediate values. This allows for a more accurate approximation of the solution and makes it a popular choice for many scientific applications.

Similar threads

  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Differential Equations
2
Replies
40
Views
546
  • Differential Equations
Replies
3
Views
379
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
4
Views
6K
Back
Top