You are on page 1of 2

Applied Physics Department, FAS, HCMUT Matlab Projects – Physics 1

Project 2:
Determining the magnetic field of a circular current
using Biot-Savart's law
1. Content
The magnetic field of any current distribution (C) can be determined by the Biot-Savart’s law
using the following equation:

This project requires students to use Matlab to calculate the magnetic field of a circular current
using the above expression by dividing the circle into small current segments and adding the
magnetic field values created by each of the segments above at a certain position. Then, use the
calculated magnetic field values to plot the field lines of mentioned magnetic field.

2. Requirements
1) Students should have basic programming knowledge of MATLAB.
2) Learn about symbolic calculation and graphical interpretation in MATLAB.

3. Tasks
Write Matlab program to:
1) Enter the circular current radius.
2) Divide the circle into small current segments and add the magnetic field generated by each
segment at a certain position (refer to Matlab code below).
3) Draw the graph representing the field lines of mentioned magnetic field (use the quiver
command - draw the arrow of the line).
Note: Students can use other approaches.
Submitting report has to contain text explaining the content of the program and the entire code
verified to run properly in Matlab.

4. References:
A. L. Garcia and C. Penland, MATLAB Projects for Scientists and Engineers, Prentice Hall,
Upper Saddle River, NJ, 1996. http://www.algarcia.org/fishbane/fishbane.html. Or
https://www.mathworks.com/matlabcentral/fileexchange/2268-projects-for-scientists-and-
engineers

* HINTS:
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);
for i=1:NGrid
xObs(i) = -xMax + (i-1)/(NGrid-1)*(2*xMax); % x values to plot
yObs(i) = -yMax + (i-1)/(NGrid-1)*(2*yMax); % y values to plot
end

%@ Loop over the segments in the current loop in yz plane


NSegments = 20;
for k=1:NSegments
%@ Compute location of the endpoints of a segment
theta1 = 2*pi*(k-1)/NSegments;
1/1
Applied Physics Department, FAS, HCMUT Matlab Projects – Physics 1

x1 = 0;
y1 = Radius*cos(theta1);
z1 = Radius*sin(theta1);
theta2 = 2*pi*k/NSegments;
x2 = 0;
y2 = Radius*cos(theta2);
z2 = Radius*sin(theta2);

%@ Compute components of segment vector dl


dlx(k) = x2-x1;
dly(k) = y2-y1;
dlz(k) = z2-z1;

%@ Compute the location of the midpoint of a segment


xc(k) = (x2+x1)/2;
yc(k) = (y2+y1)/2;
zc(k) = (z2+z1)/2;

end

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


for i=1:NGrid
for j=1:NGrid

Bx = 0; By = 0; % Initialize B to zero

%@ Loop over the segments in the loop


for k=1:NSegments

%@ Compute components of the r vector (vector between


%% segment on loop and observation point)
rx = xObs(j) - xc(k);
ry = yObs(i) - yc(k);
rz = -zc(k); % Observation points are in xy plane

%@ 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(k)*rz - dlz(k)*ry;
dlXr_y = dlz(k)*rx - dlx(k)*rz;

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


Bx = Bx + Constant*dlXr_x/r3;
By = By + Constant*dlXr_y/r3;

end

%@ Compute normalized vectors of magnetic field direction


BMag = sqrt(Bx^2 + By^2);
BDirx(i,j) = Bx/BMag;
BDiry(i,j) = By/BMag;

end
fprintf('Calculation %g%% complete\n',100*i/NGrid);
end

1/2

You might also like