-
Notifications
You must be signed in to change notification settings - Fork 64
/
hw06_solution.py
63 lines (49 loc) · 1.3 KB
/
hw06_solution.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
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# ========== HW06 SOLUTION [Python2/3] ========== #
# ========== 1 ========== #
def beale(x1, x2):
return ((1.5 - x1 * (1 - x2)) ** 2
+ (2.25 - x1 * (1 - x2 ** 2)) ** 2
+ (2.625 - x1 * (1 - x2 ** 3)) ** 2)
x1 = np.linspace(0, 4, 100)
x2 = np.linspace(-0.5, 1, 100)
x1, x2 = np.meshgrid(x1, x2)
f = beale(x1, x2)
# ========== 2 ========== #
plt.figure()
plt.contourf(x1, x2, f, 100, cmap=plt.cm.hot)
plt.colorbar()
plt.xlabel(r'$x_1$')
plt.ylabel(r'$x_2$')
plt.show()
# ========== 3 ========== #
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x1, x2, f, 'ob')
ax.set(xlabel=r'$x_1$',
ylabel=r'$x_2$',
zlabel=r'$f(x1, x2)$')
fig.show()
# ========== 4 ========== #
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x1, x2, f)
ax.set(xlabel=r'$x_1$',
ylabel=r'$x_2$',
zlabel=r'$f(x1, x2)$')
fig.show()
# ========== 5 ========== #
a = np.array([3.5, -0.2])
b = np.array([2.0, 0.9])
n_points = 100
lam = np.linspace(0, 1, num=n_points)
x1 = np.linspace(a[0], b[0], num=n_points)
x2 = np.linspace(a[1], b[1], num=n_points)
f = beale(x1, x2)
plt.figure()
plt.plot(lam, f)
plt.xlabel(r'$x_1$')
plt.ylabel(r'$x_2$')
plt.show()