import matplotlib.pyplot as plt
from matplotlib import cm
from numpy import nan, linspace, meshgrid
x1=linspace(0,2,50)
x2=linspace(0,2,50)
x1, x2 = meshgrid(x1, x2)
f=((x1 1.5)**2 5*(x2-1.7)**2)*((x1-1.4)**2 0.6*(x2-0.5)**2)
f[-x1<=0] = nan
f[-x2<=0] = nan
f[3*x1-x1*x2 4*x2-7<=0] = nan
f[2*x1 x2-3<=0] = nan
f[3*x1-4*x2**2-4*x2<=0] = nan
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(x1, x2, f, cmap=cm.jet,linewidth=0, antialiased=False,label="Kurva $f(x_1,x_2)$",alpha=1)
ax.set_xlabel('$x_1$')
ax.set_ylabel('$x_2$')
ax.set_zlabel('$f(x_1,x_2)$')
plt.show()

我想f=((x1 1.5)**2 5*(x2-1.7)**2)*((x1-1.4)**2 0.6*(x2-0.5)**2)
用給定的 5 個約束進行繪圖:
-x1<=0-x2<=03*x1-x1*x2 4*x2-7<=02*x1 x2-3<=03*x1-4*x2**2-4*x2<=0
當我運行上面的代碼時,圖中什么都沒有出現。我的錯誤是什么?如何解決?
uj5u.com熱心網友回復:
您將x1and定義x2為:
x1=linspace(0,2,50)
x2=linspace(0,2,50)
所以它們的每個值都在0和之間2。然后你編輯它們:
x1, x2 = meshgrid(x1, x2)
此時,x1andx2不再是陣列,而是矩陣。無論如何,和以前一樣,它們中的每個元素仍然介于0和之間2。
如果您應用此過濾器:
f[-x1<=0] = nan
那么 的每個元素都f變為nan,因為 的所有元素x1都是正數,所以運算式對的每個元素-x1<=0求值為。相同的邏輯適用于過濾器:Falsex1
f[-x2<=0] = nan
f因此,確實, is的每個元素nan和情節都是空的。
如果您嘗試洗掉上述兩個過濾器,則會得到:
f[3*x1-x1*x2 4*x2-7<=0] = nan
f[2*x1 x2-3<=0] = nan
f[3*x1-4*x2**2-4*x2<=0] = nan

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/419618.html
標籤:
