Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. import pylab
  2. from mpl_toolkits.mplot3d import Axes3D
  3. import numpy as np
  4. from scipy.optimize import minimize
  5. import sympy as sp
  6. from scipy.optimize import rosen, rosen_der, rosen_hess, rosen_hess_prod
  7. from scipy.optimize import Bounds
  8.  
  9. def makeData():
  10.     x = np.arange(-1, 1, 0.1)
  11.     y = np.arange(-1, 1, 0.1)
  12.     xgrid, ygrid = np.meshgrid(x, y)
  13.     zgrid = -xgrid**2 + xgrid*ygrid - 4*ygrid**2
  14.     return xgrid, ygrid, zgrid
  15.  
  16. def makeDataG():
  17.     x = np.arange(-1, 1, 0.1)
  18.     xgrid= np.meshgrid(x)
  19.     y1grid = 0.5 * xgrid - 1
  20.     y2grid = 0.5* xgrid + 0.6
  21.     return xgrid, y1grid, y2grid
  22.  
  23. def f(x):
  24.     return -1 * x[0]*x[0] + x[0]*x[1] - 4*x[1]*x[1]
  25.  
  26.  
  27.  
  28. eq_cons = {'type': 'eq',
  29.            'fun': lambda x: np.array ([x[1] - 0.5 *x[0] + 1, 0.5*x[0] - x[1] + 0.6]),
  30.            'jac': lambda x: np.array ([[-0.5, 1.0],[0.5, -1.0]])
  31.           }
  32.  
  33. x1, y1, z = makeData()
  34. x2, y2, y3 = makeDataG()
  35.  
  36. fig = pylab.figure()
  37. axes = Axes3D(fig)
  38.  
  39. fig1 = pylab.figure()
  40. axes1 = Axes3D(fig1)
  41.  
  42.  
  43. bnds = Bounds ([-np.inf, -np.inf], [np.inf, np.inf])
  44.  
  45. axes.plot_surface(x1, y1, z)
  46. axes1.plot_surface(x2, y2)
  47. axes1.plot_surface(x2, y3)
  48. x0 = np.array([-1.0, -1.0])
  49. res = minimize(rosen, x0, method='SLSQP', jac=rosen_der,
  50.                constraints=[eq_cons], options={'ftol': 1e-9, 'disp': True},
  51.                bounds=bnds)
  52.  
  53.  
  54. # axes.scatter(t.x[0], t.x[1], f(t.x), s = 480, c = 'red')
  55.  
  56. print('x= ' + str(t.x[0]) + ', y= ' + str(t.x[1]) + ', z(x,y)=' + str(f(t.x)))
  57.  
  58. pylab.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement