% MATLAB warm-up

%%%%%% INSTRUCTIONs for warm-up:
% * Simply copy and paste line-by-line into command window, hit enter and observe what happens.
% * Leave out semicolons to display results.
% * Find the "workspace" and observe how it changes as you go through all the commands.
% * Finally, execute the entire code by typing the file name "MATLAB_warmup" into the command window and hitting enter.
% * At the end of the file you find a short warmup exercise.

% comments start with "%"

% clear the command window
clc;

% clear the workspace
clear all;

%%%% adding
17 + 3 % gives result
17 + 3; % semicolon supresses display of result


%%%% special numbers
sqrt(pi);

%%%% defining variables
x = 3;
y = -1/3;

%%%% multiplying variables
x*y;

%%%% vectors
v = [1,2]; % row vector
w = [1;2];  % column vector
b = [17;-1;0.1]; % row vector
z = zeros(4,1); % all entries are zeros
x = ones(1,15); % all entries are ones (previously defined x is overwritten)

%%%% matrices
A = [1,2,3;4,5,6] % 2 x 3 matrix
B = ones(3,5); % 3 x 5 matrix
C = rand(5); % 5 x 5 matrix with pseudorandom entries

%%%% matrix-vector multiplication
A*b; % works fine

% uncomment to get an error since dimensions do not match 
% A*v; % does not work

%%%% matrix-matrix multiplication
A*B; % works fine
B*C; % works fine

% uncomment to get an error since dimensions do not match 
% B*A; % does not work

display('Reduced echelon form of A')
% % reducing to echelon form
rref(A)
 
%%%% determinant
J = [1,1,1,1;1,2,2,2;1,2,3,3;1,2,3,4]
display('determinant of J')
det(J)
 
%%%% computing eigenvalues and eigenvectors
C = [-3,7;.1,6]
display('diagonalization of A')
[V,D] = eig(C)  % gives eigenvectors arranged into V, so that A*V = V*D
inv(V)*C*V % (V^(-1))*A*V works too
 
%%%% plotting functions
x = [-5:.05:5];
y = exp(-x.^2);
plot(x,y,'r');hold on;
plot(x,y,'b*');hold off;

pause(1) % pauses 1 second before further execution

%%%% plotting surfaces
[XX,YY] = meshgrid(-1:.2:1,-1:.2:1);                                
ZZ = XX.*exp(-2*XX.^2 - 2*YY.^2);
figure;
surf(XX,YY,ZZ);


%%%%%% INSTRUCTIONs for warm-up exercise:
% * Define a vector x containing the numbers from -10 to 10 in steps of 0.1.
% * Define the parameters sigma = 1 and mu = 0.
% * Compute the function 
%          f = (1/sqrt(2*pi*sigma^2))*exp(-(1/(2*sigma^2))*(x-mu).^2);
% * Have you ever encountered this function before?
% * Find out what the difference between the commands "x*x" and "x.*x" is.
% * Plot the function f.
% * Type "help plot" into the MATLAB command window and try to find out what options the plot command has.
% * Choose different values for sigma and mu.
% * Try to plot the corresp. graphs into the same plot with different colors and/or symbols.
% * Explain how sigma and mu change the function.
