Advertisement
gronke

E/M Homework

Sep 22nd, 2012
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy
  2.  
  3. e0 = 8.85*10**-12 #C^2/Nm^2
  4.  
  5. x = 5. #meters
  6. y = 5. #meters
  7. z = 8. #meters
  8.  
  9. width = 0.001
  10.  
  11. dx = numpy.arange(-1,1,width)
  12.  
  13. intx = numpy.zeros(dx.size)
  14. inty = numpy.zeros(dx.size)
  15. intz = numpy.zeros(dx.size)
  16.  
  17. #Calculating Electric Field
  18. for i in range(dx.size):
  19.     lamb = (6.0*10**-6)*(1-dx[i]**2)
  20.     intx[i] = width*(1./(4.*numpy.pi*e0))*lamb*(x-dx[i])/(((x-dx[i])**2 + y**2 + z**2)**(3./2))
  21.     inty[i] = width*(1./(4.*numpy.pi*e0))*lamb*(y)/(((x-dx[i])**2 + y**2 + z**2)**(3./2))
  22.     intz[i] = width*(1./(4.*numpy.pi*e0))*lamb*(z)/(((x-dx[i])**2 + y**2 + z**2)**(3./2))
  23.  
  24. x_sum = numpy.sum(intx)
  25. y_sum = numpy.sum(inty)
  26. z_sum = numpy.sum(intz)
  27.  
  28. potx = numpy.zeros(dx.size)
  29.  
  30. #Calculating Electrostatic Potential
  31. for i in range(dx.size):
  32.     lamb = (6.0*10**-6)*(1-dx[i]**2)
  33.     potx[i] = width*(1./(4.*numpy.pi*e0))*lamb/(((x-dx[i])**2 + y**2 + z**2)**(1./2))
  34.  
  35. potsum = numpy.sum(potx)
  36.  
  37. potplus = numpy.zeros(dx.size)
  38. potminus = numpy.zeros(dx.size)
  39. dv = 0.001
  40.  
  41. #Calculating Electrostatic Potential at small points
  42. for i in range(dx.size):
  43.     lamb = (6.0*10**-6)*(1-dx[i]**2)
  44.     potplus[i] = width*(1./(4.*numpy.pi*e0))*lamb/((((x+dv)-dx[i])**2 + y**2 + z**2)**(1./2))
  45.     potminus[i] = width*(1./(4.*numpy.pi*e0))*lamb/((((x-dv)-dx[i])**2 + y**2 + z**2)**(1./2))
  46.  
  47. Etest = -(numpy.sum(potplus) - numpy.sum(potminus))/(2*dv)
  48.  
  49. Error = 100*numpy.abs(x_sum-Etest)/Etest
  50.  
  51. print ('Coordinates of E field are '),x_sum,(' x^ ,'),y_sum,(' y^ ,'),z_sum,(' z^')
  52. print ('Electrostatic potential is '),potsum
  53. print ('Approximation of Electric Field at X is '),Etest
  54. print ('Percent error between approximation and actual is '),Error,('%')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement