2018-09-30 23:01:58 +01:00
---
id: 59880443fb36441083c6c20e
2020-11-27 19:02:05 +01:00
title: Euler method
2018-09-30 23:01:58 +01:00
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302258
2021-01-13 03:31:00 +01:00
dashedName: euler-method
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
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 [the wikipedia page ](<https://en.wikipedia.org/wiki/Euler method> "wp: Euler method" ).
2019-03-02 17:42:56 +09:00
The ODE has to be provided in the following form:
2020-11-27 19:02:05 +01:00
< ul style = 'list-style: none;' >
2019-03-02 17:42:56 +09:00
< li >< big > $\frac{dy(t)}{dt} = f(t,y(t))$</ big ></ li >
< / ul >
2020-11-27 19:02:05 +01:00
2019-03-02 17:42:56 +09:00
with an initial value
2020-11-27 19:02:05 +01:00
< ul style = 'list-style: none;' >
2019-03-02 17:42:56 +09:00
< li > < big > $y(t_0) = y_0$< / big > < / li >
< / ul >
2020-11-27 19:02:05 +01:00
To get a numeric solution, we replace the derivative on the LHS with a finite difference approximation:
< ul style = 'list-style: none;' >
2019-03-02 17:42:56 +09:00
< li >< big > $\frac{dy(t)}{dt} \approx \frac{y(t+h)-y(t)}{h}$</ big ></ li >
< / ul >
2020-11-27 19:02:05 +01:00
2019-03-02 17:42:56 +09:00
then solve for $y(t+h)$:
2020-11-27 19:02:05 +01:00
< ul style = 'list-style: none;' >
2019-03-02 17:42:56 +09:00
< li >< big > $y(t+h) \approx y(t) + h \, \frac{dy(t)}{dt}$</ big ></ li >
< / ul >
2020-11-27 19:02:05 +01:00
2019-03-02 17:42:56 +09:00
which is the same as
2020-11-27 19:02:05 +01:00
< ul style = 'list-style: none;' >
2019-03-02 17:42:56 +09:00
< li >< big > $y(t+h) \approx y(t) + h \, f(t,y(t))$</ big ></ li >
< / ul >
2020-11-27 19:02:05 +01:00
2019-03-02 17:42:56 +09:00
The iterative solution rule is then:
2020-11-27 19:02:05 +01:00
< ul style = 'list-style: none;' >
2019-03-02 17:42:56 +09:00
< li >< big > $y_{n+1} = y_n + h \, f(t_n, y_n)$</ big ></ li >
< / ul >
2020-11-27 19:02:05 +01:00
where $h$ 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 $T(t_0) = T_0$ cools down in an environment of temperature $T_R$:
< ul style = 'list-style: none;' >
2019-03-02 17:42:56 +09:00
< li >< big > $\frac{dT(t)}{dt} = -k \, \Delta T$</ big ></ li >
< / ul >
2020-11-27 19:02:05 +01:00
2019-03-02 17:42:56 +09:00
or
2020-11-27 19:02:05 +01:00
< ul style = 'list-style: none;' >
2019-03-02 17:42:56 +09:00
< li >< big > $\frac{dT(t)}{dt} = -k \, (T(t) - T_R)$</ big ></ li >
< / ul >
2020-11-27 19:02:05 +01:00
It says that the cooling rate $\\frac{dT(t)}{dt}$ of the object is proportional to the current temperature difference $\\Delta T = (T(t) - T_R)$ to the surrounding environment.
2019-03-02 17:42:56 +09:00
The analytical solution, which we will compare to the numerical approximation, is
2020-11-27 19:02:05 +01:00
< ul style = 'list-style: none;' >
2019-03-02 17:42:56 +09:00
< li >< big > $T(t) = T_R + (T_0 - T_R) \; e^{-k t}$</ big ></ li >
< / ul >
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --instructions--
2020-08-25 18:37:10 +03:00
Implement a routine of Euler's method and then use it to solve the given example of Newton's cooling law for three different step sizes of:
2020-11-27 19:02:05 +01:00
2019-03-02 17:42:56 +09:00
< ul >
2019-06-14 20:04:16 +09:00
< li > < code > 2 s< / code > < / li >
< li > < code > 5 s< / code > and< / li >
< li > < code > 10 s< / code > < / li >
2019-03-02 17:42:56 +09:00
< / ul >
2020-11-27 19:02:05 +01:00
2020-08-25 18:37:10 +03:00
and compare with the analytical solution.
2020-11-27 19:02:05 +01:00
**Initial values:**
2019-03-02 17:42:56 +09:00
< ul >
2019-06-14 20:04:16 +09:00
< li > initial temperature < big > $T_0$< / big > shall be < code > 100 °C< / code > < / li >
< li > room temperature < big > $T_R$< / big > shall be < code > 20 °C< / code > < / li >
< li > cooling constant < big > $k$< / big > shall be < code > 0.07< / code > < / li >
< li > time interval to calculate shall be from < code > 0 s< / code > to < code > 100 s< / code > < / li >
2020-11-27 19:02:05 +01:00
< / ul >
2020-08-25 18:37:10 +03:00
First parameter to the function is initial time, second parameter is initial temperature, third parameter is elapsed time and fourth parameter is step size.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2020-08-25 18:37:10 +03:00
2020-11-27 19:02:05 +01:00
`eulersMethod` should be a function.
2020-08-25 18:37:10 +03:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof eulersMethod === 'function');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`eulersMethod(0, 100, 100, 2)` should return a number.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof eulersMethod(0, 100, 100, 2) === 'number');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`eulersMethod(0, 100, 100, 2)` should return 20.0424631833732.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.equal(eulersMethod(0, 100, 100, 2), 20.0424631833732);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`eulersMethod(0, 100, 100, 5)` should return 20.01449963666907.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert.equal(eulersMethod(0, 100, 100, 5), 20.01449963666907);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`eulersMethod(0, 100, 100, 10)` should return 20.000472392.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.equal(eulersMethod(0, 100, 100, 10), 20.000472392);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function eulersMethod(x1, y1, x2, h) {
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
}
```
# --solutions--
2018-09-30 23:01:58 +01:00
```js
function eulersMethod(x1, y1, x2, h) {
let x = x1;
let y = y1;
while ((x < x2 & & x1 < x2 ) | | ( x > x2 & & x1 > x2)) {
y += h * (-0.07 * (y - 20));
x += h;
}
return y;
}
```