-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy patheuler_method.ts
38 lines (33 loc) · 1017 Bytes
/
euler_method.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* @description Euler's method is a first-order numerical procedure for solving ordinary differential equations (ODEs) with a given initial value.
* @param {number} x0 - The initial value of x
* @param {number} y0 - The initial value of y
* @param {number} h - The step size
* @param {number} n - The number of iterations
* @param {Function} f - The function
* @return {number} - The value of y at x
* @see [EulerMethod](https://en.wikipedia.org/wiki/Euler_method)
* @example eulerMethod(0, 1, 0.1, 10, (x, y) => x + y) = 2.5937424601
* @example eulerMethod(0, 1, 0.1, 10, (x, y) => x * y) = 1.7715614317
*/
export const eulerMethod = (
x0: number,
y0: number,
h: number,
n: number,
f: Function
): number => {
if (typeof f !== 'function') {
throw new Error('f must be a function')
}
if (n < 0) {
throw new Error('Number of iterations must be non-negative')
}
let x = x0
let y = y0
for (let i = 0; i < n; i++) {
y = y + h * f(x, y)
x = x + h
}
return y
}