Tom
Tom

Reputation: 55

Plotting 3D Vector Field

I am trying to plot a 3D vector field. I used the following example as guidance:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np

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

x, y, z = np.meshgrid(np.arange(-0.8, 1, 0.2),
                      np.arange(-0.8, 1, 0.2),
                      np.arange(-0.8, 1, 0.8))

u = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z)
v = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z)
w = (np.sqrt(2.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) *
     np.sin(np.pi * z))

ax.quiver(x, y, z, u, v, w, length=0.1)

plt.show()

This example comes from the matplotlib examples library. However, I wanted to try a different function to replace u, v and w like this:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np

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

x,y,z = np.meshgrid(np.arange(-0.1,0.005,0.1),np.arange(-0.1,0.005,0.1),np.arange(-0.1,0.005,0.1))
VolMag = 3.218E-6 #Volume of magnet in experiment in m^3
BR = np.sqrt(x**2 + y**2 + z**2)
MagMoment = np.array([0,0,(BInput*VolMag)/u0])
Bx = (u0/(4*np.pi))*(3*x*MagMoment*z)/BR**5
By = (u0/(4*np.pi))*(3*y*MagMoment*z)/BR**5
Bz = (u0/(4*np.pi))*((3*z*MagMoment*(z**2)/BR**5 - MagMoment/BR**3)

ax.quiver(x,y,z,Bx,By,Bz,length=0.1)
plt.show()

This is giving me an "invalid syntax" error for line 16. Why is this? I only changed the function and some of the names of the definitions.

Upvotes: 0

Views: 264

Answers (1)

Will Meyers
Will Meyers

Reputation: 165

It looks like your missing a closing parenthesis on this line.

Bz = (u0/(4*np.pi))*((3*z*MagMoment*(z**2)/BR**5 - MagMoment/BR**3)

should be

Bz = (u0/(4*np.pi))*((3*z*MagMoment*(z**2)/BR**5 - MagMoment/BR**3))

Upvotes: 1

Related Questions