% 22.3MB1 Fall 01
% Yvan Petillot
% Fields due to discrete and line charge distributions
% This script shows you how to create and visualise fields in 3D.
% The examples taken are point and line charges for the E field.

M = moviein(20);    %Create structure for movie
close all; clear;   % Close all variables / figires
[x,y,z] = meshgrid(-1:0.1:1,0.1:0.1:1,-1:0.1:1); %Create grid for calculation of field
epsilon0 = 8.84e-9;  % Value of epsilon 0
%Point Charge
E = 1/(4*pi*epsilon0)./(x.^2+y.^2+z.^2);  % Calculates field due to a point charge
maxE = 1e2/(4*pi*epsilon0);				   % Calculates maximum of E.
figure(1);
% generate a 3D vue of the field
slice(x,y,z,log(E),[-1:0.1:1],[0.001:0.1:1],[-1:0.1:1]);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; % Create square axis
colorbar;
rotate3d on;
pause;
for i = 1 : 10
   imagesc(log(squeeze(E(i,:,:))));  %generates vue of the field at different slices
   M(:,i) = getframe;                       % Put them into the movie frames
end
movie(M);  %View the movie
pause;
slice(x,y,z,log(E),[-1:0.1:1],[0.001:0.1:1],[-1:0.1:1]);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;
%Line Charge   % Same with a line charge.
E = 1./sqrt(x.^2+y.^2);
figure(2);
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;
