Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import math
  4.  
  5.  
  6. # make an x axis from -5 to 5 with 100 grid points
  7. x_coords = np.linspace(-5,5,100)
  8. y_coords = np.linspace(-5,5,100)
  9.  
  10. X , Y = np.meshgrid(x_coords,y_coords)
  11.  
  12. #define constants for formula
  13. permitivity= 8.85 * 10**-12
  14. k = 1.0 / (4.0 * math.pi * permitivity)
  15. q = 1.0 * 10**-9
  16. charge_location = np.array([0,0])
  17.  
  18.  
  19. def calc_efield (x_coord,y_coord):
  20.    
  21.     # declare two element vector for the r_vector
  22.     r_vector = np.array([0,0])
  23.    
  24.     # calculate r vector by subtracting x coords and y coords
  25.     r_vector[0] = x_coord - charge_location[0]
  26.     r_vector[1] = y_coord - charge_location[1]
  27.    
  28.     #find the magnitude of the vector
  29.     r_mag = np.linalg.norm(r_vector)
  30.    
  31.     #calculate the kq/r^2 part
  32.     constant = k*q / r_mag ** 2
  33.    
  34.     #calculate the unit vector as the r_vector over its magnitude
  35.     unit_vector = r_vector
  36.     unit_vector = unit_vector / r_mag
  37.    
  38.     #calculate the electric field by multiplying the constant part with the unit vector
  39.     e_field = unit_vector
  40.     e_field = e_field * constant
  41.    
  42.    
  43.     return e_field
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement