Solving Differential Equations with Python and Runga-Kutta

In summary, the conversation is about writing a program in python to solve a system of differential equations using the 4th order Runga-Kutta method. The goal is for the program to be able to solve systems of arbitrary size, which would require writing a function (rk4) that takes another function (derivs) as an argument. The syntax for this is to assign what the derivs function returns into a variable and then call the rk4 function with that variable as an argument. The individual is unsure how to go about this and asks for any additional information before attempting to write the program.
  • #1
Cincinnatus
389
0
I'm trying to write a program in python to solve a system of differential equations using the 4th order Runga-Kutta method.

I would ideally like the program to be able to solve systems of arbitrary size. To do this, I believe I will need to write a function (rk4) that would take as argument another function (the derivs function).

I've never written anything like that before though, I'm not exactly sure how to go about it...

Could I write something like:

def rk4 (derivs,whatever else) :
.
.
.
whatever=derivs(arguments)
.
.
.
return array of solutions

Is there any special syntax involved in having one function take another as an argument like this?

Is there anything else I should know before I try writing this?
 
Technology news on Phys.org
  • #2
You cannot take another function as an argument, but you can certainly take the what the function returns as an argument. Say you assign what your function derivs returns into a variable asuch that the definition of your rk4 function will look something like this: def rk4 (a, <argument_list>). While calling the function rk4, call it like this:

rk4(derivs(<argument_list>), <other_arguments>)
 

FAQ: Solving Differential Equations with Python and Runga-Kutta

1. What are differential equations?

Differential equations are mathematical equations that describe how a quantity changes over time. They involve the rate of change of a variable, and can be used to model a variety of phenomena in physics, chemistry, biology, economics, and other fields.

2. What is Runga-Kutta?

Runga-Kutta is a numerical method for solving differential equations. It involves breaking down the problem into smaller steps and using approximations to calculate the solution at each step. It is commonly used because it is accurate, efficient, and can handle a wide range of differential equations.

3. Why use Python and Runga-Kutta to solve differential equations?

Python is a popular programming language that has many libraries and packages for scientific computing, making it a powerful tool for solving differential equations. Runga-Kutta is a reliable and versatile numerical method that can handle a wide range of equations. Together, they provide a flexible and efficient way to solve differential equations.

4. What are the advantages of using Runga-Kutta over other numerical methods?

Runga-Kutta is a higher-order method, meaning it can achieve a higher degree of accuracy compared to other numerical methods. It is also more stable and can handle non-linear equations, making it a better choice for solving complex differential equations.

5. Can Runga-Kutta be used for any type of differential equation?

Runga-Kutta can be used to solve a wide range of differential equations, including both ordinary and partial differential equations. However, the choice of which Runga-Kutta method to use may depend on the specific characteristics of the equation, such as its order and stiffness.

Similar threads

Replies
9
Views
2K
Replies
1
Views
1K
Replies
3
Views
671
Replies
1
Views
904
Replies
3
Views
1K
Replies
4
Views
5K
Back
Top