% matlab script to plot the vector field (electric field) from
% a line segment of uniformly charged conductor.
%
%
% say we have +q at x0=origin, -q at x1=5cm
global x0 = 0.0; %m
global x1 = 0.05; %m
% charge per unit length
global q = 1.0e-4; % Coulombs 

% set up mesh of points where feild should be calculated / plotted:
[x,y] = meshgrid( -0.1:0.02:0.1,  -0.09:0.02:0.10 );

% Components of the field
% notice the operators are .* and ./ and .^ to indicate that the calculation is done for 
% each element of the vectors
Ex = 8.99e9 * q .* ( (x-x0)./( (x-x0).^2 + y.^2 ).^1.5 + (x1-x)./( (x-x1).^2 + y.^2 ).^1.5 );
Ey = 8.99e9 * q .* ( y./( (x-x0).^2 + y.^2 ).^1.5 - y./( (x-x1).^2 + y.^2 ).^1.5 );
%
%  
% now make the vector plot (quiver)
quiver( x, y, Ex, Ey )
%
% print a table
%for ix = 1:10
%  for iy = 1:10
%    printf("at x=%f m, y=%f m E=%f i + %f j N/C. \n", x(ix,iy), y(ix,iy), Ex(ix,iy), Ey(ix,iy));
%  endfor
%endfor
