Euler method: Difference between revisions

(Restoring visibility of formulae hidden by under-tested cosmetic edits at 19:49, 22 July 2016)
Line 569:
x = 80.0000, y = 20.0052
x = 90.0000, y = 20.0016
</pre>
 
=={{header|Clojure}}==
{{trans|Python}}
<lang lisp>(ns newton-cooling
(:gen-class))
 
(defn euler [f y0 a b h]
"Euler's Method.
Approximates y(time) in y'(time)=f(time,y) with y(a)=y0 and t=a..b and the step size h."
(loop [t a
y y0
result []]
(if (<= t b)
(recur (+ t h) (+ y (* (f (+ t h) y) h)) (conj result [(double t) (double y)]))
result)))
 
(defn newton-coolling [t temp]
"Newton's cooling law, f(t,T) = -0.07*(T-20)"
(* -0.07 (- temp 20)))
 
; Run for case h = 10
(println "Example output")
(doseq [q (euler newton-coolling 100 0 100 10)]
(println (apply format "%.3f %.3f" q)))
</lang>
{{Output}}
<pre>
Example output
0.000 100.000
10.000 44.000
20.000 27.200
30.000 22.160
40.000 20.648
50.000 20.194
60.000 20.058
70.000 20.017
80.000 20.005
90.000 20.002
100.000 20.000
</pre>