Problems with Runge-Kutta method

  • Context: Graduate 
  • Thread starter Thread starter matteo86bo
  • Start date Start date
  • Tags Tags
    Method Runge-kutta
Click For Summary

Discussion Overview

The discussion revolves around the implementation of the Runge-Kutta method, specifically the midpoint method, for solving a differential equation related to the variable beta as a function of radius and time. Participants are examining the correctness of the algorithm and the code provided for evaluating the equation.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant presents a code snippet for the Runge-Kutta II method but expresses uncertainty about its correctness.
  • Another participant critiques the code, suggesting that the evaluation of beta when computing k2 is incorrect due to the use of grid points.
  • A proposed correction to the code is offered, indicating that beta should be evaluated at the midpoint of the time step.
  • A further clarification is provided regarding the formulation of the ODE and the proper application of the midpoint method, including the correct handling of the function f.
  • One participant acknowledges a mistake in the grid and the sign in the equation, indicating a lack of concern about the sign error.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correctness of the initial code implementation, and there are differing views on the proper evaluation of beta in the context of the midpoint method.

Contextual Notes

There are unresolved issues regarding the proper handling of grid points and the evaluation of the function f in the context of the Runge-Kutta method.

matteo86bo
Messages
56
Reaction score
0
Hi everyone,
I've a little problem with my algorithm.
I have two grids: one for time and the other one for the radius.
I need to evaluate this equation:

[tex]\frac{d\beta(r,t)}{dt}=A\beta(r,t)^N[/tex]

I try solving this with a simple Runge Kutta II method, but I'm not convinced at all that this works properly.

Let's check my code:

k1 = deltaT*(-A*beta(i,j-1)**N
k2 = deltaT*(-A*(beta(i,j-1)+k1/2.)**N)

beta(i,j) = beta(i,j-1) + k2

What do you think about that?
This is not the simplest case, just the one you can read in Numerical Recipes ...

P.S.:

beta(i,j) mean beta at r_i and t_j
so i and j referes to radius and time grid respectively
 
Physics news on Phys.org
I'm having difficulties making sense of your code. It looks like you're trying to the midpoint method, but in that case the evaluation of beta when computing k2 is all wrong. When you take the half-step you will, in general, not end up on a grid vertex, so you can't use "beta(i,j-1)" there.
 
Should it look like that?

k2 = deltaT*(-A*( (beta(i,j-1)+beta(i,j))/2. +k1/2.)**N)

i know i need to evaluate beta in t+deltaT/2.. can this work?
 
Like I said I got a bit confused by your notation. Let's step back a bit. The ODE is given by
[tex]\frac{dy(t)}{dt} = f(y(t), t)[/tex]

In your case we have [itex]y(t) = \beta(r, t)[/itex] and [itex]f(y(t), t) = Ay(t)^N[/itex]. Note the use of y(t) in the right hand of f.

The midpoint method is then

[tex]\tilde{y}_{j+1} = y_j + \frac{h}{2}f(y_j, t_j)[/tex]
[tex]y_{j+1} = y_j + hf(\tilde{y}_{j+1}, t_j + \frac{h}{2})[/tex]

where h is the step length ([itex]\Delta t[/itex]). Inserting the expression for f we get

[tex]\tilde{y}_{j+1} = y_j + \frac{h}{2}f(y_j, t_j) = y_j + \frac{h}{2}Ay_j^N[/tex]
[tex]y_{j+1} = y_j + hf(\tilde{y}_{j+1}, t_j + \frac{h}{2}) = y_j + hA\tilde{y}_{j+1}^N[/tex]

Here y_j = beta(i,j-1) using your notation. So in the second line we don't access the original beta, instead we use the midpoint we found. Also note that you seem to have flipped signs on A between the ODE and the code, while I haven't (not sure which sign is the correct one).

Hope this helps, and I hope I didn't mess up somewhere :)
 
thank you man!
I messed up with the grid ... the sign is my fault, but it doesn't matter ...
 
Glad it helped :)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 65 ·
3
Replies
65
Views
9K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K