I want to 3D plot a f(x,y) function that uses several matrix operation. I want to look for possible maximum and minimums. The reason of this matrix operation is that sometimes this type of functions can be expressed in a cleaner an more compact form. However, I have problems trying to fix some of the errors. In the construction of that function f(x,y), I have to turn a 2-dimensional vector (x,y) into a 3-dimensional vector of the form (x,y,0) or (x,0,y), or (0,x,y). Basically the errors come from the use of these two type of functions:
function 1:
a = np.array([[1],
[3]])
def toy_function1(x,y):
u = np.asarray((x,y))
return np.matmul(u, a )
x = np.linspace(0, 5, 4)
y = np.linspace(0, 5, 3)
x, y = np.meshgrid(x,y)
z = toy_function1(x,y)
plt.contour(x, y, y, 20, cmap='RdGy');
with error
ValueError Traceback (most recent call last)
<ipython-input-27-d1951bd0ff32> in <module>()
9 x, y = np.meshgrid(x,y)
10
---> 11 z = toy_function1(x,y)
12
13 plt.contour(x, y, y, 20, cmap='RdGy');
<ipython-input-27-d1951bd0ff32> in toy_function1(x, y)
3 def toy_function1(x,y):
4 u = np.asarray((x,y))
----> 5 return np.matmul(u, a )
6
7 x = np.linspace(0, 5, 4)
ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 2 is different from 4)
function 2
a = np.array([[1],
[2],
[3]])
def toy_function2(x,y):
u = np.insert((x,y),0,0).reshape(1,3)
return np.matmul(u, a)
x = np.linspace(0, 5, 4)
y = np.linspace(0, 5, 3)
x, y = np.meshgrid(x,y)
z = toy_function2(x,y)
plt.contour(x, y, y, 20, cmap='RdGy');
with error
ValueError Traceback (most recent call last)
<ipython-input-56-47a2104dff1b> in <module>()
13 x, y = np.meshgrid(x,y)
14
---> 15 z = toy_function2(x,y)
16
17 plt.contour(x, y, y, 20, cmap='RdGy');
<ipython-input-56-47a2104dff1b> in toy_function2(x, y)
5
6 def toy_function2(x,y):
----> 7 u = np.insert((x,y),0,0).reshape(1,3)
8 return np.matmul(u, a)
9
ValueError: cannot reshape array of size 25 into shape (1,3)