Problem using quiver function to plot magnetic field outside of a circular loop

19 views (last 30 days)
Hey guys, I'm having trouble getting the quiver function to display the magnetic field in the xz-plane outside of a circular loop of current.
Here are the functions I'm using to calculate and collate the field's component vectors at every point. I think these are working properly. The variable max is used to set the number of gridsteps.
function [ bx,by,bz ] = calcfield( max,x,y,z )
%Calculates the field inside a Helmholtz coil
radius = .5;
dtheta = 2*pi/20;
bx = 0;
by = 0;
bz = 0;
for theta = 0:dtheta:2*pi-dtheta
dlx = -radius*dtheta*sin(theta);
dly = radius*dtheta*cos(theta);
rx = x - radius*cos(theta);
ry = y - radius*sin(theta);
rz = z;
r = sqrt(rx^2+ry^2+rz^2);
if r > 1/max % avoids wire
bx = bx + dly * rz/(r^3);
by = by - dlx * rz/(r^3);
bz = bz + (dlx*ry-dly*rx)/(r^3);
end
end
end
function [ field ] = findfield( max )
%Finds the field in the x-z plane
A = [1:2*max+1];
D = [1:2*max+1];
field = cat(3,A,D);
for i = 1:2*max+1
for k = 1:2*max+1
[bx,by,bz]=calcfield(max,(i-max-1)/(max),0,(k-max-1)/(max));
field(1,i,k) = bx;
field(2,i,k) = by;
field(3,i,k) = bz;
end
end
end
Now, this will give us the field between -1 and 1 in both the x and z directions. If we use max = 20 and then use the squeeze function, we can get the vector components as arrays mapped to coordinates.
EDU>> field = findfield(20);
EDU>> Bx = squeeze(field(1,:,:));
EDU>> Bz = squeeze(field(3,:,:));
Now we need some arrays of coordinates to match these.
EDU>> x = linspace(-1,1,41);
EDU>> z = linspace(-1,1,41)';
The position 0 will be the 21st element for each of these.
On the z-axis, the Bz component will be the only contributor and should always be positive. We can check this latter part easily.
EDU>> Bz(21,:)
ans =
Columns 1 through 7
1.1240 1.2696 1.4393 1.6379 1.8708 2.1448 2.4676
Columns 8 through 14
2.8483 3.2970 3.8249 4.4429 5.1605 5.9833 6.9092
Columns 15 through 21
7.9232 8.9918 10.0583 11.0426 11.8484 12.3802 12.5664
Columns 22 through 28
12.3802 11.8484 11.0426 10.0583 8.9918 7.9232 6.9092
Columns 29 through 35
5.9833 5.1605 4.4429 3.8249 3.2970 2.8483 2.4676
Columns 36 through 41
2.1448 1.8708 1.6379 1.4393 1.2696 1.1240
Good! We also see that it's greatest at the origin (center of the loop) and decreases with distance.
So now I want to plot this, so I use quiver(x,z,Bx,Bz), and get a graph that shows the magnetic field immediately increase in magnitude and change direction at (0,.5) and (0,-.5). Obviously, I'm misusing the quiver function in some way, but I have no idea what to do. Any help would be most appreciated. (Note: I have also input the coordinates using the meshgrid function and gotten the same result.)

Answers (0)

Categories

Find more on Vector Fields in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!