|
1 |
| -import { eulerMethod } from './eulerMethod'; |
| 1 | +import { eulerMethod } from '../euler_method' |
2 | 2 |
|
3 | 3 | describe('eulerMethod', () => {
|
4 |
| - it('should compute y for a linear function (x + y)', () => { |
5 |
| - const result = eulerMethod(0, 1, 0.1, 10, (x, y) => x + y); |
6 |
| - expect(result).toBeCloseTo(2.5937424601, 5); |
7 |
| - }); |
8 |
| - |
9 |
| - it('should compute y for a multiplicative function (x * y)', () => { |
10 |
| - const result = eulerMethod(0, 1, 0.1, 10, (x, y) => x * y); |
11 |
| - expect(result).toBeCloseTo(1.7715614317, 5); |
12 |
| - }); |
13 |
| - |
14 |
| - it('should return the initial value y0 when there are zero iterations', () => { |
15 |
| - const result = eulerMethod(0, 1, 0.1, 0, (x, y) => x + y); |
16 |
| - expect(result).toBe(1); |
17 |
| - }); |
18 |
| - |
19 |
| - it('should return the correct value for a very small step size', () => { |
20 |
| - const result = eulerMethod(0, 1, 0.01, 100, (x, y) => x + y); |
21 |
| - expect(result).toBeCloseTo(2.7048138294, 5); |
22 |
| - }); |
23 |
| - |
24 |
| - it('should return the correct value after one iteration', () => { |
25 |
| - const result = eulerMethod(0, 1, 0.1, 1, (x, y) => x + y); |
26 |
| - expect(result).toBeCloseTo(1.1, 5); |
27 |
| - }); |
28 |
| - |
29 |
| - it('should return the initial value y0 when step size is zero', () => { |
30 |
| - const result = eulerMethod(0, 1, 0, 10, (x, y) => x + y); |
31 |
| - expect(result).toBe(1); |
32 |
| - }); |
33 |
| - |
34 |
| - it('should return correct value for negative step size', () => { |
35 |
| - const result = eulerMethod(1, 1, -0.1, 10, (x, y) => x + y); |
36 |
| - expect(result).toBeCloseTo(0.3162798676, 5); |
37 |
| - }); |
38 |
| - |
39 |
| - it('should throw an error when number of iterations is negative', () => { |
40 |
| - expect(() => eulerMethod(0, 1, 0.1, -5, (x, y) => x + y)).toThrow('Number of iterations must be non-negative'); |
41 |
| - }); |
42 |
| - |
43 |
| -}); |
| 4 | + it('should compute y for dy/dx = y with y(0) = 1 at x = 1', () => { |
| 5 | + const result = eulerMethod(0, 1, 0.1, 10, (x: number, y: number) => y) |
| 6 | + expect(result).toBeCloseTo(2.59374, 5) |
| 7 | + }) |
| 8 | + |
| 9 | + it('should compute y for dy/dx = -2y with y(0) = 1 at x = 1', () => { |
| 10 | + const result = eulerMethod(0, 1, 0.1, 10, (x: number, y: number) => -2 * y) |
| 11 | + const expectedResult = 1 * Math.pow(0.8, 10) |
| 12 | + expect(result).toBeCloseTo(expectedResult, 5) |
| 13 | + }) |
| 14 | + |
| 15 | + it('should compute y for dy/dx = x with y(0) = 0 at x = 1', () => { |
| 16 | + const result = eulerMethod(0, 0, 0.1, 10, (x: number, y: number) => x) |
| 17 | + expect(result).toBeCloseTo(0.45, 2) |
| 18 | + }) |
| 19 | + |
| 20 | + it('should compute y for dy/dx = x + y with y(0) = 1 at x = 0.5', () => { |
| 21 | + const h = 0.1 |
| 22 | + const n = 5 |
| 23 | + const result = eulerMethod(0, 1, h, n, (x: number, y: number) => x + y) |
| 24 | + expect(result).toBeCloseTo(1.72102, 5) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should compute y for dy/dx = x^2 with y(0) = 0 at x = 1', () => { |
| 28 | + const result = eulerMethod(0, 0, 0.2, 5, (x: number, y: number) => x ** 2) |
| 29 | + expect(result).toBeCloseTo(0.24, 3) |
| 30 | + }) |
| 31 | + |
| 32 | + it('should handle negative step size for dy/dx = y with y(1) = e', () => { |
| 33 | + const result = eulerMethod( |
| 34 | + 1, |
| 35 | + Math.E, |
| 36 | + -0.001, |
| 37 | + 1000, |
| 38 | + (x: number, y: number) => y |
| 39 | + ) |
| 40 | + expect(result).toBeCloseTo(1, 2) |
| 41 | + }) |
| 42 | +}) |
0 commit comments