Introduction to Matlab

advertisement
Introduction to Matlab 7
Part I
Daniel Baur
ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften
ETH Hönggerberg / HCI F128 – Zürich
E-Mail: daniel.baur@chem.ethz.ch
http://www.morbidelli-group.ethz.ch/education/snm/Matlab
Daniel Baur / Introduction to Matlab Part I
1
File System
 Your home directory is mapped to Y:\
 The «my documents» folder points to Y:\private
 File reading and writing can take longer than usual since
this is a network drive
Always save your data in your home directory!!
If you save it locally on the computer, it might be lost.
Daniel Baur / Introduction to Matlab Part I
2
Accessing your Data from Home
 To access your home directory from outside the ETH,
connect to the ETH VPN and map the folder
\\d.ethz.ch\dfs\users\all\<Login-Name>
 Windows: Map network drive (right-click on computer)
Mac: Go to / Connect to server
Unix: smbmount
 Log in as d\<Login-Name>
Daniel Baur / Introduction to Matlab Part I
3
Introduction
 What is Matlab?
 Matlab is an interactive system for numerical computation
 What are the advantages of Matlab?









Quick and easy coding (high level language)
Procedural coding and Object oriented programming are supported
Minimal effort required for variable declaration / initialization
Simple handling of vectors and matrices (MATrix LABoratory)
High quality built-in plotting functions
Full source-code portability
Strong built-in editing and debugging tools
Extremely diverse and high quality tool boxes available
Large community that contributes files and programs (mathworks file
exchange website)
 Extensive documentation / help files
Daniel Baur / Introduction to Matlab Part I
4
Introduction (Continued)
 What are the weaknesses of Matlab?
 Not optimal for symbolic calculations (especially on the output side),
use Maple or Mathematica instead
 Not as fast as C++ or Fortran, especially for computationally
demanding problems
 Very expensive (except for students)
 Where to get Matlab?
 ETH students have free access to Matlab
 Go to http://www.ides.ethz.ch/ and search for Matlab in the catalogue
 You might have to set a password on the ides-website in order to log
in
 Remember to choose the correct operating system
 Map the web-drive \\ides.ethz.ch\<Login-Name> to download / install
Matlab
Daniel Baur / Introduction to Matlab Part I
5
Matlab environment (Try it out!)
Variable Inspector / Editor
File Structure
Workspace (Variable List)
Command Prompt
Command History
File Details
Daniel Baur / Introduction to Matlab Part I
6
Where to get help
 If you know which command to use, but not how:
 Type help command in the command window for quick help
 Type doc command in the command window to open the help page
of the command
 Right click on a word and select «help on selection», or click the
word and press F1
 If you do not know which command to use:
 There are extensive forums and other sources available on the
internet, google helps a lot!
 Type doc or use the menu bar to open the user help and search for
what you need
 Send me an email 
Daniel Baur / Introduction to Matlab Part I
7
What if something goes wrong?
 The topmost error message is usually the one containing
the most useful information
 The underlined parts of the message are actually links that
you can click to get to the place where the error happened!
 If a program gets stuck, use ctrl+c to terminate it
Daniel Baur / Introduction to Matlab Part III
8
Variables in Matlab
 Try:
 Valid examples:




 Invalid examples:
a = 1
speed = 1500
Cost_Function = a + 2
String = 'Hello World'
 2ndvariable = 'yes'
 First Element = 1
 Rules





Variable names are case sensitive («NameString» ≠ «Namestring»)
Maximum 63 characters
First character must be a letter
Letters, numbers and underscores «_» are valid characters
Spaces are not allowed
Daniel Baur / Introduction to Matlab Part I
9
Variables in Matlab (Continued)
 Try out these commands:










a = 2
b = 3;
c = a+b;
d = c/2;
d
who
whos
clear
who
TestString = 'Hello World'
Note that every variable has a
size (all variables are arrays!)
No need to declare variables
or specify variable types!
Daniel Baur / Introduction to Matlab Part I
10
Variables in Matlab (Continued)
 Variable assignments





a
b
c
a
a
=
=
=
+
=
2;
3;
a + b;
b
b = 2;
The result is stored in «c»
The result is stored in «ans»
This produces an error
 By pressing the up and down arrows, you can scroll
through the previous commands
 A semicolon «;» at the end of a line supresses command
line output
 By pressing the TAB key, you can auto-complete variable
and function names
Daniel Baur / Introduction to Matlab Part I
11
Vectors in Matlab
 Vector handling is very intuitive in Matlab (try these!):
 Row vector:
 Column vector:
 Vector with defined spacing:
 Vector with even spacing:
 Transpose:
a
a
b
c
d
e
f
=
=
=
=
=
=
=
[1 2 3]
[1, 2, 3]
[1; 2; 3]
0:5:100 (unit: 0:100)
linspace(0, 100, 21)
logspace(0, 3, 25)
e'
 You should see
Daniel Baur / Introduction to Matlab Part I
12
Vector arithmetics
 Try these out:
 a = [1, 2, 3]
 b = [1; 2; 3]
Operations with constants
 c = 2*a
 d = 2+a
Vector addition
 f = a + c
Element-by-Element
operations
 a.^2
 d = d./a
Functions using element-byelement operations (examples)
 b = sqrt(b)
 c = exp(c)
 d = factorial(d)
Vector product
 A = b*a
A is a (3,3) matrix!
 a*a
Error! (1,3)*(1,3)
 a^2
Daniel Baur / Introduction to Matlab Part I
Operations
with
scalar
constants (except power) are
always element-by-element.
13
Vector arithmetics (Continued)
 Notes on vector multiplication
 a = [1, 2, 3]
a  1 2 3
 b = [1; 2; 3]
1 
b   2 
 3 
 c = a*b
 d = b*a
(1,3)*(3,1) = (1,1) Scalar (dot product)
(3,1)*(1,3) = (3,3) Matrix
 e = a.*a
 f = a.*b
(1,3).*(1,3) = (1,3) Vector (element-by-element)
Error! Vectors must be the same size for
element-by-element operations
Remember the rules for vector /
matrix addition, subraction and
multiplication!
Daniel Baur / Introduction to Matlab Part I
14
Matrices in Matlab
 Creating matrices (try these out!)





Direct:
Matrix of zeros:
Matrix of ones:
Random matrix:
Normally distributed:
A = [1 2 3; 4 5 6; 7 8 9]
B = zeros(3); B = zeros(3,2);
C = ones(3); C = ones(3,2);
R = rand(3); R = rand(3,2);
RD = randn(3)
 Matrix characteristics
 Size
 Largest dimension
 Number of elements
[nRows, nColumns] = size(A)
nColumns = size(A,2)
maxDim = length(A)
nElements = numel(A)
 Creating vectors
 Single argument calls create a square matrix, therefore use
commands like v = ones(3,1); to create vectors
Daniel Baur / Introduction to Matlab Part I
15
Accessing elements of vectors / matrices
 Try:
 Vectors





Single element:
Multiple elements:
Range of elements:
Last element:
All elements:
 Matrices






a = (1:5).^2
a(:) always returns a
A = a'*a;
column vector.
Single element:
Submatrix:
Entire row / column:
Multiple rows / columns:
Last element of row / column:
All elements as column vector:
Daniel Baur / Introduction to Matlab Part I
16
Arithmetics with matrices
 Try these out:
 A = rand(3)
Operations with constants
 B = 2*A
 C = 2+A
Matrix addition; Transpose
 D = A+C
 D = D'
Deleting rows / columns
 C(3,:) = []
 D(:,2) = []
Matrix multiplication
 C*D
 D*C
Not commutative!
 A^2
Element-by-element operations
 A.^2
 E = 2.^A
Ei,j = 2^Ai,j
 sqrt(A)
Functions using matrices
 sqrtm(A)
 sqrtm(A)^2
 inv(A)
Daniel Baur / Introduction to Matlab Part I
17
Matrix divison
 Consider the following
 A = rand(3); B = rand(3);
 A*C = B
  C = A-1*B = inv(A)*B
 Matrix inversion is one of the most computationally expensive
operations overall, so what should we do instead?
 Matlab has more sophisticated built-in algorithms to do matrix
divisions which are called left- and right divide; They are symbolized
by the operators \ and /, respectively.
 inv(A)*B = A-1*B  A\B;
 A*inv(B) = A*B-1  A/B;
Daniel Baur / Introduction to Matlab Part I
18
More matrix manipulations
 Try:
 Matrices in block form
 B = [ones(3); zeros(3); eye(3)]
 From matrices to vectors
 b = B(:)
 From vectors to matrices
 b = 1:12; B = zeros(3,4); B(:) = b
 B = reshape(b, 3, 4)
 C = repmat(b, 5, 1)
 Diagonal matrices
 b = 1:12; D = diag(b)
 Meshes
 [X, Y] = meshgrid(0:2:10, 0:5:40)
Daniel Baur / Introduction to Matlab Part I
19
More Matrix Manipulations (Continued)
Daniel Baur / Introduction to Matlab Part I
20
Operators for matrices
 Consider the operators:
 [nRows, nColumns] = size(A);
 [maxValue, Position] = max(A,[],dim);
 sum(A,dim);
 sum(A(:));





Also: min(A)
Also: mean(A), var(A), std(A), ...
det(A);
inv(A);
eig(A);
cond(A);
norm(A,p);

p
norm( A, p)    xi 
 i 1

n
Daniel Baur / Introduction to Matlab Part I
1
p
21
Exercise
1. Compute the approximate value of exp(1)
 Hints: Define a vector of length 20 for the first 20 elements of the
summation, then sum it up; The ! operator is factorial()

xk
e 
k 0 k !
x
2. Compute the approximate value of exp(2)
3. Compute the cross product of u = [1, 3, 2] and v = [-1, 1, 2]
 u2 v3  u3v2 


u  v   u3v1  u1v3 
u v u v 
 1 2 2 1
Daniel Baur / Introduction to Matlab Part I
22
Solution of Linear Algebraic Systems (Exercise)
1. Write the following system of equations in Matrix form:
 2 x1  4 x2  8 x3  2

3 x1  2 x2  2 x3  5  A  x  b
 x  3 x  x  4
2
3
 1
2. Is this system singular?
3. How would you solve this system?
Computing the inverse of a
matrix is very expensive.
Use left division instead!
Daniel Baur / Introduction to Matlab Part I
23
Exercise (Continued)
1. Solve the system
AX  B
2 4 8 
 2 14 26 
 3 2 2   X   5 5 9 




1 3 1
 4 8 2 
2. Now solve this system:


A


 0
1 0
0 4

0 0
B
 38 
 25 
0 




0 
 22 
x  

5  
234



 9 



 68
Daniel Baur / Introduction to Matlab Part I
24
Download