Python rk4

  • #1
389
0

Main Question or Discussion Point

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?
 

Answers and Replies

  • #2
1,438
1,300
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>)
 

Related Threads on Python rk4

  • Last Post
Replies
1
Views
3K
Replies
11
Views
7K
  • Last Post
Replies
6
Views
1K
  • Last Post
Replies
1
Views
1K
Replies
13
Views
4K
Replies
3
Views
6K
Replies
7
Views
948
Replies
6
Views
4K
  • Last Post
Replies
4
Views
1K
  • Last Post
Replies
7
Views
881
Top