of differential equations with the method of Runge-Kutta.
The parameter system-derivative is a function that takes a system
state (a vector of values for the state variables y1, ..., yn)
and produces a system derivative (the values y1/, ...,
yn/). The parameter initial-state provides an initial
system state, and h is an initial guess for the length of the
integration step.
The value returned by integrate-system is an infinite stream of
system states.
Runge-Kutta-4 takes a function, f, that produces a
system derivative from a system state. Runge-Kutta-4
produces a function that takes a system state and
produces a new system state.
(define runge-kutta-4
(lambda (f h)
(let ((*h (scale-vector h))
(*2 (scale-vector 2))
(*1/2 (scale-vector (/ 1 2)))
(*1/6 (scale-vector (/ 1 6))))
(lambda (y)
;; y is a system state
(let* ((k0 (*h (f y)))
(k1 (*h (f (add-vectors y (*1/2 k0)))))
(k2 (*h (f (add-vectors y (*1/2 k1)))))
(k3 (*h (f (add-vectors y k2)))))
(add-vectors y
(*1/6 (add-vectors k0
(*2 k1)
(*2 k2)
k3))))))))
Infinite streams are implemented as pairs whose car holds the first
element of the stream and whose cdr holds a promise to deliver the rest
of the stream.
(define head car)
(define tail
(lambda (stream) (force (cdr stream))))
The following illustrates the use of integrate-system in
integrating the system
which models a damped oscillator.
(define damped-oscillator
(lambda (R L C)
(lambda (state)
(let ((Vc (vector-ref state 0))
(Il (vector-ref state 1)))
(vector (- 0 (+ (/ Vc (* R C)) (/ Il C)))
(/ Vc L))))))