% 22.3MB1 Fall 01
% Yvan Petillot
% Demo of Matlab Capabilities to visualise EM Fields and Potentials
% File Name : EmfieldsM.m

% Creates mesh for calculus
[x,y] = meshgrid(-2:0.2:2,-1:0.2:1);
% As we have variable fields, we need to create a movie to visualise the changes
nframes = 25;  % Number of frames in Movie
VF = moviein(nframes); % Create Movie structure for potential
EF = moviein(nframes); % Idem for Field
delt_t = 0.1;  % Time variation unit

% For each frame we repeat the actions
for k = 1 : nframes
   % Find t
   t = (k-1)*delt_t;
   % Calculate the potential
   V = x.*y.^2.*exp(-t.*x.^2-t.*y.^2);
   figure(1);
   clf(1);
   % Visualise the potential in 3D
   contour3(x,y,V,100);
	xlabel('x');
	ylabel('y');
	title('Potential(Volts) generated by n coplanar charges');
   % Put the current figure in a movie frame
   VF(:,k) = getframe;
   figure(2);
   clf(2);
	% Get the fields as E = grad(-V)
	[ex,ey] = gradient(-V,0.2,0.2);
   % Show the field using the quiver function
   quiver(x,y,ex,ey,2);
	xlabel('x');
	ylabel('y');
	title('Potential(Volts) and Electric Field(Volt/m) for n charges in 2D');
   drawnow;
   % Put the field in a Movie Frame
   EF(:,k) = getframe;
end
% Show movies
figure(1);
movie(VF,1);
figure(2);
movie(EF,1);