-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexplicit_euler_method.py
58 lines (38 loc) · 1.32 KB
/
explicit_euler_method.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
55
56
57
58
import numpy as np
import matplotlib.pyplot as plt
import math
# ----- Functions ------
def analytical_func(x):
return math.log(((x**3)/(3*math.e))+math.e)
def dydx(x, y):
return (x**2) * (np.exp(-(y+1)))
# ---- values ----
h = 0.3 # Step size
steps = np.arange(0,3 + h, h) # Numerical grid
y = 1 # Initial Condition
# ---- explicit euler method
u = np.zeros(len(steps))
u[0] = y
for i in range(0, len(steps) - 1):
u[i + 1] = u[i] + h*dydx(steps[i], u[i])
# ----- analytic solution -----
analytic_solns = []
for i in steps:
analytic_solns.append(analytical_func(i))
real_end_y = analytic_solns[-1]
# ---- last value numeric ----
last_numeric_val = u[-1]
# ---- measurements ----
print('The analytical value of y at x = 3 is: {}'.format(real_end_y))
print('The numerical method value of y at x = 3 is: {}'.format(last_numeric_val))
print('The explicit euler error of y at x = 3 is: %{}'.format(100*(abs(last_numeric_val-real_end_y)/real_end_y)))
# ----- plotting -----
#plt.figure(figsize = (8, 4))
plt.plot(steps, u, 'bo--', label='Explicit Euler')
plt.plot(steps, analytic_solns , 'go--', label='Exact')
plt.title('Exact vs Euler Method')
plt.xlabel('x values - step size 0.3')
plt.ylabel('u(x) values at x')
plt.grid()
plt.legend(loc='upper left')
plt.show()