Title: Introduction to Matlab 7 Lesson I
1Introduction to Matlab 7Lesson I
- Marco Lattuada
- Swiss Federal Institute of Technology - ETH
- Institut für Chemie und Bioingenieurwissenschaften
- ETH Hönggerberg/ HCI F135 Zürich (Switzerland)
- E-mail lattuada_at_chem.ethz.ch
2How to connect to you folders
- Go to My Computer
- In Tools, click on Map Network Drive
- In the window that appears, type
\\d.ethz.ch\dfs\users\all\ltnethz usernamegt - Select option connect with another username
- Enter d\ ltnethz usernamegt
- Enter your E-Mail password.
- Remember do not keep you data on the computer
you are working on, as they can be deleted!!!!
3Matlab 7.0.4 - 7.8
- What is Matlab?
- Advantages over other choices
Matlab is an interatcive system for numerical
computation
- Quick and easy coding with high level language
- Procedural coding (object-oriented supported)
- Minimal attention required for data
structure/declaration - Simple handling of vectors/matrixes
- High quality graphics available
- Full portability of the m-files
- Built-in editing and debugging tools
- Built-in functions (also on internet)
4Matlab 7.0.4 - 7.8
- Not optimal for symbolic calculations (choose
Maple, Mathematica) - Not as fast as C (C) and Fortran
- Not the best choice for very computationally
demanding problems
5Matlab Introduction Window
File Structure
6Variables
- Names are case sensitive (Marco ? marco)
- 63 Characters max (1st character must be a
letter) - Letters, numerals and sign _ are valid
characters - No Spaces in between
Rules
Invalid Examples
Valid Examples
2ndvariable yes first one 1
a 1 speed 1500 BeamOutput_Type1 vQv name
John Smith
No need to specify the variable type
7Variables
- Try to type
- a 2
- b 3
- c ab
- d c/2
- d
- who
- whos
- clear
- who
- myName marco
Why these are arrays??? Every variable is an
array!!!
By pressing w?, the command who is recalled
8Variables
- Where are my results stored?
- a 2
- b 3
- c ab The result is stored in c
- ab The results in stored in ans
-
By pressing ? or ?, one can recall the previous
commands
By using the symbol at the end of a command,
the execution is performed, but the result is not
shown in the command window
9Vectors
- Row vector gtgt a 1 2 3
- Column vector gtgt b 1 2 3
- Equally spaced vector gtgt c 05100 (or
0100 or 0100) - Evenly spaced vector gtgt d linspace(0,100,21)
- ( vector linspace(start,end,elements)
) ( vector logspace(start,end,elements) ) - generates a vector from 10start to 10end
- Transpose gtgt e d'
If everything is an array, what is a vector ???
You should see
Fast help by gtgt help linspace
10Vector's Arithmetic
- Try to type
- c 2a
- d 2a
- f ac
- g ab
- dot(a,b)
- A ba
- aa
- a2
- a.2
- a.a
- d d./a
- b sqrt(b)
- c exp(c)
- d factorial(d)
Addition/multiplication by constants
Addition of vectors
Dot product of vectors
Your first matrix ? (3,1)(1,3) (3,3)
Error! ? (1,3)(1,3) nothing
Element-by-element operations
Examples of functions returning
element-by-element operations
11Vector's Arithmetic II
- Further examples
- a 1 2 3
- b 1 2 3
- c ab
- d ba
-
-
(1,3)x(3,1)(1,1) SCALAR !
(3,1)x(1,3)(3,3) Matrix!
Never forget the rules for matrix multiplication!
12Arrays/Matrices
- Methods of creating matrices
- By coefficients gtgt A 1 2 3 4 5 6 7 8 9
- Matrix of zeros gtgt B zeros(3,2)
- Matrix of ones gtgt C ones(2,3)
- Identity matrix gtgt D eye(3)
- Random matrix gtgt R rand(3,3)
- Normally distr. random matrix gtgt RD randn(3,3)
- Matrix Characteristics
- Size gtgt size(A)
- Length gtgt length(A)
13Accessing Elements
- Vectors (gtgt a 15.2)
- Single element gtgt a(1)
- Multiple elements gtgt a(1 3)
- Range of elements gtgt a(23)
- Last element gtgt a(end)
- Matrices (gtgt A a'a)
- Single element gtgt A(1,3)
- Submatrix gtgt A(23,23)
- Matrix Column gtgt A(2,)
- Matrix Line gtgt A(,3)
- Lines intersected with columns gtgt A(2 3,1 3
5)
14Arithmetic with Matrices
- Try to type
- B 2A
- C 2A
- D AC
- D D
- C(3,)
- D(,2)
- CD
- DC
- A2
- A.2
- E2.A
- sqrt(A)
- sqrtm(A)
- A(-1)
Addition/multiplication by constants
Addition of matrices
Transpose
Elimination of columns/lines
Product between matrices
Remember product between matrices is usually not
commutative!
Square of matrix Square of matrix's elements Ei,j
2Ai,j Square-root of matrix's
elements Square-root of matrix (try gtgt
(sqrtm(A))2) Inverse of A (equal to gtgt inv(A))
15Matrix Division
- Let us consider the following case
- A, B
-
- A/B
- A\B
-
Two square matrices
A divided by B, alternatively mrdevide(A,B)
equivalent to Ainv(B)
alternatively mldevide(A,B) equivalent to
inv(B)A
The two divisions are different, because the
product between matrices is not commutative!
16Other Ways of Creating Matrices
- Matrices in block form
- From matrices to vectors
- From vectors to matrices
- Diagonal matrices
- Meshes
- From one element/column/line
17Operators for Matrices
- Size gtgt size(C)
- Predominant size gtgt length(C)
- Maximum (columns) gtgt max(A)
- Maximum (general) gtgt val,elem max(A)
- Maximum (overall) gtgt max(A())
- Sum of elements gtgt sum(A)
- Mean value gtgt mean(A())
- Determinant gtgt det(A)
- Inverse gtgt inv(A)
- Condition Number gtgt cond(A)
- Norm gtgt norm(A,i)
min(A) -max(-A)
var(A), std(A),
18Exercises
- Compute the approximate value of exp(1)
- Compute the approximate value of exp(2)
- Compute the cross-product of u(1,3,2) ? v(-1,1,2)
19Solution of Linear Algebraic Systems
- Write in matrix notation the following system of
equations - Is this system singular?
- How would you solve this system?
Shortcut
20Exercises
- Solve the following system
- Find the inverse matrix of A
- Solve the following system
21Structures in Matlab
- What is a structure?
- Example chemical components (try)
- gtgt comp(1).name 'water'
- gtgt comp(1).Mw 18.02
- gtgt comp(1).density 1
- gtgt comp(2).name 'ethanol'
- gtgt comp(2).Mw 46.06
- gtgt comp(2).density 0.789
Structures are arrays whose names have
dot-separated parts. They can be used to store
information of different kinds together in a
hierarchical structure.
22Loops in Matlab
- General form of for-loops
- Example
for variable expression statements end
23Examples of Loops
- Try the following
- gtgt for i 05100
- i
- end
- gtgt v 0 4 5 11
- gtgt for i v, i, end
- gtgt n 1e6 x rand(n,1)
- gtgt tic s 0 for i 1n, s sx(i)2 end, s,
toc - gtgt tic s sum(x.2) s, toc
A technique is a trick that works Giancarlo Rota
24While Loops in Matlab
- General form of while-loops
- Example
while expression statements end
The statement is executed as long as the
expression is true
The statements are executed an indefinite number
of times
25Examples of Loops
- Try the following
- gtgt clear x n 5e4 x(1) 2
- gtgt tic for i 2n, x(i) x(i-1)2i end, toc
- gtgt x1 zeros(1,n) x1(1) 2
- gtgt tic for i 2n, x1(i) x1(i-1)2i end,
toc - gtgt v 2linspace(1,n,n)
- gtgt tic x2 cumsum(v) toc
Pre-allocate arrays
Vectorize and use pre-defined functions
20 times faster
129 times faster