-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathBk3_Ch18_02.py
94 lines (67 loc) · 2.45 KB
/
Bk3_Ch18_02.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
###############
# Authored by Weisheng Jiang
# Book 3 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
# Bk3_Ch18_02
import numpy as np
from sympy import *
from matplotlib import pyplot as plt
x = Symbol('x')
f_x = exp(-x**2)
integrate(f_x,(x,-oo,oo))
integrate(exp(-x**2/2),(x,-oo,oo))
f_x_fcn = lambdify([x],f_x)
integral_f_x = integrate(f_x, x)
integral_f_x_fcn = lambdify([x],integral_f_x)
a = -0.5
b = 1
num = 201; # number of mesh grids
x_array = np.linspace(-3,3,num)
x_a_b_array = np.linspace(a,b,num)
y_array = f_x_fcn(x_array)
y_a_b_array = f_x_fcn(x_a_b_array)
fig, ax = plt.subplots()
ax.plot(x_array, y_array, 'b')
ax.axvline(x = a, color = 'r', linestyle = '-')
ax.axvline(x = b, color = 'r', linestyle = '-')
ax.axhline(y = 0, color = 'k', linestyle = '-')
ax.fill_between(x_a_b_array,
y_a_b_array,
edgecolor = 'none',
facecolor = '#DBEEF3')
ax.set_xlim(x_array.min(), x_array.max())
ax.set_ylim(np.floor(y_array.min()),
np.ceil(y_array.max()))
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
ax.set_xlabel('x')
ax.set_ylabel('f(x)')
integral_a_b = integral_f_x_fcn(b) - integral_f_x_fcn(a)
integral_a_b_v2 = integrate(f_x, (x, a, b))
integral_a_b_v2 = float(integral_a_b_v2)
ax.set_title(r'$\int_a^b f(x) = %0.3f$'%integral_a_b)
#%% plot integral function
t = Symbol('t')
integral_f_x_oo_t = integrate(f_x, (x,-oo,t))
integral_f_x_oo_t_fcn = lambdify([t],integral_f_x_oo_t)
t_array = np.linspace(-3,3,num)
integral_f_x_oo_t_array = integral_f_x_oo_t_fcn(t_array)
fig, ax = plt.subplots()
ax.plot(t_array, integral_f_x_oo_t_array, 'b')
ax.axvline(x = a, color = 'r', linestyle = '-')
ax.axvline(x = b, color = 'r', linestyle = '-')
ax.axhline(y = integral_f_x_oo_t_fcn(a),
color = 'r', linestyle = '-')
ax.axhline(y = integral_f_x_oo_t_fcn(b),
color = 'r', linestyle = '-')
ax.set_xlim(t_array.min(), t_array.max())
ax.set_ylim(np.floor(integral_f_x_oo_t_array.min()),
np.ceil(integral_f_x_oo_t_array.max()))
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
ax.set_xlabel('x')
ax.set_ylabel('Integral, F(x)')
ax.grid(linestyle='--', linewidth=0.25, color=[0.75,0.75,0.75])
plt.show()