Your SlideShare is downloading. ×
  • Like
Matlab
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Matlab

  • 730 views
Published

you can contact me for further details... …

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

Published in Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
730
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
43
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Introduction to Matlab 7 Lesson I
  • 2. How to connect to you folders
    • Go to “My Computer”
    • In Tools, click on “Map Network Drive”
    • In the window that appears, type “d.ethz.chdfsusersall<nethz username>”
    • Select option “connect with another username”
    • Enter “d <nethz username>”
    • Enter your E-Mail password.
    • Remember: do not keep you data on the computer you are working on, as they can be deleted!!!!
    Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  • 3. Matlab 7.0.4 - 7.10
    • What is Matlab?
    • Advantages over other choices:
    Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # 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)
  • 4. Matlab 7.0.4 - 7.10
    • Disadvantages:
    Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
    • 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 (choose Fortran or C++ instead)
  • 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. Variables Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # Rules:
    • 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
    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.
    • Try to type:
    • a = 2
    • b = 3;
    • c = a+b;
    • d = c/2;
    • d
    • who
    • whos
    • clear
    • who
    • myName = ‘marco’
    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.
    • Where are my results stored?
    • a = 2
    • b = 3;
    • c = a+b; The result is stored in “c”
    • a+b The results in stored in “ans”
    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.
    • Row vector: >> a = [ 1 2 3 ];
    • Column vector: >> b = [ 1; 2; 3 ];
    • Equally spaced vector: >> c = 0:5:100; (or 0:100; or [0:100])
    • Evenly spaced vector: >> d = linspace(0,100,21);
    • ( vector = linspace(start,end,#elements) ) ( vector = logspace(start,end,#elements) )
    • generates a vector from 10 start to 10 end
    • Transpose: >> e = d';
    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.
    • Try to type:
    • c = 2*a
    • d = 2+a;
    • f = a+c
    • g = a*b
    • dot(a,b)
    • A = b*a
    • a*a
    • a^2
    • a.^2
    • a.*a
    • d = d./a
    • b = sqrt(b)
    • c = exp(c)
    • d = factorial(d)
    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.
    • Further examples:
    • a = [ 1 2 3 ];
    • b = [ 1; 2 ; 3 ];
    • c = a*b
    • d = b*a
    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.
    • Methods of creating matrices:
    • By coefficients >> A = [1 2 3; 4 5 6; 7 8 9];
    • Matrix of zeros >> B = zeros(3,2);
    • Matrix of ones >> C = ones(2,3);
    • Identity matrix >> D = eye(3);
    • Random matrix >> R = rand(3,3);
    • Normally distr. random matrix >> RD = randn(3,3);
    • Matrix Characteristics:
    • Size >> size(A)
    • Length >> length(A)
    Arrays/Matrices Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  • 13.
    • Vectors (>> a = [1:5].^2;)
    • Single element >> a(1)
    • Multiple elements >> a([1 3])
    • Range of elements >> a(2:3)
    • Last element >> a(end)
    • Matrices (>> A = a'*a;)
    • Single element >> A(1,3)
    • Submatrix >> A(2:3,2:3)
    • Matrix Column >> A(:,2)
    • Matrix Row >> A(3,:)
    • Rows intersected with columns >> A([2 3],[1 3 5])
    Accessing Elements Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  • 14.
    • Try to type:
    • B = 2*A
    • C = 2+A
    • D = A+C
    • D = D’
    • C(3,:) = []
    • D(:,2) = []
    • C*D
    • D*C
    • A^2
    • A.^2
    • E=2.^A
    • sqrt(A)
    • sqrtm(A)
    • A^(-1)
    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.
    • Let us consider the following case:
    • A, B
    • A/B
    • AB
    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.
    • Matrices in block form
    • From matrices to vectors
    • From vectors to matrices
    • Diagonal matrices
    • Meshes
    • From one element/column/line
    Other Ways of Creating Matrices Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  • 17.
    • Size >> size(C)
    • Predominant size >> length(C)
    • Maximum (columns) >> max(A)
    • Maximum (general) >> [val,elem] = max(A)
    • Maximum (overall) >> max(A(:))
    • Sum of elements >> sum(A)
    • Mean value >> mean(A(:))
    • Determinant >> det(A)
    • Inverse >> inv(A)
    • Condition Number >> cond(A)
    • Norm >> norm(A,i)
    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. Exercises
    • 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)
    Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  • 19.
    • Write in matrix notation the following system of equations:
    • Is this system singular?
    • How would you solve this system?
    Solution of Linear Algebraic Systems Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page # Shortcut
  • 20. Exercises
    • Solve the following system:
    • Find the inverse matrix of A
    • Solve the following system:
    Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
  • 21.
    • What is a structure?
    • Example: chemical components (try)
    • >> comp(1).name = 'water';
    • >> comp(1).Mw = 18.02;
    • >> comp(1).density = 1;
    • >> comp(2).name = 'ethanol';
    • >> comp(2).Mw = 46.06;
    • >> comp(2).density = 0.789;
    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.
    • General form of for -loops:
    • Example:
    Loops in Matlab Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
    • for variable = expression
      • statements;
    • end
  • 23.
    • Try the following:
    • >> for i = 0:5:100
    • i
    • end
    • >> v = [0 4 5 11];
    • >> for i = v, i, end
    • >> n = 1e6; x = rand(n,1);
    • >> tic; s = 0; for i = 1:n, s = s+x(i)^2; end , s, toc
    • >> tic; s = sum(x.^2); s, toc
    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. While Loops in Matlab
    • General form of while -loops:
    • Example:
    Marco Lattuada – Statistical and Numerical Methods for Chemical Engineers Matlab Introduction Course – Page #
    • while expression
      • statements;
    • end
    The statement is executed as long as the expression is true The statements are executed an indefinite number of times
  • 25.
    • Try the following:
    • >> clear x; n = 5e4; x(1) = 2;
    • >> tic; for i = 2:n, x(i) = x(i-1)+2*i; end, toc;
    • >> x1 = zeros(1,n); x1(1) = 2;
    • >> tic; for i = 2:n, x1(i) = x1(i-1)+2*i; end, toc;
    • >> v = 2*linspace(1,n,n);
    • >> tic; x2 = cumsum(v); toc;
    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)