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))