Runge-Kutta method: Difference between revisions

adding common lisp
m (→‎{{header|jq}}: the dy solution is much simpler, so put it first)
(adding common lisp)
Line 208:
10 676 -7.54191e-08
</pre>
 
=={{header|Common Lisp}}==
 
<lang lisp>(defun runge-kutta (f x y x-end n)
(let ((h (float (/ (- x-end x) n) 1d0))
k1 k2 k3 k4)
(setf x (float x 1d0)
y (float y 1d0))
(cons (cons x y)
(loop for i below n do
(setf k1 (* h (funcall f x y))
k2 (* h (funcall f (+ x (* 0.5d0 h)) (+ y (* 0.5d0 k1))))
k3 (* h (funcall f (+ x (* 0.5d0 h)) (+ y (* 0.5d0 k2))))
k4 (* h (funcall f (+ x h) (+ y k3)))
x (+ x h)
y (+ y (/ (+ k1 k2 k2 k3 k3 k4) 6)))
collect (cons x y)))))
 
(let ((sol (runge-kutta (lambda (x y) (* x (sqrt y))) 0 1 10 101)))
(loop for n from 0
for (x . y) in sol
when (zerop (mod n 10))
collect (list x y (- y (/ (expt (+ 4 (* x x)) 2) 16)))))
 
((0.0d0 1.0d0 0.0d0)
(0.99009900990099d0 1.5502091594068796d0 -1.3682675659154597d-7)
(1.9801980198019808d0 3.921571584328101d0 -8.589685593918261d-7)
(2.9702970297029716d0 10.276292494568686d0 -2.721706728436857d-6)
(3.9603960396039626d0 24.218048062815786d0 -5.844164682855535d-6)
(4.950495049504953d0 50.79198516734904d0 -1.0156597575416981d-5)
(5.940594059405944d0 96.48472119934078d0 -1.559309210108495d-5)
(6.930693069306935d0 169.2243440410163d0 -2.2113406231483168d-5)
(7.920792079207926d0 278.3804120741693d0 -2.9694453814954613d-5)
(8.910891089108917d0 434.7639541876611d0 -3.832280521010034d-5)
(9.900990099009908d0 650.6274697817249d0 -4.799038299552194d-5))</lang>
 
=={{header|D}}==
Anonymous user