%% Introduction to Matlab EL1150, Lecture 1
%%
format compact %(to suppress extra line feeds)
%% Interactive calculations
% Matlab is interactive, no need to declare variables
2+3*4/2
%% 
x=5e-3; y=1; x+y
%%
% Most elementary functions and constants are already defined
cos(pi)
%%
abs(1+i)
%%
sqrt(10)
%% 
exp(1)
%% Variable and memory management
% Matlab uses double precision (appr. 16 significant digits)
format long
pi
%%
format short
pi
%%
% All variables are shown with
who
%%
whos
%%
% Variables can be stored on file
save filename
clear
load filename
%% The Help System
% Search for appropriate function (slow)
lookfor fourier
%% 
% Rapid help with syntax and function definition 
help ifft
%%
%Advanced hyperlinked help system
doc
doc ifft
%% Vectors and matrices
% Vectors (arrays) are entered as
u=[1 2 3 4]
v=[1,2,3,4]
w=[1;2;3;4]
%%
% Matrices (2D arrays) are defined similarly
A=[1,2,3;4,5,6;7,8,9]
B=[1,2;
   3,4;
   5,6]
%% Matrix operators
% All common operators are overloaded
v+2
%%
% Common operators are available
B=A' % Complex conjugate transpose, use A.' to avoid complex conjugation
%%
A*B
%%
A+B
%%
% Matlab is case-sensitive
a
%%
% Element-wise operations
A.*B
%%
A.^2
%% Matrix functions
% Many elementary matrices predefined
eye(3)
%%
zeros(2,3)
%%
% Elementary functions are often overloaded
sin(A)
%%
% Specialized matrix functions and operators
As=sqrtm(A)
%%
As^2
%%
As.^2
%%
help elmat
%% Indexing matrices
A %to remember
%%
% Indexing using parentheses
A(2,3)
%%
% Index submatrices using vectors of row and column indices
A([2 3],[1 2])
%%
% Ordering of indices is important!
A([3 2],[2 1])
%%
% Index complete row or column using the colon operator
A(1,:)
%%
% Can also add limit index range
A(1:2,:)
%%
% General notation for colon operator
v=1:5
%%
w=1:2:9
%% Linear algebra
% System of linear equations
%
% $Ax=b$
%
% $x=A^{-1}b$
A=magic(3);b=[1;2;3];
x=inv(A)*b
%%
x=A\b
%%
% Many standard functions predefined
det(A)
%%
rank(A)
%%
eig(A)
%% Function arguments
% The number of input/output arguments can often be varied
m=min(A)
%%
m=min(A,4)
%%
m=min(A,[],2)
%%
[m,ind]=min(A)
%%
help min
%% Limited precision
% Machine epsilon
eps
%%
a=1+eps;b=1;
a==b
%%
a=2+eps;b=2;
a==b
%%
A=[1,0;0,eps];
inv(A)
%%
A=[2,0;0,eps];
inv(A)
%% Graphics
% Visualization of vector data
x=-pi:0.1:pi; y=sin(x);
plot(x,y)
%%
plot(x,y,'s-')
xlabel('x');ylabel('y=sin(x)');
%%
% Can change plot properties in Figure menu, or via ”handle”
h=plot(x,y); set(h, 'LineWidth', 4);
%%
% Many other plot functions available
v=1:4; pie(v)
%%
% Three-dimensional graphics
A = zeros(32);
A(14:16,14:16) = rand(3);
F=-abs(fft2(A));
mesh(F)
%%
% Several other plot functions available 
surfl(F)
%%
% Can change color, lightning and material properties
colormap pink
light
lighting phong
material shiny
%%
% Bitmap images can also be visualized
load mandrill
image(X); colormap(map);
axis image off