Python Solving Differential Equations with Python and Runga-Kutta

Click For Summary
The discussion focuses on writing a Python program to solve systems of differential equations using the 4th order Runge-Kutta method. The goal is to create a function, rk4, that accepts another function, derivs, as an argument to handle systems of arbitrary size. There is clarification on the syntax for passing functions as arguments, emphasizing that while a function can be passed, it is more common to pass the results of the function. The suggested approach involves defining rk4 to accept the output of derivs along with other necessary arguments. This method allows for flexibility in solving various differential equation systems. Additionally, participants highlight the importance of understanding function arguments and returns when implementing this solution.
Cincinnatus
Messages
389
Reaction score
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
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>)
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
Replies
55
Views
6K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 43 ·
2
Replies
43
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K