forked from Visualize-ML/Book3_Elements-of-Mathematics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBk3_Ch13_01.py
More file actions
110 lines (76 loc) · 2.57 KB
/
Bk3_Ch13_01.py
File metadata and controls
110 lines (76 loc) · 2.57 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
###############
# Authored by Weisheng Jiang
# Book 3 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
# Bk3_Ch13_01
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
def mesh_square(x1_0,x2_0,r,num):
# generate mesh
rr = np.linspace(-r,r,num)
xx1,xx2 = np.meshgrid(rr,rr);
xx1 = xx1 + x1_0;
xx2 = xx2 + x2_0;
return xx1, xx2
def plot_surf(xx1,xx2,ff,caption):
norm_plt = plt.Normalize(ff.min(), ff.max())
colors = cm.coolwarm(norm_plt(ff))
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(xx1,xx2,ff,
facecolors=colors, shade=False)
surf.set_facecolor((0,0,0,0))
# z_lim = [ff.min(),ff.max()]
# ax.plot3D([0,0],[0,0],z_lim,'k')
plt.show()
plt.tight_layout()
ax.set_xlabel('$\it{x_1}$')
ax.set_ylabel('$\it{x_2}$')
ax.set_zlabel('$\it{f}$($\it{x_1}$,$\it{x_2}$)')
ax.set_title(caption)
ax.xaxis._axinfo["grid"].update({"linewidth":0.25, "linestyle" : ":"})
ax.yaxis._axinfo["grid"].update({"linewidth":0.25, "linestyle" : ":"})
ax.zaxis._axinfo["grid"].update({"linewidth":0.25, "linestyle" : ":"})
plt.rcParams["font.family"] = "Times New Roman"
plt.rcParams["font.size"] = "10"
def plot_contourf(xx1,xx2,ff,caption):
fig, ax = plt.subplots()
cntr2 = ax.contourf(xx1,xx2,ff, levels = 15, cmap="RdBu_r")
fig.colorbar(cntr2, ax=ax)
plt.show()
ax.set_xlabel('$\it{x_1}$')
ax.set_ylabel('$\it{x_2}$')
ax.set_title(caption)
ax.grid(linestyle='--', linewidth=0.25, color=[0.5,0.5,0.5])
#%% initialization
x1_0 = 0; # center of the mesh
x2_0 = 0; # center of the mesh
r = 2; # radius of the mesh
num = 30; # number of mesh grids
xx1,xx2 = mesh_square(x1_0,x2_0,r,num); # generate mesh
#%% Visualizations
plt.close('all')
# f(x1,x2) = -x1
ff = -xx1;
caption = '$\it{f} = -\it{x_1}$';
plot_surf (xx1,xx2,ff,caption)
plot_contourf (xx1,xx2,ff,caption)
#%% f(x1,x2) = x2
ff = xx2;
caption = '$\it{f} = \it{x_2}$';
plot_surf (xx1,xx2,ff,caption)
plot_contourf (xx1,xx2,ff,caption)
#%% f(x1,x2) = x1 + x2
ff = xx1 + xx2;
caption = '$\it{f} = \it{x_1} + \it{x_2}$';
plot_surf (xx1,xx2,ff,caption)
plot_contourf (xx1,xx2,ff,caption)
#%% f(x1,x2) = -x1 + x2
ff = -xx1 + xx2;
caption = '$\it{f} = -\it{x_1} + \it{x_2}$';
plot_surf (xx1,xx2,ff,caption)
plot_contourf (xx1,xx2,ff,caption)