I'm trying to get more acquainted with Clojre so I decided to do my Runge Kutta integrator project in it. However I'm having problems working with the immutable nature of the let statement. I want to evaluate 8 variables in each iteration of the loop and use them to recurse through it until my loop is finished.
The way I understand it, since my recur is inside the let's scope, my k's and l's won't be overwritten with each recursion. I'm looking for a more idiomatic way to recurse through my integrator.
(loop [psin 0 Xin 1 xn dx] ;initial values
(if (> xn 1)
psin
(let [k1 (evalg xn psin) ;define 4 variables to calculate next step of Xi, psi
l1 (evalf xn Xin) ;evalf and evalg evaluate the functions as per Runge Kutta
k2 (evalg (+ (* 0.5 dx) xn) (+ (* 0.5 l1) psin))
l2 (evalf (+ (* 0.5 dx) xn) (+ (* 0.5 k1) Xin))
k3 (evalg (+ (* 0.5 dx) xn) (+ (* 0.5 l2) psin))
l3 (evalf (+ (* 0.5 dx) xn) (+ (* 0.5 k2) Xin))
k4 (evalg (+ dx xn) (+ l3 psin))
l4 (evalf (+ dx xn) (+ k3 Xin))]
(do
(let [Xinew (+ Xin (* (/ dx 6) (+ k1 k4 (* 2 k3) (* 2 k2))) )
psinew (+ psin (* (/ dx 6) (+ l1 l4 (* 2 l2) (* 2 l3) )))]
(println k1)
(recur psinew Xinew (+ dx xn)))))))
Many thanks! Looking forward to getting more acquainted with clojure:)