You are on page 1of 1

% This script allows the user to specify a current

% directed out of the page (+z direction) that lies on the origin,
% is assumed infinite, and points in the z direction
% and plot the vector magnetic field in the xy-plane
%
%
% inputs: I (value of the curent), x and y limits of the plot
% outputs: the magnetic field vector plot
clear

% prompt user for input materials


disp('Enter the graphs limits');
plotlim = input('[xmin xmax ymin ymax]... \n > ');
if isempty(plotlim); plotlim = [-1 1 -1 1]; end
% check if entered correctly
I = input('Enter the current in Amperes... \n ');
if isempty(I); I = 1; end % check if current is entered

dx=(plotlim(2)-plotlim(1))/10;
dy=(plotlim(4)-plotlim(3))/10;
xrange=plotlim(1):dx:plotlim(2);
xrange=plotlim(3):dx:plotlim(4);

[x,y]=meshgrid(xrange,yrange);
U=zeros(length(xrange), length(yrange);
V=zeros(length(xrange), length(yrange);
for x=1:length(xrange)
for y=1:length(yrange)
r=sqrt(xrange(x)^2+yrange(y)^2);
% the distance from the current
phiuvector=[-yrange(y),xrange(x)/r;
% the unit vector in the phi direction
H=I/(2*pi*r)*phiuvector;
% Ampere's law for an infinite current
% fill matices which contain vector
% components in x and y direction
U(y, x)=H(1); % vector x corresponds to columns
V(y, x)=H(2); % vector x corresponds to columns

end

% Display results
figure
quiver(xrange,yrange,U,V)
axis square
axis(plotlim)
xlabel('x location (m)')
ylabel('y location (m)')
disp('Value of first vector to the right of');
disp(sprintf('origin = %f A/m',I/(2*pi*dx)))

You might also like