Solving Differential Equations with Python and Runga-Kutta

Click For Summary
SUMMARY

This discussion focuses on implementing the 4th order Runge-Kutta method in Python to solve systems of differential equations. The user seeks to create a function named rk4 that accepts another function, derivs, as an argument. Key insights include the ability to pass the return value of the derivs function to rk4, and the correct syntax for defining and calling such functions in Python.

PREREQUISITES
  • Understanding of Python programming language
  • Familiarity with differential equations
  • Knowledge of the Runge-Kutta method
  • Experience with function arguments and return values in Python
NEXT STEPS
  • Implement the rk4 function to solve a simple differential equation
  • Explore Python libraries such as NumPy for numerical computations
  • Research advanced Runge-Kutta methods for higher accuracy
  • Learn about visualizing solutions using Matplotlib
USEFUL FOR

Mathematicians, data scientists, and software developers interested in numerical methods for solving differential equations using Python.

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>)
 

Similar threads

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