0% found this document useful (0 votes)
35 views3 pages

'Enter The Current of The Loop (A) : ' 'Enter Radius of The Loop (M) : '

The document is a MATLAB script that calculates and visualizes the magnetic field generated by a circular current loop in the yz plane. It prompts the user for the current and radius, computes the magnetic field at various grid points, and then plots the field direction using a quiver plot. The script includes loops to define grid points, calculate segment vectors, and evaluate the magnetic field components at each observation point.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views3 pages

'Enter The Current of The Loop (A) : ' 'Enter Radius of The Loop (M) : '

The document is a MATLAB script that calculates and visualizes the magnetic field generated by a circular current loop in the yz plane. It prompts the user for the current and radius, computes the magnetic field at various grid points, and then plots the field direction using a quiver plot. The script includes loops to define grid points, calculate segment vectors, and evaluate the magnetic field components at each observation point.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

clear; clc; %clear command window and initial variables' values

I = input('Enter the current of the loop (A): '); % Enter current


Radius = input('Enter radius of the loop (m): '); % Enter radius
Constant = 4*pi*1e-7/(4*pi) * I; % Constant
NGrid = 5; % Number of grid points for plots
xMax = 5; % Limits for graphics
yMax = xMax; % Limits for graphics
zMax = xMax; % Limits for graphics
fprintf('Field plotted from x = %g m to x = %g m\n',-xMax,xMax);
fprintf('Field plotted from y = %g m to y = %g m\n',-yMax,yMax);
fprintf('Field plotted from z = %g m to z = %g m\n',-zMax,zMax);
%@ Loop to indentify all grid points for plots
xObs=zeros(NGrid,NGrid,NGrid); % Preallocate array
yObs=zeros(NGrid,NGrid,NGrid); % Preallocate array
zObs=zeros(NGrid,NGrid,NGrid); % Preallocate array
for i=1:NGrid
for j=1:NGrid
for k=1:NGrid
xObs(i,j,k) = -xMax + (i-1)/(NGrid-1)*(2*xMax); % x values to plot
yObs(i,j,k) = -yMax + (j-1)/(NGrid-1)*(2*yMax); % y values to plot
zObs(i,j,k) = -zMax + (k-1)/(NGrid-1)*(2*zMax); % z values to plot
end
end
end
%@ Loop over the segments in the current loop in yz plane
NSegments = 20;
dlx=zeros(NSegments,1); % Preallocate array
dly=zeros(NSegments,1); % Preallocate array
dlz=zeros(NSegments,1); % Preallocate array
xc=zeros(NSegments,1); % Preallocate array
yc=zeros(NSegments,1); % Preallocate array
zc=zeros(NSegments,1); % Preallocate array
for b=1:NSegments
%@ Compute location of the endpoints of a segment
theta1 = 2*pi*(b-1)/NSegments;
x1 = 0;
y1 = Radius*cos(theta1);
z1 = Radius*sin(theta1);
theta2 = 2*pi*b/NSegments;
x2 = 0;
y2 = Radius*cos(theta2);
z2 = Radius*sin(theta2);
%@ Compute components of segment vector dl
dlx(b) = x2-x1;
dly(b) = y2-y1;
dlz(b) = z2-z1;
%@ Compute the location of the midpoint of a segment
xc(b) = (x2+x1)/2;
yc(b) = (y2+y1)/2;
zc(b) = (z2+z1)/2;
end

%@ Loop over all grid points and evaluate B(x,y,z) on grid


BDirx=zeros(NGrid,NGrid,NGrid); % Preallocate array
BDiry=zeros(NGrid,NGrid,NGrid); % Preallocate array
BDirz=zeros(NGrid,NGrid,NGrid); % Preallocate array
for i=1:NGrid
for j=1:NGrid
for k=1:NGrid
% Initialize B to zero
Bx = 0;
By = 0;
Bz = 0;
%@ Loop over the segments in the loop
for l=1:NSegments
%@ Compute components of the r vector (vector between segment on loop and
observation point)
rx = xObs(i,j,k) - xc(l);
ry = yObs(i,j,k) - yc(l);
rz = zObs(i,j,k) - zc(l);
% Observation points are in a three dimensional Cartesian coordinate system
%@ Compute r^3 from r vector
r3 = sqrt(rx^2 + ry^2 + rz^2)^3;
%@ Compute x and y components of cross product dl X r
dlXr_x = dly(l)*rz - dlz(l)*ry;
dlXr_y = dlz(l)*rx - dlx(l)*rz;
dlXr_z = dlx(l)*ry - dly(l)*rx;

%@ Increment sum of x and y components of magnetic field


Bx = Bx + Constant*dlXr_x/r3;
By = By + Constant*dlXr_y/r3;
Bz = Bz + Constant*dlXr_z/r3;
end
%@ Compute normalized vectors of magnetic field direction
BMag = sqrt(Bx^2 + By^2 + Bz^2);
BDirx(i,j,k) = Bx/BMag;
BDiry(i,j,k) = By/BMag;
BDirz(i,j,k) = Bz/BMag;
end
end
end
%@ Plot magnetic field direction as a quiver (arrow) plot
xlim([-10 10]);
ylim([-10 10]);
zlim([-10 10]);
quiver3(xObs,yObs,zObs,BDirx,BDiry,BDirz); % Draw arrows for B field
hold on; % Plot new ones and still keep the previous.
%@ Plot the circular current (a loop in yz plane)
t = 0:pi/360:10*pi;
plot3(0*t, Radius*sin(t),Radius*cos(t));
if I>0 % Mark the location of the current
plot3(0,Radius,0,'b^');
plot3(0,-Radius,0,'rv');
else
plot3(0,Radius,0,'bv');
plot3(0,-Radius,0,'r^');
end
title('Magnetic field direction for a circular current in yz plane');
xlabel('x'); % x-axis
ylabel('y'); % y-axis
zlabel('z'); % z-axis
hold off;

You might also like