% 22.3MB1 Fall 01
% Yvan Petillot
% Fields due to discrete and line charge distributions

function chargedist2D(charges)

[x,y,z] = meshgrid(-1:0.05:1,-1:0.05:1,-1:0.05:1);
%Point Charges
E = x.*0;
for i = 1 : size(charges,1)
	E = E+1./((x-charges(i,1)).^2+(y-charges(i,2)).^2+(z-charges(i,3)).^2);
end
figure(1);
imagesc(E(:,:,10));
xlabel('x');
ylabel('y');
title('Slice of Electric Field for z = -0.5 meters');
drawnow;
pause;
disp('Hit return for next figure');
imagesc(E(:,:,21));
xlabel('x');
ylabel('y');
title('Slice of Electric Field for z = 0 meters');
drawnow;
pause;
disp('Hit return for next figure');
imagesc(E(:,:,31));
xlabel('x');
ylabel('y');
title('Slice of Electric Field for z = 0.5 meters');
drawnow;
pause;
disp('Hit return for next figure');
[x,y,z] = meshgrid(-1:0.05:1,0.0001:0.05:1,-1:0.05:1);
%Point Charges
E = x.*0;
for i = 1 : size(charges,1);
	E = E+1./((x-charges(i,1)).^2+(y-charges(i,2)).^2+(z-charges(i,3)).^2);
end
slice(x,y,z,log(E),[-0.9:0.05:0.9],0.9,[-0.9:0.05:0.9]);shading interp;
colormap hsv;
xlabel('x');
ylabel('y');
zlabel('z');
title('Electric Field (Log Magnitude)due to point charge at origin (0,0,0)');
axis square;
colorbar;
rotate3d on;
pause;
disp('Hit Return for a line charge');
%Line Charge
[x,y,z] = meshgrid(-1:0.05:1,-1:0.05:1,-1:0.05:1);
E = x.*0;
for i = 1 : size(charges,1);
	E = E+1./sqrt((x-charges(i,1)).^2+(y-charges(i,2)).^2);
end
figure(2);
imagesc(squeeze(E(:,11,:)));
xlabel('x');
ylabel('y');
title('Slice of Electric Field for y = -0.5 meters');
drawnow;
pause;
disp('Hit return for next figure');
imagesc(squeeze(E(:,21,:)));
xlabel('x');
ylabel('y');
title('Slice of Electric Field for y = 0 meters');
drawnow;
pause;
disp('Hit return for next figure');
imagesc(squeeze(E(:,31,:)));
xlabel('x');
ylabel('y');
title('Slice of Electric Field for y = 0.5 meters');
drawnow;
pause;
disp('Hit return for next figure');
[x,y,z] = meshgrid(-1:0.05:1,0.0001:0.05:1,-1:0.05:1);
E = x.*0;
for i = 1 : size(charges,1);
	E = E+1./sqrt((x-charges(i,1)).^2+(y-charges(i,2)).^2);
end
slice(x,y,z,log(E),[-0.9:0.1:0.9],0.9,[-0.9:0.1:0.9]);shading interp;
colormap hsv;
xlabel('x');
ylabel('y');
zlabel('z');
title('Electric Field (Log Magnitude)due to line charge along z-axis');
axis square;
colorbar;
rotate3d on;
