Advertisement
Guest User

Code

a guest
May 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.44 KB | None | 0 0
  1. %% This script draws a plot of the magnetic field for an infinitely long wire
  2.  
  3. %% Defining constants
  4. one_over_4pic2eps0 = 10^-7;
  5.  
  6. Current = 100; %Amp
  7.  
  8.  
  9. %% defining r and calculating the field strength
  10.  
  11. r = [0:2000];
  12. B = (2*Current*one_over_4pic2eps0)./r;
  13.  
  14. %% Plotting field strength;
  15.  
  16. figure(1);
  17.  
  18. plot(r,B);
  19. ylim([-1*10^-7, 2*10^-6]);
  20. xlabel('r [m]');
  21. ylabel('|B| [T]');
  22. title('Magnetic field strenght based on distance from infinite wire');
  23.  
  24. %% Defining vector field r infinite wire
  25. x_values = [-10:0.5:10];
  26. y_values = [-10:0.5:10];
  27.  
  28. [X_r, Y_r] = meshgrid(x_values, y_values);
  29. X_B = (one_over_4pic2eps0*2*Current).*(-Y_r./(X_r.^2+Y_r.^2));
  30. Y_B = (one_over_4pic2eps0*2*Current).*(X_r./(X_r.^2+Y_r.^2));
  31.  
  32.  
  33. %% plotting B field infinite wire
  34.  
  35. figure(2);
  36. %quiver(X_r, Y_r, X_r, Y_r); %% vector r
  37. quiver(X_r, Y_r, X_B, Y_B); %% vector r
  38. pbaspect([1 1 1]);
  39. xlim([-10 10]);
  40. ylim([-10 10]);
  41.  
  42. %% calculating for finite wire
  43.  
  44. X_UnitVecB = -Y_r./sqrt(X_r.^2+Y_r.^2);
  45. Y_UnitVecB =  X_r./sqrt(X_r.^2+Y_r.^2);
  46.  
  47. %% let the wire start at x=0 and go up to x=length
  48. length = 10;    % meters
  49. x_observer = length/2; % meters
  50. d_cable = 2; % meters
  51.  
  52. a = [-10:0.001:10]; % meters from wire
  53. theta1 = atan(x_observer./a);
  54. theta2 = atan((length-x_observer)./a);
  55.  
  56. mu_0 = 4*pi*(10^-7);
  57.  
  58. mag_B_1 = (mu_0*Current)./((4*pi).*a) .* (sin(theta1) + sin(theta2));
  59. a = a-d_cable;
  60. mag_B_2 = (mu_0*-Current)./((4*pi).*a) .* (sin(theta1) + sin(theta2));
  61.  
  62. figure(3);
  63. plot(a+(d_cable/2), abs(mag_B_1+mag_B_2));
  64. xlim([-5 5]);
  65. ylim([0 0.001]);
  66. %ylim([-1*10^-9 1*10^-7]);
  67.  
  68. %% quiver plot
  69. x_values = [-10:0.1:10];
  70. y_values = [-10:0.1:10];
  71.  
  72. [X1,Y1] = meshgrid(x_values+1, y_values);
  73. wire_1_unit_vecX = -Y1./sqrt(X1.^2+Y1.^2);
  74. wire_1_unit_vecY = X1./sqrt(X1.^2+Y1.^2);
  75.  
  76. [X2,Y2] = meshgrid(x_values-1, y_values);
  77. wire_2_unit_vecX = Y2./sqrt(X2.^2+Y2.^2);
  78. wire_2_unit_vecY = -X2./sqrt(X2.^2+Y2.^2);
  79.  
  80. a = sqrt((x_values+1).^2+(y_values).^2);
  81. theta1 = atan(x_observer./a);
  82. theta2 = atan((length-x_observer)./a);
  83. mag_B_1 = (mu_0*Current)./((4*pi).*a) .* (sin(theta1) + sin(theta2));
  84. a = sqrt((x_values-1).^2+(y_values).^2);
  85. theta1 = atan(x_observer./a);
  86. theta2 = atan((length-x_observer)./a);
  87. mag_B_2 = (mu_0*Current)./((4*pi).*a) .* (sin(theta1) + sin(theta2));
  88. unitVecX = wire_1_unit_vecX.*mag_B_1+wire_2_unit_vecX.*mag_B_2;
  89. unitVecY = wire_1_unit_vecY.*mag_B_1+wire_2_unit_vecY.*mag_B_2;
  90. [X, Y] = meshgrid(x_values, y_values);
  91. quiver(X, Y, unitVecX, unitVecY);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement