-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise-9.py
54 lines (36 loc) · 991 Bytes
/
exercise-9.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
51
52
53
54
import math
from tabulate import tabulate
def f(t, y):
return y * math.cos(t + math.pow(y, 2))
def solve_euler():
table = []
a, b, n = 0, 3, 10
h = (b - a) / n
y = 3
for i in range(0, 11):
t_i = a + i * h
n_y = y + h * f(t_i, y)
table.append([i, y])
y = n_y
return table
def k1(t, y):
return y * math.cos(t + y ** 2)
def k2(t, y, h):
k1_i = k1(t, y)
return k1(t + h / 2, y + (h / 2) * k1_i)
def solve_runge_kutta():
table = []
a, b, n = 0, 3, 10
h = (b - a) / n
y = 3
for i in range(0, 11):
t_i = a + i * h
n_y = y + (1 / 6) * h * (k1(t_i, y) + 2 * k2(t_i, y, h))
table.append([i, y])
y = n_y
return table
if __name__ == '__main__':
r_euler = solve_euler()
print('Euler\n', tabulate(r_euler, headers=['i', 'y']), '\n')
r_runge_kutta = solve_runge_kutta()
print('Runge-Kutta\n', tabulate(r_runge_kutta, headers=['i', 'y']), '\n')