4.4 KiB
4.4 KiB
title | id | challengeType |
---|---|---|
Euler method | 59880443fb36441083c6c20e | 5 |
Description
- $\frac{dy(t)}{dt} = f(t,y(t))$
- $y(t_0) = y_0$
- $\frac{dy(t)}{dt} \approx \frac{y(t+h)-y(t)}{h}$
- $y(t+h) \approx y(t) + h \, \frac{dy(t)}{dt}$
- $y(t+h) \approx y(t) + h \, f(t,y(t))$
- $y_{n+1} = y_n + h \, f(t_n, y_n)$
- $\frac{dT(t)}{dt} = -k \, \Delta T$
- $\frac{dT(t)}{dt} = -k \, (T(t) - T_R)$
- $T(t) = T_R + (T_0 - T_R) \; e^{-k t}$
Instructions
- 2 s
- 5 s and
- 10 s
- initial temperature $T_0$ shall be 100 °C
- room temperature $T_R$ shall be 20 °C
- cooling constant $k$ shall be 0.07
- time interval to calculate shall be from 0s to 100 s
Tests
tests:
- text: <code>eulersMethod</code> is a function.
testString: assert(typeof eulersMethod === 'function', '<code>eulersMethod</code> is a function.');
- text: <code>eulersMethod(0, 100, 100, 10)</code> should return a number.
testString: assert(typeof eulersMethod(0, 100, 100, 10) === 'number', '<code>eulersMethod(0, 100, 100, 10)</code> should return a number.');
- text: <code>eulersMethod(0, 100, 100, 10)</code> should return 20.0424631833732.
testString: assert.equal(eulersMethod(0, 100, 100, 2), 20.0424631833732, '<code>eulersMethod(0, 100, 100, 10)</code> should return 20.0424631833732.');
- text: <code>eulersMethod(0, 100, 100, 10)</code> should return 20.01449963666907.
testString: assert.equal(eulersMethod(0, 100, 100, 5), 20.01449963666907, '<code>eulersMethod(0, 100, 100, 10)</code> should return 20.01449963666907.');
- text: <code>eulersMethod(0, 100, 100, 10)</code> should return 20.000472392.
testString: assert.equal(eulersMethod(0, 100, 100, 10), 20.000472392, '<code>eulersMethod(0, 100, 100, 10)</code> should return 20.000472392.');
Challenge Seed
function eulersMethod(x1, y1, x2, h) {
// Good luck!
}
Solution
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;
}