Euler method: Difference between revisions

Content added Content deleted
(→‎{{header|Common Lisp}}: incf is not needed, it sets the variable - which is already done by DO)
(→‎{{header|Common Lisp}}: slightly more modern/idiomatic version)
Line 586: Line 586:
(euler #'newton-cooling 100 0 100 5)
(euler #'newton-cooling 100 0 100 5)
(euler #'newton-cooling 100 0 100 10)</lang>
(euler #'newton-cooling 100 0 100 10)</lang>

<lang lisp>;; slightly more idiomatic Common Lisp version

(defun newton-cooling (time temperature)
"Newton's cooling law, f(t,T) = -0.07*(T-20)"
(declare (ignore time))
(* -0.07 (- temperature 20)))

(defun 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 for time from a below b by h
for y = y0 then (+ y (* h (funcall f time y)))
do (format t "~6,3F ~6,3F~%" time y)))</lang>


<pre>
<pre>