Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- import math
- # make an x axis from -5 to 5 with 100 grid points
- x_coords = np.linspace(-5,5,100)
- y_coords = np.linspace(-5,5,100)
- X , Y = np.meshgrid(x_coords,y_coords)
- #define constants for formula
- permitivity= 8.85 * 10**-12
- k = 1.0 / (4.0 * math.pi * permitivity)
- q = 1.0 * 10**-9
- charge_location = np.array([0,0])
- def calc_efield (x_coord,y_coord):
- # declare two element vector for the r_vector
- r_vector = np.array([0,0])
- # calculate r vector by subtracting x coords and y coords
- r_vector[0] = x_coord - charge_location[0]
- r_vector[1] = y_coord - charge_location[1]
- #find the magnitude of the vector
- r_mag = np.linalg.norm(r_vector)
- #calculate the kq/r^2 part
- constant = k*q / r_mag ** 2
- #calculate the unit vector as the r_vector over its magnitude
- unit_vector = r_vector
- unit_vector = unit_vector / r_mag
- #calculate the electric field by multiplying the constant part with the unit vector
- e_field = unit_vector
- e_field = e_field * constant
- return e_field
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement