2

I am trying to plot the electric field generated when an spheric conductor is placed in an homogeneous electric field. I based my code on this question to use np.where function to filter out the unitary circle. But instead of that, all of the field at -1< x<1 is filtered out.

Image I got

My code is shown below, how can I fix this?

import sys
import numpy as np
import matplotlib.pyplot as plt
import scipy.constants as spc

def E(x, y):
    """due to an spheric conductor set on a homogeneous electric field"""
    E_x = 3*x*y/((y**2+x**2)**2.5)
    E_y = 3*y*y/((y**2+x**2)**2.5)-1/((y**2+x**2)**1.5)+1
    return E_x, E_y

# Grid of x, y points
n= 100
a=2
y = np.linspace(-a, a, n)
x = np.linspace(-a, a, n)
X, Y = np.meshgrid(x, y)
r=(x**2+y**2)**0.5
circle = r>=1
Ex, Ey = E(X,Y)
Ex, Ey = np.where(circle,Ex,0), np.where(circle,Ey,0)

#plot
fig = plt.figure()
ax = fig.add_subplot(111)

color = 2 * np.log(np.hypot(Ex, Ey))
ax.streamplot(x, y, Ex, Ey, color=color, linewidth=1, cmap=plt.cm.inferno,
              density=1, arrowstyle='->', arrowsize=1.5)

ax.set_ylabel('$y$')
ax.set_xlabel('$x$')
ax.set_ylim(-a,a)
ax.set_xlim(-a,a)
ax.set_aspect('equal')
plt.show()

1 Answer 1

1

When you create your r array to use in the circle filter, you are using the 1D x and y arrays, when you should be using the 2D X and Y arrays.

Change that line from

r = (x**2 + y**2)**0.5

to

r = (X**2 + Y**2)**0.5

and it works as expected

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Oh I totally ignored that small detail. Thank you very much for pointing that out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.