Advertisement
hiddenGem

B-field of a Long Wire

Jun 25th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.13 KB | None | 0 0
  1. %% B-field of a Long Wire
  2. % Calculates the magnitude of the B-field at point p.
  3.  
  4. % Constants
  5. muNaught = (4*pi)*10^-7                   %[T*m/A] Vacuum Permeability
  6.  
  7. % Inputs and Variables
  8. I = input('What is the current creating the field in Amps?\n');
  9. a = input('Coordinates or just radius?\n');
  10. switch a
  11.     case 1
  12.         R = input('Input the distance from point p to the wire in coordinate form in brackets [x,y]\n');
  13.         r = sqrt(R(1)^2+R(2)^2);
  14.     case 2
  15.         r = input('What is the radius in meters?\n');
  16. end
  17.  
  18. % Outputs and Equations
  19. B = muNaught*I/(2*pi*r);
  20. fprintf('The magnitude is %.3e T',B)
  21.  
  22. if a == 1
  23.     b = input('Would you like to calculate the x and y coordinates of the magnetic field? Y/N', 's');
  24.     if b == 'Y'
  25.         Bx = R(2)*B/r;
  26.         By = R(1)*B/r;
  27.         fprintf('The x and y coordinates are %.3e and %.3e', Bx, By)
  28.     end
  29. elseif a == 2
  30.     theta = input('What is angle to calculate the x and y coordinates?\n');
  31.     if theta == 0
  32.     else
  33.         Bx = sind(theta)*B/r;
  34.         By = cosd(theta)*B/r;
  35.         fprintf('The x and y coordinates are %.3e and %.3e', Bx, By)
  36.     end
  37. end
  38.  
  39. %{
  40. **IMPORTANT**
  41.     This code works under the assumption that the direction of the magnetic
  42.     field is downwards. If the direction of the magnetic field is upwards
  43.     then the calculation of the x and y coordinates will be reversed. You
  44.     could easily implement a selection structure that gives users the
  45.     option to choose which direction the of the arrow but I will not be
  46.     making this edit anytime soon
  47. ******
  48.  
  49. The Bx and By components are calculated by sin(theta) or cos(theta) * B.
  50.     Where the sin(theta) or cos(theta) is replaced with it's distance
  51.     equivalent x-coordinate or y-coordinate / r
  52. The user has the option to input the radius directly or use a coordinate
  53.     system if the user chooses to input a coordinate system they will be given
  54.     the option to calculate the x and y coordinates. If the user chooses to
  55.     input the radius directly they will have to input the degrees to
  56.     calculate the x and y coordinates
  57. If the user does not to want to calculate the x and y coordinates, when
  58.     given the choice to input an angle the user should just type in '0' to
  59.     cancel
  60. There is a function file wireBfield that will give the mag, Bx, and By that
  61.     you can find on my page.
  62. %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement