0

我有一个方程如下:

y = x^T * A * x + b^T * x + c

其中 x, b, c 是 n 空间中的向量,A 是 nxn 矩阵。

我可以在 matplotlib 中绘制线性方程,但不确定矩阵方程如何(如果可能)也显示在 3d 图中。

我尝试使用以下代码,A 是矩阵,w、c 和 b 是列向量。X 和 Y 是网格,Z 是解。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
# if using a Jupyter notebook, include:
%matplotlib inline

fig = plt.figure(figsize=(10,6))
ax1 = fig.add_subplot(111, projection='3d')

n = 50
i = -5.0
j = 5.0

A = np.random.randint(i, j, size=(n, n))
w = np.random.randint(i, j, size=(n, 1))
c = b = np.random.randint(i, j, size=(n, 1))

X,Y = np.meshgrid(n,n)
Z = w.T*A*w + b.T*w + c

mycmap = plt.get_cmap('gist_earth')
surf1 = ax1.plot_surface(X, A, Z, cmap=mycmap)
fig.colorbar(surf1, ax=ax1, shrink=0.5, aspect=10)

plt.show()

由此产生的情节似乎不是一个令人满意的情节。

4

1 回答 1

1

您的代码中有两个问题:1)meshgrid使用不正确(它需要两个数组,而不是两个整数);2)在表面情节中,您使用X, A, Z而不是X, Y, Z--X, A, Z会起作用,并且可能有意义,但我猜这不是您的意图。

这是一个有效的解决方案:

在此处输入图像描述

fig = plt.figure(figsize=(10,6))
ax1 = fig.add_subplot(111, projection='3d')

n = 10
i = -5.0
j = 5.0

A = np.random.randint(i, j, size=(n, n))
w = np.random.randint(i, j, size=(n, 1))
c = b = np.random.randint(i, j, size=(n, 1))

X,Y = np.meshgrid(np.arange(n),np.arange(n))
Z = w.T*A*w + b.T*w + c

mycmap = plt.get_cmap('gist_earth')
surf1 = ax1.plot_surface(X, Y, Z, cmap=mycmap)
fig.colorbar(surf1, ax=ax1, shrink=0.5, aspect=10)
于 2020-05-05T18:12:43.660 回答