import seaborn as sns import matplotlib pyplot as plt from matplotlib

 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
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from numpy import *
fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.linspace(-7, 7, 100)
y = np.linspace(-7, 7, 100)
X, Y = np.meshgrid(x, y)
Z = 10*(X**2 - Y**2)**2 - (1 - X)**2 + X*Y
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
fig = plt.figure()
ax = fig.gca(projection='3d')
mult = 20
x = np.linspace(-7, 7, 100)
y = np.linspace(-7, 7, 100)
X, Y = np.meshgrid(x, y)
Z = 10*(X**2 - Y**2)**2 - (1 - X)**2 + X*Y
cset = ax.contour(X, Y, Z, [1, 4, 9, 16], cmap=cm.coolwarm)
ax.clabel(cset, fontsize=90, inline=1)
fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y = numpy.linspace(left, right, 200), numpy.linspace(left, right, 200)
(X, Y), Z = numpy.meshgrid(X, Y), f(X, Y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, linewidth=0, antialiased=False)
from scipy.optimize import minimize
rnd = lambda: 10*numpy.random.random() - 5
g = lambda (x, y): f(x, y)
n_sample = 10000
minimum = np.array(
[minimize(g, x0=(rnd(), rnd()), bounds=((-5,5), (-5, 5)), method='SLSQP')['x']
for _ in range(n_sample)]
)
maximum = np.array(
[minimize(lambda x: -g(x), x0=(rnd(), rnd()), bounds=((-5,5), (-5, 5)), method='SLSQP')['x']
for _ in range(n_sample)]
)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(minimum[:, 0], minimum[:, 1], g((minimum[:, 0], minimum[:, 1])), c='r', alpha=0.5)
ax.scatter(maximum[:, 0], maximum[:, 1], g((maximum[:, 0], maximum[:, 1])), c='b', alpha=0.5)
g_value_in_loc_min = g((minimum[:, 0], minimum[:, 1]))
x_min = np.min(g_value_in_loc_min)
print x_min
def f1(x1, x2): return x1**2 + x2**2 - 0.5*x1*x2
def g(x1, x2): return x2**2 - x1**2 - 3
def u(x1): return 2*x1 - 0.5*x1*numpy.sqrt(x1**2 + 3) + 3
fig = plt.figure()
ax = fig.gca(projection='3d')
X, Y = numpy.linspace(left, right, 200), numpy.linspace(left, right, 200)
(X, Y), Z = numpy.meshgrid(X, Y), f1(X, Y)
ax.plot_surface(X, Y, Z, color='b')
X, Y = numpy.linspace(left, right, 100), numpy.linspace(left, right, 100)
(X, Y), Z = numpy.meshgrid(X, Y), g(X, Y)
ax.plot_surface(X, Y, Z, color='r')
Y = numpy.linspace(-10, 10, 1000)
pylab.plot(Y, u(Y))
y = Out[46]['x'][0]
x = y**2+3
print(x, y, f(x, y))