% Unit 2 (MATLAB Course @ UAM)
% (c) Alexander Knebe
%===================================
% 2D potential of one single charge
%===================================
clear all
close all
clc

% position of electrical charge
x0 = +0.25;
y0 = -0.15;

% range and # of points
Nmesh = 100;
xmin  = -1.25;
xmax  = +1.00;
ymin  = -0.75;
ymax  = +1.15;

% generate a linearly spaced mesh in x and y
xmesh = linspace(xmin,xmax,Nmesh);
ymesh = linspace(ymin,ymax,Nmesh);
[x,y] = meshgrid(xmesh,ymesh);

% calculate the scalar field at every mesh point (e.g. 1/r)
Upot = -1./sqrt((x-x0).^2+(y-y0).^2);



%% figure(1): CONTOUR
%====================
% here we calculate some properties of the contours
Ncont = 100;
Umin  = min(min(Upot));
Umax  = max(max(Upot));
v     = logspace(log10(Umin),log10(Umax),Ncont);
v     = linspace(Umin,Umax,Ncont);

% here we eventually plot the contours
figure(1)
contour(x,y,Upot,v)

xlabel('x')
ylabel('y')
axis equal
colorbar


%% figure(2): SURF
%====================
figure(2)
surf(x,y,Upot)

xlabel('x')
ylabel('y')
zlabel('Upot')
shading flat


%% figure(3): SURFL
%====================
figure(3)
surfl(x,y,Upot)

xlabel('x')
ylabel('y')
zlabel('Upot')


%% figure(4): MESH
%====================
figure(4)
mesh(x,y,Upot)

xlabel('x')
ylabel('y')
zlabel('Upot')
