2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,50 +1,61 @@
Euler's method numerically approximates solutions of first-order ordinary differential equations (ODEs) with a given initial value. It is an explicit method for solving initial value problems (IVPs), as described in [[wp:Euler method|the wikipedia page]].
Euler's method numerically approximates solutions of first-order ordinary differential equations (ODEs) with a given initial value.   It is an explicit method for solving initial value problems (IVPs), as described in [[wp:Euler method|the wikipedia page]].
The ODE has to be provided in the following form:
:<math>\frac{dy(t)}{dt} = f(t,y(t))</math>
::: <big><math>\frac{dy(t)}{dt} = f(t,y(t))</math></big>
with an initial value
:<math>y(t_0) = y_0</math>
::: <big><math>y(t_0) = y_0</math></big>
To get a numeric solution, we replace the derivative on the LHS with a finite difference approximation:
To get a numeric solution, we replace the derivative on the &nbsp; LHS &nbsp; with a finite difference approximation:
:<math>\frac{dy(t)}{dt} \approx \frac{y(t+h)-y(t)}{h}</math>
::: <big><math>\frac{dy(t)}{dt} \approx \frac{y(t+h)-y(t)}{h}</math></big>
then solve for <math>y(t+h)</math>:
:<math>y(t+h) \approx y(t) + h \, \frac{dy(t)}{dt}</math>
::: <big><math>y(t+h) \approx y(t) + h \, \frac{dy(t)}{dt}</math></big>
which is the same as
:<math>y(t+h) \approx y(t) + h \, f(t,y(t))</math>
::: <big><math>y(t+h) \approx y(t) + h \, f(t,y(t))</math></big>
The iterative solution rule is then:
:<math>y_{n+1} = y_n + h \, f(t_n, y_n)</math>
::: <big><math>y_{n+1} = y_n + h \, f(t_n, y_n)</math></big>
where &nbsp; <big><math>h</math></big> &nbsp; is the step size, the most relevant parameter for accuracy of the solution. &nbsp; A smaller step size increases accuracy but also the computation cost, so it has always has to be hand-picked according to the problem at hand.
<math>h</math> is the step size, the most relevant parameter for accuracy of the solution. A smaller step size increases accuracy but also the computation cost, so it has always has to be hand-picked according to the problem at hand.
'''Example: Newton's Cooling Law'''
Newton's cooling law describes how an object of initial temperature <math>T(t_0) = T_0</math> cools down in an environment of temperature <math>T_R </math>:
:<math>\frac{dT(t)}{dt} = -k \, \Delta T</math>
Newton's cooling law describes how an object of initial temperature &nbsp; <big><math>T(t_0) = T_0</math></big> &nbsp; cools down in an environment of temperature &nbsp; <big><math>T_R</math></big>:
::: <big><math>\frac{dT(t)}{dt} = -k \, \Delta T</math></big>
or
::: <big><math>\frac{dT(t)}{dt} = -k \, (T(t) - T_R)</math></big>
:<math>\frac{dT(t)}{dt} = -k \, (T(t) - T_R)</math>
It says that the cooling rate <math>\frac{dT(t)}{dt}</math> of the object is proportional to the current temperature difference <math>\Delta T = (T(t) - T_R)</math> to the surrounding environment.
<br>
It says that the cooling rate &nbsp; <big><math>\frac{dT(t)}{dt}</math></big> &nbsp; of the object is proportional to the current temperature difference &nbsp; <big><math>\Delta T = (T(t) - T_R)</math></big> &nbsp; to the surrounding environment.
The analytical solution, which we will compare to the numerical approximation, is
::: <big><math>T(t) = T_R + (T_0 - T_R) \; e^{-k t}</math></big>
:<math>T(t) = T_R + (T_0 - T_R) \; e^{-k t}</math>
'''Task'''
;Task:
Implement a routine of Euler's method and then to use it to solve the given example of Newton's cooling law with it for three different step sizes of:
:::* &nbsp; 2 s
:::* &nbsp; 5 s &nbsp; &nbsp; &nbsp; and
:::* &nbsp; 10 s
and to compare with the analytical solution.
The task is to implement a routine of Euler's method and then to use it to solve the given example of Newton's cooling law with it for three different step sizes of 2 s, 5 s and 10 s and to compare with the analytical solution.
The initial temperature <math>T_0</math> shall be 100 °C, the room temperature <math>T_R</math> 20 °C, and the cooling constant <math>k</math> 0.07. The time interval to calculate shall be from 0 s to 100 s.
A reference solution ([[#Common Lisp|Common Lisp]]) can be seen below. We see that bigger step sizes lead to reduced approximation accuracy.
;Initial values:
:::* &nbsp; initial temperature &nbsp; <big><math>T_0</math></big> &nbsp; shall be &nbsp; 100 °C
:::* &nbsp; room temperature &nbsp; <big><math>T_R</math></big> &nbsp; shall be &nbsp; 20 °C
:::* &nbsp; cooling constant &nbsp; &nbsp; <big><math>k</math></big> &nbsp; &nbsp; shall be &nbsp; 0.07
:::* &nbsp; time interval to calculate shall be from &nbsp; 0 s &nbsp; ──► &nbsp; 100 s
<br>
A reference solution ([[#Common Lisp|Common Lisp]]) can be seen below. &nbsp; We see that bigger step sizes lead to reduced approximation accuracy.
[[Image:Euler_Method_Newton_Cooling.png|center|750px]]

View file

@ -0,0 +1,21 @@
(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)))

View file

@ -7,8 +7,8 @@
(defun euler (f y0 a b h)
;; Set the initial values and increments of the iteration variables.
(do ((t a (incf t h))
(y y0 (incf y (* h (funcall f t y)))))
(do ((t a (+ t h))
(y y0 (+ y (* h (funcall f t y)))))
;; End the iteration when t reaches the end b of the time interval.
((>= t b) 'DONE)

View file

@ -0,0 +1,13 @@
;; 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)))

View file

@ -0,0 +1,38 @@
-module(euler).
-export([main/0, euler/5]).
cooling(_Time, Temperature) ->
(-0.07)*(Temperature-20).
euler(_, Y, T, _, End) when End == T ->
io:fwrite("\n"),
Y;
euler(Func, Y, T, Step, End) ->
if
T rem 10 == 0 ->
io:fwrite("~.3f ",[float(Y)]);
true ->
ok
end,
euler(Func, Y + Step * Func(T, Y), T + Step, Step, End).
analytic(T, End) when T == End ->
io:fwrite("\n"),
T;
analytic(T, End) ->
Y = (20 + 80 * math:exp(-0.07 * T)),
io:fwrite("~.3f ", [Y]),
analytic(T+10, End).
main() ->
io:fwrite("Analytic:\n"),
analytic(0, 100),
io:fwrite("Step 2:\n"),
euler(fun cooling/2, 100, 0, 2, 100),
io:fwrite("Step 5:\n"),
euler(fun cooling/2, 100, 0, 5, 100),
io:fwrite("Step 10:\n"),
euler(fun cooling/2, 100, 0, 10, 100),
ok.

View file

@ -0,0 +1,5 @@
-- the solver
dsolveBy _ _ [] _ = error "empty solution interval"
dsolveBy method f mesh x0 = zip mesh results
where results = scanl (method f) x0 intervals
intervals = zip mesh (tail mesh)

View file

@ -0,0 +1,14 @@
-- 1-st order Euler
euler f x (t1,t2) = x + (t2 - t1) * f t1 x
-- 2-nd order Runge-Kutta
rk2 f x (t1,t2) = x + h * f (t1 + h/2) (x + h/2*f t1 x)
where h = t2 - t1
-- 4-th order Runge-Kutta
rk4 f x (t1,t2) = x + h/6 * (k1 + 2*k2 + 2*k3 + k4)
where k1 = f t1 x
k2 = f (t1 + h/2) (x + h/2*k1)
k3 = f (t1 + h/2) (x + h/2*k2)
k4 = f (t1 + h) (x + h*k3)
h = t2 - t1

View file

@ -0,0 +1,23 @@
import Graphics.EasyPlot
newton t temp = -0.07 * (temp - 20)
exactSolution t = 80*exp(-0.07*t)+20
test1 = plot (PNG "euler1.png")
[ Data2D [Title "Step 10", Style Lines] [] sol1
, Data2D [Title "Step 5", Style Lines] [] sol2
, Data2D [Title "Step 1", Style Lines] [] sol3
, Function2D [Title "exact solution"] [Range 0 100] exactSolution ]
where sol1 = dsolveBy euler newton [0,10..100] 100
sol2 = dsolveBy euler newton [0,5..100] 100
sol3 = dsolveBy euler newton [0,1..100] 100
test2 = plot (PNG "euler2.png")
[ Data2D [Title "Euler"] [] sol1
, Data2D [Title "RK2"] [] sol2
, Data2D [Title "RK4"] [] sol3
, Function2D [Title "exact solution"] [Range 0 100] exactSolution ]
where sol1 = dsolveBy euler newton [0,10..100] 100
sol2 = dsolveBy rk2 newton [0,10..100] 100
sol3 = dsolveBy rk4 newton [0,10..100] 100

View file

@ -1,15 +0,0 @@
import Text.Printf
euler :: (Num a, Ord a) => (a -> a -> a) -> a -> a -> a -> a -> [(a,a)]
euler f y0 a b h =
(a, y0) :
if a < b
then euler f (y0 + (f a y0) * h) (a + h) b h
else []
newtonCooling :: Double -> Double -> Double
newtonCooling _ t = -0.07 * (t - 20)
main = do
mapM_ (uncurry $ printf "%6.3f %6.3f\n") $ euler newtonCooling 100 0 100 10
putStrLn "DONE"

View file

@ -0,0 +1,26 @@
/*REXX pgm solves example of Newton's cooling law via Euler's method (diff. step sizes).*/
numeric digits length( e() - 1) /*use the number of decimal digits in E*/
parse arg Ti Tr cc tt ss /*obtain optional arguments from the CL*/
if Ti=='' | Ti=="," then Ti=100 /*given? Default: initial temp in ºC.*/
if Tr=='' | Tr=="," then Tr= 20 /* " " room " " " */
if cc=='' | cc=="," then cc= 0.07 /* " " cooling constant. */
if tt=='' | tt=="," then tt=100 /* " " total time seconds. */
if ss ='' | ss ="," then ss=2 5 10 /* " " the step sizes. */
@= '' /*the character used in title separator*/
do sSize=1 for words(ss); say; say; say center('time in' , 11)
say center('seconds' , 11, @) center('Euler method', 16, @) ,
center('analytic', 18, @) center('difference' , 14, @)
$=Ti; inc=word(ss,Ssize) /*the 1st value; obtain the increment.*/
do t=0 to Ti by inc /*step through calculations by the inc.*/
a=format(Tr + (Ti-Tr)/exp(cc*t),6,9) /*calculate the analytic (exact) value.*/
say center(t,11) format($,6,3) 'ºC ' a "ºC" format(abs(a-$)/a*100,6,2) '%'
$=$ + inc * cc * (Tr-$) /*calc. next value via Euler's method. */
end /*t*/
end /*stepSize*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
e: return 2.718281828459045235360287471352662497757247093699959574966967627724076630353548
/*──────────────────────────────────────────────────────────────────────────────────────*/
exp: procedure; parse arg x; ix=x%1; if abs(x-ix)>.5 then ix=ix+sign(x); x=x-ix; z=1
_=1; w=1; do j=1; _=_*x/j; z=(z+_)/1; if z==w then leave; w=z; end /*j*/
if z\==0 then z=e()**ix * z; return z

View file

@ -0,0 +1,3 @@
10 LET d$="-0.07*(y-20)": LET y=100: LET a=0: LET b=100: LET s=10
20 LET t=a
30 IF t<=b THEN PRINT t;TAB 10;y: LET y=y+s*VAL d$: LET t=t+s: GO TO 30