% Introduction to Matlab --- Raymond Spiteri
% notice that everything after a '%' is a comment
% no matter where it appears on the line
%%%%%%%%%%%%%%%% getting started %%%%%%%%%%%%%%%%%%%%%%%
help % this is a very important command to know!
lookfor % when you're not sure what you want!
demo % for touring the capabilities of Matlab
% you can probably guess what the next two commands do
% (if you can't, you'll probably figure it out pretty quick!)
quit
exit
% some other useful commmands
pwd % print working directory
cd % on its own, this is equivalent to pwd
% but it can be used to change directory
dir % both of these show you the contents of the directory
ls % but they are not exactly the same!
path % shows you what's in Matlab's path
% i.e., what directories it will look in for files
who % the names of the variables in your workspace
whos % the names and sizes
clear % clears the current workspace
% it is often good practice to start scripts with this command!
diary 'oct30.txt' % start recording your session in file oct30.txt
% in the meantime, let's look at some pre-defined constants
pi
eps
Inf
NaN
diary off % stop recording session
% scientific notation is OK
a = 1e-10
% our friend the complex number
i + i
j + j
% in Matlab, *everything* is a matrix
A = [1 2 3; 4 5 6; 7 8 9] % give me a matrix
% don't worry -- Matlab is case sensitive, so the old (scalar) a still exists!
size(A) % dimensions of A
A' % transpose
A + A % matrix addition
2*A % scalar-matrix multiplication
A - A % matrix subtraction
A*A % matrix-matrix multiplication
A^2 % powers of matrices
A/A % matrix division??? this really means A*inv(A)
A^(-1) % same as inv(A)
det(A) % determinant
trace(A) % sum of diagonal elements
eig(A) % eigenvalues
[V,lambda] = eig(A) % eigenvectors and eigenvalues
% access a chunk of A
A(:,1) % first column of A
A(2,:) % second row of A
% component-wise operations
A.*A % not the same as A^2 !!!
A.^2 % same as A.*A
A./A % matrix of ones (pretty boring!)
exp(A) % take exponential of every element
format long e
log(A) % take natural logarithm (ln) of every element
% matrix functions
expm(A) % matrix exponential! I + A + A^2/2! + A^3/3! + ...
logm(A) % matrix logarithm (complex because of 0 eigenvalue)
% common matrices
m=4; n=2; % the semi-colon suppresses the output
% very useful when writing scripts (improves performance!)
ones(m,n) % matrix of ones
zeros(m) % matrix of zeros
eye(n) % identity matrix (math joke)
diag([1,2,3,4]) % a diagonal matrix
diag([1,2,3,4],-1) % a matrix with a subdiagonal
% linear system solving: given matrix A and vector b such that Ax=b, find x
A = [3 1; 2 2];
b = [0; 1];
x = A\b
% random numbers
x = rand % a random number between 0 and 1 (uniformly chosen)
A = rand(m) % uniformly distributed mxm matrix of random numbers
B = randn(m,n) % normally distributed mxn matrix of random numbers (mean 0, standard deviation 1)
seed = 0; % used to 'control' randomness
rand('state',seed) % (make random runs reproducible)
% let's generate (and save) some random data for later
random_data = randn(1000,1);
save random_data.dat random_data -ascii
clear
load random_data.dat
random_data
more on % let's stop it so we can look at it!
% simple plotting
ezplot('sin(x)/x') % default axes
ezplot('sin(x)/x',[-3*pi, 3*pi,-0.5,1.5]) % customized axes
% data plotting
plot(random_data)
hold on % do not make new figure
plot(random_data,'ro')
hold off
semilogy(random_data)
% basic statistical functions
max(random_data)
min(random_data)
mean(random_data) % average
std(random_data) % standard deviation
median(random_data)
sum(random_data)
prod(random_data)
sort(random_data) % sort data in increasing order
hist(random_data,40) % create a histogram of data frequencies
a = [0.5 1 1.6 1.2 .8 2.1]; % create some other phony data
pie(a) % make a pie chart
pie(a,a==max(a)) % pull out the largest slice (dieting can wait)
a = sort(a);
t = 1:length(a);
plot(a,'ko')
hold on
coeff = polyfit(t,a,1) % linear regression (line of best fit)
fit = coeff(1)*t + coeff(2)
plot(t,fit)
coeff = polyfit(t,a,2) % parabola of best fit
fit = coeff(1)*t.^2 + coeff(2)*t + coeff(3)
plot(t,fit,'g--')
% simple looping constructs
% for loop
x = 0;
for i=1:10,
x = x + 1;
end
disp(x)
% while loop
x = 0;
while (x < 10),
x = x + 1;
end
disp(x)
x=0;
for i=1:10,
if (x > 5)
x = x + 1;
end
end
x
x=0;
for i=1:10,
if (x > 5)
x = x + 1;
else
x = x - 1;
end
end
x
% symbolics
S1 = sym('sin(x)')
diff(S1)
diff(S1,4)
int(S1)
% numerical quadrature: quad and quadl
a = input('Please enter left end of integration: ');
b = input('Please enter right end of integration: ');
quad('sin(x)',a,b)