-
-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathbisection.test.ts
32 lines (24 loc) · 989 Bytes
/
bisection.test.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
import { bisection } from '../bisection';
const f = (x: number): number => x * x - 2;
const g = (x: number): number => x * x * x - 4;
describe('Bisection Method with multiple functions', () => {
test.each([
[1, 2, Math.sqrt(2)],
[0, 2, Math.sqrt(2)],
])('should find the root of f(x) = x^2 - 2 in range (%i, %i)', (a, b, expected) => {
const root = bisection(f, a, b, 1e-7, 100);
expect(root).toBeCloseTo(expected, 6);
});
test.each([
[1, 2, Math.cbrt(4)],
[1, 3, Math.cbrt(4)],
])('should find the root of g(x) = x^3 - 4 in range (%i, %i)', (a, b, expected) => {
const root = bisection(g, a, b, 1e-7, 100);
expect(root).toBeCloseTo(expected, 6);
});
test('should throw error for invalid range', () => {
expect(() => bisection(f, -1, 1, 1e-7, 100)).toThrow(
'The function must have opposite signs at the endpoints a and b.'
);
});
});