我目前正在解决一个优化问题,我想用 plot3d 工具包绘制我找到的解决方案。假设我试图找到函数的最小值F
。让 x 的值最小化F x*
。我想将生成的曲面绘制F
为网格,并在同一个图上打印F : F(x*)
.
我为此目的使用此代码:
x = [a / 10 - 5 for a in range(100)]
y = [a / 10 - 5 for a in range(100)]
Z = []
for i in range(len(x)):
Z.append([0] * len(x))
Z = array(Z)
for i in range(len(x)):
for j in range(len(x)):
Z[i, j] = F([x[i], y[j]])
X = identity(len(x))
for i in range(len(x)):
X[:, i] = x
Y = X.T
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X, Y, Z, rstride=2, cstride=2, color='blue')
ax = fig.gca(projection = '3d')
ax.plot(sol.x[0], sol.x[1], [F(sol.x)], color='green')
plt.show()
sol.x
最小化的值在哪里F
(为了简单起见sol.x=x*
)。此代码返回以下错误:
Traceback (most recent call last):
File "projet2.py", line 103, in <module>
ax.plot(sol.x[0],sol.x[1],[f1(sol.x)],color='green')
File "/usr/lib/pymodules/python2.7/mpl_toolkits/mplot3d/axes3d.py", line 1296, in plot
len(xs) == len(args[0]) :
TypeError: object of type 'numpy.float64' has no len()
有没有人知道如何解决这个问题?
谢谢
昆汀