-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhw_test.py
50 lines (37 loc) · 1.25 KB
/
hw_test.py
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
39
40
41
42
43
44
45
46
47
48
49
50
# install pytest first: pip install pytest
# run with the following command: pytest hw_test.py
import numpy as np
from hw07 import *
def test_p1():
y = p1(func1, 1, [0, 3], 10000, 'euler')
assert np.isclose(y[-1], exact1(3), atol=1e-2 )
y = p1(func1, 1, [0, 3], 100, 'midpoint')
assert np.isclose(y[-1], exact1(3), atol=1e-2 )
y = p1(func1, 1, [0, 3], 10, 'rk4')
assert np.isclose(y[-1], exact1(3), atol=1e-2 )
def test_p2():
y = p1(func2, 1, [0, 3], 10000, 'euler')
assert np.isclose(y[-1], exact2(3), atol=1e-4 )
y = p1(func2, 1, [0, 3], 100, 'midpoint')
assert np.isclose(y[-1], exact2(3), atol=1e-4 )
y = p1(func2, 1, [0, 3], 10, 'rk4')
assert np.isclose(y[-1], exact2(3), atol=1e-4 )
def test_p3():
y = p1(func3, 1, [0, 3], 10000, 'euler')
assert np.isclose(y[-1], exact3(3), atol=1e-4 )
y = p1(func3, 1, [0, 3], 100, 'midpoint')
assert np.isclose(y[-1], exact3(3), atol=1e-4 )
y = p1(func3, 1, [0, 3], 10, 'rk4')
assert np.isclose(y[-1], exact3(3), atol=1e-4 )
def func1(t, y):
return y
def func2(t, y):
return -y
def func3(t, y):
return -y + t
def exact1(t):
return np.exp(t)
def exact2(t):
return np.exp(-t)
def exact3(t):
return 2 * np.exp(-t) + t - 1