fork download
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4.  
  5. # Define the function
  6. def f(x, y):
  7. return 9 - 2*x + 4*y - x**2 - 4*y**2
  8.  
  9. # Generate x and y values
  10. x = np.linspace(-5, 5, 100)
  11. y = np.linspace(-5, 5, 100)
  12. X, Y = np.meshgrid(x, y)
  13. Z = f(X, Y)
  14.  
  15. # Create 3D plot
  16. fig = plt.figure()
  17. ax = fig.add_subplot(111, projection='3d')
  18. ax.plot_surface(X, Y, Z, cmap='viridis')
  19.  
  20. # Set labels
  21. ax.set_xlabel('x')
  22. ax.set_ylabel('y')
  23. ax.set_zlabel('f(x, y)')
  24.  
  25. # Show plot
  26. plt.show()
Success #stdin #stdout 0.76s 62216KB
stdin
Standard input is empty
stdout
Standard output is empty