Introduction to  Matlab 7 Lesson I
How to connect to you folders <ul><li>Go to “My Computer” </li></ul><ul><li>In Tools, click on “Map Network Drive” </li></...
Matlab 7.0.4 - 7.10 <ul><li>What is Matlab? </li></ul><ul><li>Advantages over other choices: </li></ul>Marco Lattuada – St...
Matlab 7.0.4 - 7.10 <ul><li>Disadvantages: </li></ul>Marco Lattuada – Statistical and Numerical Methods for Chemical Engin...
Matlab Introduction Window Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction C...
Variables Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #  R...
<ul><li>Try to type: </li></ul><ul><li>a = 2 </li></ul><ul><li>b = 3; </li></ul><ul><li>c = a+b; </li></ul><ul><li>d = c/2...
<ul><li>Where are my results stored? </li></ul><ul><li>a = 2 </li></ul><ul><li>b = 3; </li></ul><ul><li>c = a+b; The resul...
<ul><li>Row vector: >> a = [ 1 2 3 ]; </li></ul><ul><li>Column vector:  >> b = [ 1; 2; 3 ]; </li></ul><ul><li>Equally spac...
<ul><li>Try to type: </li></ul><ul><li>c = 2*a </li></ul><ul><li>d = 2+a; </li></ul><ul><li>f = a+c </li></ul><ul><li>g = ...
<ul><li>Further examples: </li></ul><ul><li>  a = [ 1 2 3 ]; </li></ul><ul><li>  b = [ 1; 2 ; 3 ]; </li></ul><ul><li>c = a...
<ul><li>Methods of creating matrices: </li></ul><ul><li>By coefficients >> A = [1 2 3; 4 5 6; 7 8 9]; </li></ul><ul><li>Ma...
<ul><li>Vectors (>> a = [1:5].^2;) </li></ul><ul><li>Single element >> a(1) </li></ul><ul><li>Multiple elements >> a([1 3]...
<ul><li>Try to type: </li></ul><ul><li>B = 2*A </li></ul><ul><li>C = 2+A </li></ul><ul><li>D = A+C </li></ul><ul><li>D = D...
<ul><li>Let us consider the following case: </li></ul><ul><li>A, B </li></ul><ul><li>A/B </li></ul><ul><li>AB </li></ul>Ma...
<ul><li>Matrices in block form </li></ul><ul><li>From matrices to vectors  </li></ul><ul><li>From vectors to matrices </li...
<ul><li>Size >> size(C) </li></ul><ul><li>Predominant size >> length(C) </li></ul><ul><li>Maximum (columns) >> max(A) </li...
Exercises <ul><li>Compute the approximate value of exp(1) </li></ul><ul><li>Compute the approximate value of exp(2) </li><...
<ul><li>Write in matrix notation the following system of equations: </li></ul><ul><li>Is this system singular? </li></ul><...
Exercises <ul><li>Solve the following system: </li></ul><ul><li>Find the inverse matrix of A </li></ul><ul><li>Solve the f...
<ul><li>What is a structure? </li></ul><ul><li>Example: chemical components (try) </li></ul><ul><li>>> comp(1).name = 'wat...
<ul><li>General form of  for -loops: </li></ul><ul><li>Example: </li></ul>Loops in Matlab Marco Lattuada – Statistical and...
<ul><li>Try the following: </li></ul><ul><li>>>  for  i = 0:5:100 </li></ul><ul><li>i </li></ul><ul><li>end </li></ul><ul>...
While Loops in Matlab <ul><li>General form of  while -loops: </li></ul><ul><li>Example: </li></ul>Marco Lattuada – Statist...
<ul><li>Try the following: </li></ul><ul><li>>> clear x; n = 5e4; x(1) = 2; </li></ul><ul><li>>> tic; for i = 2:n, x(i) = ...
Upcoming SlideShare
Loading in …5
×

Matlab

894 views

Published on

you can contact me for further details...
Engr. Muhamamd Abdul Rehman Iqbal
+92300-5576762 / acmeengspll@yahoo.com

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
894
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
54
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Matlab

  1. 1. Introduction to Matlab 7 Lesson I
  2. 2. How to connect to you folders <ul><li>Go to “My Computer” </li></ul><ul><li>In Tools, click on “Map Network Drive” </li></ul><ul><li>In the window that appears, type “d.ethz.chdfsusersall<nethz username>” </li></ul><ul><li>Select option “connect with another username” </li></ul><ul><li>Enter “d <nethz username>” </li></ul><ul><li>Enter your E-Mail password. </li></ul><ul><li>Remember: do not keep you data on the computer you are working on, as they can be deleted!!!! </li></ul>Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  3. 3. Matlab 7.0.4 - 7.10 <ul><li>What is Matlab? </li></ul><ul><li>Advantages over other choices: </li></ul>Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # Matlab is an interatcive system for numerical computation <ul><li>Quick and easy coding with high level language </li></ul><ul><li>Procedural coding (object-oriented supported) </li></ul><ul><li>Minimal attention required for data structure/declaration </li></ul><ul><li>Simple handling of vectors/matrixes </li></ul><ul><li>High quality graphics available </li></ul><ul><li>Full portability of the m-files </li></ul><ul><li>Built-in editing and debugging tools </li></ul><ul><li>Built-in functions (also on internet) </li></ul>
  4. 4. Matlab 7.0.4 - 7.10 <ul><li>Disadvantages: </li></ul>Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # <ul><li>Not optimal for symbolic calculations (choose Maple, Mathematica) </li></ul><ul><li>Not as fast as C (C++) and Fortran </li></ul><ul><li>Not the best choice for very computationally demanding problems (choose Fortran or C++ instead) </li></ul>
  5. 5. Matlab Introduction Window Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # File Structure Variable Editor Commands / Instructions Command History Variables / Structures Try it! Do not be too timid about your actions. All life is an experiment. The more you make, the better! Ralph Waldo Emerson
  6. 6. Variables Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # Rules: <ul><li>Names are case sensitive (“Marco” ≠ “marco”) </li></ul><ul><li>63 Characters max (1st character must be a letter) </li></ul><ul><li>Letters, numerals and sign “_” are valid characters </li></ul><ul><li>No Spaces in between </li></ul>Valid Examples a = 1 speed = 1500 BeamOutput_Type1 = v*Q*v’ name = ’John Smith’ Invalid Examples 2ndvariable = ’yes’ first one = 1 No need to specify the variable type
  7. 7. <ul><li>Try to type: </li></ul><ul><li>a = 2 </li></ul><ul><li>b = 3; </li></ul><ul><li>c = a+b; </li></ul><ul><li>d = c/2; </li></ul><ul><li>d </li></ul><ul><li>who </li></ul><ul><li>whos </li></ul><ul><li>clear </li></ul><ul><li>who </li></ul><ul><li>myName = ‘marco’ </li></ul>Variables Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # By pressing “w”+↑, the command who is recalled… Why these are arrays??? Every variable is an array!!!
  8. 8. <ul><li>Where are my results stored? </li></ul><ul><li>a = 2 </li></ul><ul><li>b = 3; </li></ul><ul><li>c = a+b; The result is stored in “c” </li></ul><ul><li>a+b The results in stored in “ans” </li></ul>Variables Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # 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
  9. 9. <ul><li>Row vector: >> a = [ 1 2 3 ]; </li></ul><ul><li>Column vector: >> b = [ 1; 2; 3 ]; </li></ul><ul><li>Equally spaced vector: >> c = 0:5:100; (or 0:100; or [0:100]) </li></ul><ul><li>Evenly spaced vector: >> d = linspace(0,100,21); </li></ul><ul><li>( vector = linspace(start,end,#elements) ) ( vector = logspace(start,end,#elements) ) </li></ul><ul><li> generates a vector from 10 start to 10 end </li></ul><ul><li>Transpose: >> e = d'; </li></ul>Vectors Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # If everything is an array, what is a vector ??? You should see: Fast help by: >> help linspace
  10. 10. <ul><li>Try to type: </li></ul><ul><li>c = 2*a </li></ul><ul><li>d = 2+a; </li></ul><ul><li>f = a+c </li></ul><ul><li>g = a*b </li></ul><ul><li>dot(a,b) </li></ul><ul><li>A = b*a </li></ul><ul><li>a*a </li></ul><ul><li>a^2 </li></ul><ul><li>a.^2 </li></ul><ul><li>a.*a </li></ul><ul><li>d = d./a </li></ul><ul><li>b = sqrt(b) </li></ul><ul><li>c = exp(c) </li></ul><ul><li>d = factorial(d) </li></ul>Vector's Arithmetic Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # Addition/multiplication by constants Dot product of vectors Addition 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
  11. 11. <ul><li>Further examples: </li></ul><ul><li> a = [ 1 2 3 ]; </li></ul><ul><li> b = [ 1; 2 ; 3 ]; </li></ul><ul><li>c = a*b </li></ul><ul><li>d = b*a </li></ul>Vector's Arithmetic II Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # (3,1)x(1,3)=(3,3) Matrix! (1,3)x(3,1)=(1,1) SCALAR (dot product) ! Never forget the rules for matrix multiplication!
  12. 12. <ul><li>Methods of creating matrices: </li></ul><ul><li>By coefficients >> A = [1 2 3; 4 5 6; 7 8 9]; </li></ul><ul><li>Matrix of zeros >> B = zeros(3,2); </li></ul><ul><li>Matrix of ones >> C = ones(2,3); </li></ul><ul><li>Identity matrix >> D = eye(3); </li></ul><ul><li>Random matrix >> R = rand(3,3); </li></ul><ul><li>Normally distr. random matrix >> RD = randn(3,3); </li></ul><ul><li>Matrix Characteristics: </li></ul><ul><li>Size >> size(A) </li></ul><ul><li>Length >> length(A) </li></ul>Arrays/Matrices Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  13. 13. <ul><li>Vectors (>> a = [1:5].^2;) </li></ul><ul><li>Single element >> a(1) </li></ul><ul><li>Multiple elements >> a([1 3]) </li></ul><ul><li>Range of elements >> a(2:3) </li></ul><ul><li>Last element >> a(end) </li></ul><ul><li>Matrices (>> A = a'*a;) </li></ul><ul><li>Single element >> A(1,3) </li></ul><ul><li>Submatrix >> A(2:3,2:3) </li></ul><ul><li>Matrix Column >> A(:,2) </li></ul><ul><li>Matrix Row >> A(3,:) </li></ul><ul><li>Rows intersected with columns >> A([2 3],[1 3 5]) </li></ul>Accessing Elements Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  14. 14. <ul><li>Try to type: </li></ul><ul><li>B = 2*A </li></ul><ul><li>C = 2+A </li></ul><ul><li>D = A+C </li></ul><ul><li>D = D’ </li></ul><ul><li>C(3,:) = [] </li></ul><ul><li>D(:,2) = [] </li></ul><ul><li>C*D </li></ul><ul><li>D*C </li></ul><ul><li>A^2 </li></ul><ul><li>A.^2 </li></ul><ul><li>E=2.^A </li></ul><ul><li>sqrt(A) </li></ul><ul><li>sqrtm(A) </li></ul><ul><li>A^(-1) </li></ul>Arithmetic with Matrices Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # Addition/multiplication by constants Addition of matrices Transpose Elimination of columns/lines Product between matrices Square of matrix Square of matrix's elements E i,j = 2^A i,j Square-root of matrix's elements Square-root of matrix (try: >> (sqrtm(A))^2) Inverse of A (equal to: >> inv(A)) Remember: product between matrices is usually not commutative!
  15. 15. <ul><li>Let us consider the following case: </li></ul><ul><li>A, B </li></ul><ul><li>A/B </li></ul><ul><li>AB </li></ul>Matrix Division Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # Two square matrices A divided by B, alternatively mrdevide(A,B) equivalent to A*inv(B) The two divisions are different, because the product between matrices is not commutative! alternatively mldevide(A,B) equivalent to inv(B)*A
  16. 16. <ul><li>Matrices in block form </li></ul><ul><li>From matrices to vectors </li></ul><ul><li>From vectors to matrices </li></ul><ul><li>Diagonal matrices </li></ul><ul><li>Meshes </li></ul><ul><li>From one element/column/line </li></ul>Other Ways of Creating Matrices Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  17. 17. <ul><li>Size >> size(C) </li></ul><ul><li>Predominant size >> length(C) </li></ul><ul><li>Maximum (columns) >> max(A) </li></ul><ul><li>Maximum (general) >> [val,elem] = max(A) </li></ul><ul><li>Maximum (overall) >> max(A(:)) </li></ul><ul><li>Sum of elements >> sum(A) </li></ul><ul><li>Mean value >> mean(A(:)) </li></ul><ul><li>Determinant >> det(A) </li></ul><ul><li>Inverse >> inv(A) </li></ul><ul><li>Condition Number >> cond(A) </li></ul><ul><li>Norm >> norm(A,i) </li></ul>Operators for Matrices Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # min(A) = -max(-A) var(A), std(A), …
  18. 18. Exercises <ul><li>Compute the approximate value of exp(1) </li></ul><ul><li>Compute the approximate value of exp(2) </li></ul><ul><li>Compute the cross-product of u(1,3,2)  v(-1,1,2) </li></ul>Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  19. 19. <ul><li>Write in matrix notation the following system of equations: </li></ul><ul><li>Is this system singular? </li></ul><ul><li>How would you solve this system? </li></ul>Solution of Linear Algebraic Systems Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # Shortcut
  20. 20. Exercises <ul><li>Solve the following system: </li></ul><ul><li>Find the inverse matrix of A </li></ul><ul><li>Solve the following system: </li></ul>Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  21. 21. <ul><li>What is a structure? </li></ul><ul><li>Example: chemical components (try) </li></ul><ul><li>>> comp(1).name = 'water'; </li></ul><ul><li>>> comp(1).Mw = 18.02; </li></ul><ul><li>>> comp(1).density = 1; </li></ul><ul><li>>> comp(2).name = 'ethanol'; </li></ul><ul><li>>> comp(2).Mw = 46.06; </li></ul><ul><li>>> comp(2).density = 0.789; </li></ul>Structures in Matlab Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # Structures are arrays whose names have dot-separated parts. They can be used to store information of different kinds together in a hierarchical structure.
  22. 22. <ul><li>General form of for -loops: </li></ul><ul><li>Example: </li></ul>Loops in Matlab Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # <ul><li>for variable = expression </li></ul><ul><ul><li>statements; </li></ul></ul><ul><li>end </li></ul>
  23. 23. <ul><li>Try the following: </li></ul><ul><li>>> for i = 0:5:100 </li></ul><ul><li>i </li></ul><ul><li>end </li></ul><ul><li>>> v = [0 4 5 11]; </li></ul><ul><li>>> for i = v, i, end </li></ul><ul><li>>> n = 1e6; x = rand(n,1); </li></ul><ul><li>>> tic; s = 0; for i = 1:n, s = s+x(i)^2; end , s, toc </li></ul><ul><li>>> tic; s = sum(x.^2); s, toc </li></ul>Examples of Loops Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # A technique is a trick that works Giancarlo Rota
  24. 24. While Loops in Matlab <ul><li>General form of while -loops: </li></ul><ul><li>Example: </li></ul>Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # <ul><li>while expression </li></ul><ul><ul><li>statements; </li></ul></ul><ul><li>end </li></ul>The statement is executed as long as the expression is true The statements are executed an indefinite number of times
  25. 25. <ul><li>Try the following: </li></ul><ul><li>>> clear x; n = 5e4; x(1) = 2; </li></ul><ul><li>>> tic; for i = 2:n, x(i) = x(i-1)+2*i; end, toc; </li></ul><ul><li>>> x1 = zeros(1,n); x1(1) = 2; </li></ul><ul><li>>> tic; for i = 2:n, x1(i) = x1(i-1)+2*i; end, toc; </li></ul><ul><li>>> v = 2*linspace(1,n,n); </li></ul><ul><li>>> tic; x2 = cumsum(v); toc; </li></ul>Examples of Loops Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # Pre- allocate arrays Vectorize and use pre-defined functions 20 times faster 129 times faster Make it right before you make it faster Elements of Programming Style (1978)

×