1
votes

I would like to plot functions $g(x)=x^Tx$ and $f(x)=x^TMx$ while $x \in \mathbb{R}^n$ and $M \in \mathbb{R}^{nxn}$. -Sorry it's seems like latex don't work here-

Function g is easy to plot:

import numpy as np 
import matplotlib.pyplot as plt 

data = np.linspace(-4, 4, 20)
x_1, x_2  = np.meshgrid(data, data, indexing="ij")

gx = np.zeros_like(x_1)
for i in range(data.shape[0]):
    for j in range(data.shape[0]):
        x = np.array([x_1[i,j], x_2[i,j]])
        # g = x^Tx
        g = x.T  @ x 
        gx[i,j] = g 

fig = plt.figure(figsize=(15,8))
ax = fig.add_subplot(121, projection="3d")
surf = ax.plot_surface(x_1, x_2, gx)
ax.set_xlabel("x_1")
ax.set_ylabel("x_2")
ax.set_zlabel("f")

plt.show()

enter image description here

But function f is a problem because I don't know how to combine the mesh with the matrix, is there a smart way to solve this problem?