1
繪製的作用下: (X1 - 3)^ 2 +(X2 - 2)^ 2matplotlib,如何繪製3D 2可變功能的給定條件
隨着約束:
- X1^2 - ×2 - 3 < = 0
- X2 - 1 < = 0
- -x1 < = 0
該方程式也可以找到here。
我試圖解決這個圖形使用matplotlib
但使用其中缺少第一條件下面的代碼(the question i found that helped me with the code)結束了與上述曲線圖。
import matplotlib.pyplot as plt
from numpy import arange
from pylab import meshgrid
# function to be plotted
def z_func(a, b):
return (a - 3) * (a - 3) + (b - 2) * (b - 2)
x1 = arange(15.0, 0, -0.1) # x1 >= 0 according to given conditions
x2 = arange(-15.0, 1, 0.1) # x2 <= 1 according to given conditions
X1,X2 = meshgrid(x1, x2)
Z = z_func(X1, X2)
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X1, X2, Z, rstride=1, cstride=1, cmap=cm.RdBu,linewidth=0, antialiased=False)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_zlabel('z-axis')
ax.view_init(elev=25, azim=-120)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
以哪種方式修改上述代碼以考慮第一個條件?
感謝