%-HOW TO SEARCH FOR HELP-

help rand % open help file for rand
help randi % open help file for randi

%http://uk.mathworks.com/help/matlab/ref/rand.html





%-CREATION OF BASIC ELEMENTS
%adding ";" at the end of the command prevent the result to be printed on
%screen
1

[1 2 3]
[1
    2
    3]

[1,2,3]
[1;2;3]




%-RANDOM NUMBER GENERATIONS-

%generate ONE natural number between 1 and 10
randi([1,10])

%generate ONE real number between 0 and 1
rand(1)

%generate ONE real number between 0 and 10
10*rand(1)

%create list of values between 0 and 2*pi in increments of 0.01
ang = [0:0.01:2*pi];





%-RANDOM ROW GENERATIONS-

%generatore a row of 10 natural numbers between 1 and 10
randi([1,10],1,10)

%generate a row of 10 real random numbers between 0 and 1
rand([1,10])

%generate a row of 10 real random numbers between 1 and 10
10*rand([1,10])




%-RANDOM ROW GENERATIONS-
(10*rand([1,10]))'




%-VARIABLE DECLARATION
matrix=zeros(2,3)





%-MATRIX CREATIONS AND OPERATIONS-

%--EXAMPLE 1--

% define a matrix
favouriteMatrix = [1,2,3 ; 4,5,6 ; 7,8,9] 

%basic commands
favouriteMatrix(2,2)
favouriteMatrix(:,1)
favouriteMatrix(1,:)
favouriteMatrix(:,:)

% determinant of that matrix
det(favouriteMatrix) 

%eigenvalues
eig(favouriteMatrix)

%--EXAMPLE 2--

% creates a 3x3 matrix of randomly distributed numbers on (0,1)
rand(3)

%--EXAMPLE 3--

% now we want to create a 50x50 matrix called James, who contains random integers
% between 1 and 10
James = randi([1,10],50,50);

% eigenvalues of James (outputs a list, which we have labelled E)
E = eig(James)

%--EXAMPLE 4--

% define transition rates
p=0.3; q=1-p; 

% create 3x3 stochastic transition matrix (with periodic boundaries)
A = [0,p,q;q,0,p;p,q,0];

% eigenvalues
eig(A)





%-----PICTURES-----

%--EXAMPLE 1--
% plot eigenvalues of James on the complex plane, using stylish blue
% circles
% i.e. plot real component against imaginary component of each item in E
figure
plot(real(E),imag(E),'bo');


%--EXAMPLE 2--
% parametric plot of a circle radius 1, centered on (0,0)
% plotted as a fashionable red line

figure
plot(real(eig(A)),imag(eig(A)),'bo');
hold on;
plot(0+1.*cos(ang),0+1.*sin(ang),'r');
hold off;

%--EXAMPLE 2 BIS--
figure
plot(real(eig(A)),imag(eig(A)),'bo');
hold on;
plot(0+1.*cos(ang),0+1.*sin(ang),'r');

xlabel('Real Part');
ylabel('Imaginary Part');
legend('Eigenvalues','Circle');
axis([-0.8 0.8 -1 1]);
str={'What a wonderful number:',3.14};
text(0,0,str,'Color','k','FontSize',14);

hold off;





%---PLOT FUNCTION---

%--EXAMPLE 1--
X = [0:0.01:10];
Y = X.^2;
plot(X,Y)

%--EXAMPLE 2--
syms a b
test=@(a,b)a+b^2;
a=1;
fplot(test(a,b))

%--EXAMPLE 3--

fplot(@(x) exp(x),[-3 0],'b');
hold on;
fplot(@(x) cos(x),[0 3],'b');
plot(X,Y);
axis([0 3 0 2]);
grid on;
hold off;





%-----EXERCISE 1-----
% iterate the matrix and watch what happens to it's entries
% what does this tell you?

A

A^2

A^3

A^4

A^5

A^10

A^100

% obtain the eigenvectors and eigenvalues of A
% N.B. the transpose is necessary to obtain the LEFT eigenvectors
% we aren't interested in RIGHT eigenvectors
[V,E] = eig(A')





%-----EXERCISE 2-----
% define new transition rates
r = 0.1; p=0.2;q=1-p-r; 

% create 'lazy' random walk transition matrix
A = [r,p,0,q;q,r,p,0;0,q,r,p;p,0,q,r]
 
A^2

A^10

A^100

% visualisation of Gershgorin Theorem
figure
plot(real(eig(A)),imag(eig(A)),'bo') % plot eigenvalues of A (blue circles)
hold on; 
plot(0.1+0.9.*cos(ang),0.9.*sin(ang),'r') % plot radius 0.9 circle centered on (0.1,0)
plot(real(eig(A^10)),imag(eig(A^10)),'go') % plot eigenvalues of A^10 (green circles)
hold off;
