How to convert a np.meshgrid() function to a complex function?

139 Views Asked by At

I'm trying to create a script that plot a 3D magnitude surface graph of a transfer function using python.

I used as an example for my code, this other code made for matlab that works.

#Example using predefined functions for num and den
num = s+8
den = s**2+s+5
[X, Y] = meshgrid(-10:0.1:10)
s = X+Y*1i
res = (polyval(num, s))./(polyval(den,s))
figure(1)
mesh(X,Y,abs(res))

Converting that code to python I made:

num = s+8
den = s**2+s+5

fig = plt.figure()
ax = plt.axes(projection='3d')

x = np.arange(-10, 10, 0.1)
y = np.arange(-10, 10, 0.1)
X,Y=np.meshgrid(x,y);
s = X+Yj
Z= abs(np.polyval([num],s)/np.polyval([den],s))
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False)

# Customize the z axis.
ax.set_zlim(-1.01, 1.01)
ax.zaxis.set_major_locator(LinearLocator(10))

# A StrMethodFormatter is used automatically
ax.zaxis.set_major_formatter('{x:.02f}')

# Add a color bar which maps values to colors.
fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

However I'm receiving this error when I try to compile my python code.

"NameError: name 'Yj' is not defined"

How should I convert my "Y", which is a np.meshgrid, to a complex variable so I can use it with np.polyval later?

or I could use another solution for my Z which does not require defining s as a complex variable, except I don't know how to do that.

I have tried doing this:

...
Y = [complex(*a) for a in Y]
s = X+Yj

Nonetheless I get this error: TypeError: complex() takes at most 2 arguments (200 given)


EDIT1: I found out how to fix my problem with the complex variable for Y.

s = X + 1j * Y

Writing "s" like that does work, but now I have another problem with the line...

Z= abs(np.polyval([num],s)/np.polyval([den],s))
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, linewidth=0, antialiased=False)

Which give me the error: "raise TypeError("Cannot convert expression to float") TypeError: Cannot convert expression to float"

I believe that error has something to do with the "/" operand in the Z equation, since on matlab it is used "./" instead of "/", but the same operand does not work for Python.

0

There are 0 best solutions below