

% Getting started with Matlab
% H. Igel, 2009


% these function close all previous figures and clears the workspace
% (deletes all variables)

close all
clear

% Some simple  Matlab commands

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initializing and plotting vectorial quantities
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Initialize a vector with coordinates
xmin=0;
xmax=1000;
dx=1;
x=xmin:dx:xmax;

% getting information about the vector
nx=length(x);

% initializing a function of x
% f(x) = x^2
f=x.*x;     % the ".*" implies an element by element multiplication

% plot the function
plot(x,f)

% plot labels
title(' f(x) = x^2 ')
xlabel(' x ')
ylabel(' f(x) ')

disp(' Press any key to continue ')
pause    % this way the program is interrupted


% plot a sin function
lambda=250;     % wavelength
k=2*pi/lambda;   % wave number
f=sin(k*x);      % k is a scalar so ".*" is not necessary!

% plot the function
plot(x,f)

% plot labels
title(' f(x) = sin(x) ')
xlabel(' x ')
ylabel(' f(x) ')

disp(' Press any key to continue ')
pause  


% plot a Gaussian function
sigma=250;     % variance
x0=250;   % location of maximum
f=exp(-1/sigma^2*((x-x0).*(x-x0)));      % k is a scalar so ".*" is not necessary!

% plot the function
plot(x,f)

% plot labels
title(' f(x) = exp(-1/sigma^2 (x-x0)^2 ')
xlabel(' x ')
ylabel(' f(x) ')

disp(' Press any key to continue ')
pause  


% Now lets have variable input

disp(' -------------------- ')
disp(' Choose your variable ')
disp(' -------------------- ')



% plot a sin function

%lambda=250;     % wavelength
lambda=input(' Give wavelength ')
k=2*pi/lambda;   % wave number
f=sin(k*x);      % k is a scalar so ".*" is not necessary!

% plot the function
plot(x,f)

% plot labels
title(' f(x) = sin(x) ')
xlabel(' x ')
ylabel(' f(x) ')

disp(' Press any key to continue ')
pause  


% plot a Gaussian function
sigma=250;     % variance
sigma=input(' Give half-width  ')
x0=250;   % location of maximum
f=exp(-1/sigma^2*((x-x0).*(x-x0)));      % k is a scalar so ".*" is not necessary!

% plot the function
plot(x,f)

% plot labels
title(' f(x) = exp(-1/sigma^2 (x-x0)^2 ')
xlabel(' x ')
ylabel(' f(x) ')

disp(' Press any key to continue ')
pause  


% random functions in 1D
%length of domain
nx=length(x);
f=rand([1 nx]);      % [1 nx] implies a vector/matrix with dimension 1,nx

% plot the function
plot(x,f)

% plot labels
title(' f(x) = random ')
xlabel(' x ')
ylabel(' f(x) ')

disp(' Press any key to continue ')
pause  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2D function and their display options, loops, multiple plots
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear
% Initialize two vectors with coordinates
xmin=0;
xmax=1000;
dx=2;
x=xmin:dx:xmax;
nx=length(x);

ymin=0;
ymax=1000;
dy=2;
y=ymin:dy:ymax;
ny=length(y);

% initialize a 2D Gaussian function 
x0=250;
y0=250;
sigma=100;

% note that loops like this are inefficient in Matlab, they can be
% performed with matrix matrix calculations
for i=1:nx
    for j=1:ny
        f(i,j)=exp(-1/sigma^2*((x(i)-x0).*(x(i)-x0)+(y(j)-y0).*(y(j)-y0)));
    end
end

% multiple plot options in a single plot


subplot(3,1,1)

imagesc(x,y,f)

subplot(3,1,2)

meshc(x,y,f)

subplot(3,1,3) 

surf(x,y,f), shading interp





