A brief introduction to Matlab

Matlab for Scientific Programming
A Brief Introduction
Mark Levene
Follow the links to learn more!
Many features will be demonstrated
What can we achieve in 3 hours?
• Demonstration of why you may consider to
use Matlab and for what types of tasks.
• Tips on getting you started as a Matlab
• You will need to practice Malab at home,
and/or when doing a project for which
Matlab may be suitable.
Matlab Resources
There are many!!
• Matlab tutorials and learning resources
• Attaway, MATLAB A Practical Introduction to
Programming and Problem Solving, Second Edition,
Elsevier, 2012 (Introductory).
• Banches, Text Mining with Matlab, Springer, 2012
• Martinez et al., Exploratory Data Analysis with Matlab,
Second Edition, CRC Press, 2011(Advanced).
• Matlab claims over 1 million users world wide in 2012!!
• There are many Matlab books.
• Also see Matlab documentation centre.
Why Matlab?
Introductory Example
• Example5_2 from Martinez – load iris
• 3 classes of Iris to be distinguished by
– sepal length, sepal width
– petal length, petal width
• Look at the “data”
• Briefly discuss the kmeans algorithm for
grouping data into k groups (here k=3);
demonstrate help in Matlab
What can you do in Matlab?
• The normal things you can do in any other
programming languages, but is interpreted and
not strict in its typing to allow quick prototyping.
• Has many built-in features to handle matrices,
maths & stats, data analysis and plotting.
• Has a wide range of toolboxes such as curve
fitting, neural networks, bioinformatics, symbolic
maths and finance.
• Although Matlab is proprietary there are many
open source toolboxes; see Matlab central.
A simple function in Matlab
function area = conearea(radius, height)
area = pi/3 * radius^2 * height;
conearea(4,6.1)  ans = 102.2065
• Matlab has the usual control flow that
other languages have – use help when
needed !!
Vectorised code
• Chapter 5 in Attaway
for i=1:10 v(i) = i; end %create a vector
v = 1:1:10 %start=1, increment=1,end=10
for i=1:10 w(i) = w(i)^2; end %standard loop
w=w.^2; %vectorised code
• Can use any other vector operations!
w=log(v); %a vector can be an argument
• Can query a vector using find:
find(w>5); %returns indices satisfying condition
Matrices and Linear Algebra
• Chapter 12 in Attaway
• Can also vectorise (try it out yourselves!)
m = rand(3,3); %create a random 3x3
• Matrix operations work as expected!
n = m.*m %Matrix multiplication
• You will need to revise your linear algebra
to make use of these Matlab features.
Example SVD and PCA
(Eigenvector decomposition)
• For example Singular Value Decomposition
(SVD) – Look this up for maths details,
• SVD has applications in many areas including
Information Retrieval is easy in Matlab.
m = rand(100,10); svd(m);
• Principal Components Analysis (PCA) is a
special case of SVD measuring the directions
along which the variance is maximised.
load filteredyeastdata;
mapcaplot(yeastvalues, genes);
Basic Statistics
• Chapter 13 in Attaway
x= [8 9 3; 10 2 3; 6 10 9];
mean(x)  ans = 8 7 5
var(x)  ans = 4 19 12
std(x)  ans = 2.0000 4.3589 3.4641
• y = [9 10 10 9 8 7 3 10 9 8 5 10];
mode(y)  ans = 10
median(y)  ans = 9
• There is much more in the statistics toolbox
Curve fitting
Example7_3 in Martinez
Demonstrate cftool with (x,x) and (x,y)
Example 9_2 in Martinez
Demonstrate histogram
Demonstrate cftool with (xk,nuk)
normfit = [33,40,42,41,39,32];
linfit = [2,44,49,61,82,95]
• Chapter 15 in Attaway
• Symbolic maths
syms x y
f = x^2 + y^2 + 2*x*y
simplify(f)  ans = (x + y)^2
expand(ans)  f
ezplot(x^2+2*x+2) % plot the function
Solving equations
solve(2*x^2+x-6)  ans = -2 3/2
syms x a b c
solve(a*x^2+b*x+c)  ans =
-(b + (b^2 - 4*a*c)^(1/2))/(2*a)
-(b - (b^2 - 4*a*c)^(1/2))/(2*a)
• Can try and solve more complex equations
solve(exp(x)-3)  ans = log(3)
syms x
diff(x^3,x)  ans = 3*x^2 %differentiation
int(3*x^2)  ans = x^3 %integration
• Can do much more, including solving differential
• Do not worry about the Maths as such, as in
Matlab we pick up the tools as and when we
need them.
• Matlab provides and easy-to-use, state-of-theart environment for scientific computing.
• There are a wide variety of toolboxes for
different applications, many of which are open
• Matlab may not always be the most efficient
solution but it is great for quick prototyping.
• Matlab is not designed for general purpose
programming, although it is a complete

similar documents