RosettaCodeData/Task/Euler-method/00DESCRIPTION

62 lines
2.8 KiB
Plaintext

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:
::: <big><math>\frac{dy(t)}{dt} = f(t,y(t))</math></big>
with an initial value
::: <big><math>y(t_0) = y_0</math></big>
To get a numeric solution, we replace the derivative on the &nbsp; LHS &nbsp; with a finite difference approximation:
::: <big><math>\frac{dy(t)}{dt} \approx \frac{y(t+h)-y(t)}{h}</math></big>
then solve for <math>y(t+h)</math>:
::: <big><math>y(t+h) \approx y(t) + h \, \frac{dy(t)}{dt}</math></big>
which is the same as
::: <big><math>y(t+h) \approx y(t) + h \, f(t,y(t))</math></big>
The iterative solution rule is then:
::: <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.
'''Example: Newton's Cooling Law'''
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>
<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>
;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.
;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]]