AOS Linux Tutorial Matlab Michael Havas Dept. of Atmospheric and Oceanic Sciences McGill University October 19, 2010
Outline 1 Introduction 2 Matlab basics Built-in functions Vectors Matrices Saving and Loading 3 Plotting 4 Systems of Linear Equations 5 Matlab scripts 6 Next time
Matlab History Matlab History Created in the late 1970’s by Cleve Moler. Designed to give students access to LINPACK and EISPACK without having to learn fortran. Rewritten and released by Mathworks in 1984 under a commercial license.
What is Matlab and what is it good for? What’s Matlab? Is a numerical computing environment and programming language. Although concentrated on numerical computing, symbolic computing is possible. What’s it good for Matrix manipulations. Plotting of functions and data. Implementation of algorithms. Creation of user interfaces. Interfacing with programs written in other languages including C, C++ and Fortran.
Matlab tutorial Credit where credit is due Credit The majority (ALL) of this tutorial is taken from: http://www.maths.dundee.ac.uk/~ftp/na-reports/ MatlabNotes.pdf Much thanks to Dundee University for providing such a great tutorial to the public.
Outline 1 Introduction 2 Matlab basics Built-in functions Vectors Matrices Saving and Loading 3 Plotting 4 Systems of Linear Equations 5 Matlab scripts 6 Next time
Matlab basics Matlab as a calculator Operators and order of operations () Parentheses. ˆ Exponent. *, / Multiplication and division. +, - Addition and subtraction. Example >> 2 + 3 / 4 ∗ 5 ans = 5.7500 >>
Matlab basics Numbers Kinds of numbers Matlab recognizes several different kinds of numbers: Type Examples Integer 1362, 217897 Real 1.234, -10.76 Complex 3.21 - 4.3i Inf Infinity NaN Not a Number, 0/0 − 1 . 3412 e + 03 = − 1 . 3412 × 10 3 “e” Notation Note Matlab does all operations in double-precision.
Matlab basics Formats The format command The format command controls how Matlab prints numbers. Some Command Example of Output >> format short 31.4162 examples; >> format short -e 3.1416e+01 >> forat long e 3.141952653589793e+01 >> format blank 31.42 Note Matlab does all operations in double-precision.
Matlab basics Variables Defining variables We can use our own names to Predefined variables define variables. Variable names The variable ans is a predefined must begin with a letter and variable defined as the answer to contain only letters and numbers. the previous query. For example: >> x = 3 − 2ˆ4 >> 3 − 2ˆ4 x = ans = − 13 − 13 >> y = x ∗ 5 >> ans ∗ 5 y = ans = − 65 − 65 Avoid using reserved words like pi , sin , . . .
Matlab basics Suppressing output Suppressing output To suppress output of a given expression, simply append a ; . For example: >> x = 13; >> y = 5 ∗ x , z=xˆ2+y y = − 65 z = 104 >>
Matlab basics Elementary and trigonometric functions Trigonometric functions Matlab knows sin , cos and tan and their inverse asin , acos and atan . These functions expect paramters in radians. For example: >> x = 5 ∗ cos ( pi /6) , y=5 ∗ s i n ( pi /6) x = 4.3301 y = 2.500 >> acos ( x /5) , asin ( y ) x = 0.5236 y = 0.5236 >> pi /6 ans = 0.5236 Elementary functions These include sqrt , exp , log , log10 .
Matlab basics Vectors Row vectors Row vectors are written [1 2 3 4] and can be arbitrarily large. Be careful with spaces [1+ 2 3 4] is not equal to [1 +2 3 4] . Suggest using commas between values. Can use operators in vector assignment: [ sin ( pi ), cos ( pi ), tan ( pi ), 17] . Column vectors Column vectors are written [1; 2; 3] or >> [ 1 2 3] Can be arbitrarily large. Can use operators in vector assignment.
Matlab basics Vectors Getting and setting elements in a vector Use the () operator. For example, >> v1 =[1 ,2 ,3]; v2 = [ 4 ; 5 ; 6 ] ; >> v1 (2) ans = 2 >> v2 (2) ans = 5 >> v2 (2) = 17 v2 = 4 17 6 >>
Matlab basics Vectors Colon notation Is used as a shortcut to creating row vectors. Generally, a : b : c , produces a row vector of entries starting at a , incrementing by the value b until it gets to c . Transposing We can convert a row vector to a column vector using the transpose operator ’ . For example: >> v = 1:2 v = 1 2 >> v ’ ans = 1 2 Note that if v is complex, then ’ returns the complex conjugate transpose. .’ will return the regular transpose.
Matlab basics Vectors Vector operators and functions Name Symbol Example Vector addition + [1 , 2 , 3] + [4 , 5 , 6] Vector subtraction [1 , 2 , 3] − [4 , 5 , 6] - Scalar multiplication * 10 ∗ [1 , 2 , 3] Vector concatenation [[1 , 2 , 3] , [4 , 5 , 6]] [ ] Vector sorting sort ([3 , 2 , 1]) sort() Vector multiplication * [1 , 2 , 3] ∗ [1; 2; 3] Vector dot product [1 , 2 , 3] . ∗ [1 , 2 , 3] .* Vector dot division ./ [1 , 2 , 3] ./ [1 , 2 , 3] Vector dot power [1 , 2 , 3].ˆ[1 , 2 , 3] .^ Vector length length() length ([1 , 2 , 3])
Matlab basics Matrices Syntax � 1 � 2 3 The matrix A = can be defined in Matlab as: 4 5 6 >> A = [1 , 2 , 3 4 , 5 , 6] A = 1 2 3 4 5 6 >> A = [1 , 2 , 3; 4 , 5 , 6] A = 1 2 3 4 5 6 > > >
Matlab basics Matrices size of a matrix The size of a matrix can be computed using the size function. It returns a row vector of length two with the first element being the number of rows in the matrix and the second element being the number of columns. For example, >> [ numRows , numCols ] = s i z e (A) numRows = 2 numCols = 3 Transposing a matrix The transpose of a matrix is done using the ’ operator. For example: >> A’ ans = 1 4 2 5 3 6
Matlab basics Matrices Special matrices ones ( m , n ) An m × n matrix of ones. zeros ( m , n ) An m × n matrix of zeros. eye ( n ) The n × n identity matrix. diag ( ⌊ d 1 , . . . , d n ⌋ ) A diagonal n × n matrix with elements d 1 , d 2 , . . . , d n along the diagonal. diag(A) also returns the diagonal elements of a matrix A n × n
Matlab basics Matrices Constructing Matrices It is often convenient to build matrices from smaller ones. For example: >> C=[0 1; 3 − 2; 4 2 ] ; x =[8; − 4;1]; >> G = [C x ] G = 0 1 8 3 − 2 − 4 4 2 1 >> J = [ 1 : 4 ; 5 : 8 ; 9 : 1 2 ; 20 0 5 4 ] ; >> K = [ diag ( 1 : 4 ) J ; J ’ zeros ( 4 , 4 ) ] K = 1 0 0 0 1 2 3 4 0 2 0 0 5 6 7 8 0 0 3 0 9 10 11 12 0 0 0 4 20 0 5 4 1 5 9 20 0 0 0 0 2 6 10 0 0 0 0 0 3 7 11 5 0 0 0 0 4 8 12 4 0 0 0 0
Matlab basics Matrices Extracting an element from a matrix To extract an element from a matrix, you use A ( i , j ) where A is a matrix, i is the row you are interested in and j is the column. Extracting more than just an element A (: , j ) Extracts the j’th column. A (: , j 1 : j 2 ) Extracts the j 1 ’th to the j 2 ’th columns. A ( i , :) Extracts the i’th column. A ( i 1 : i 2 ; j 1 : j 2 ) Extracts the region defined by rows i 1 to i 2 and colmns j 1 and j 2
Matlab basics Matrices Sparse matrices Example Matlab defines a sparse matrix For example, we can represent using three vectors: 0 10 0 0 i A vector of length 0 0 0 0 n defining the row A = 0 0 11 0 as: indices of non-zero 0 0 0 0 elements. 0 0 0 12 j A vector of length >> i = [1 , 3 , 5 ] ; n defining the >> j = [ 2 , 3 , 4 ] ; colunn indices of >> v = [10 11 1 2 ] ; non-zero elements >> S = sparse ( i , j , v ) S = v A vector of length n (1 ,2) 10 defining the values (3 ,3) 11 at each index (5 ,4) 12 defined by i and j .
Matlab basics Matrices Matrix operations Name Symbol Example Matrix addition + [1 , 2; 3 , 4] + [5 , 6; 7 , 8] Matrix subtraction [1 , 2; 3 , 4] − [5 , 6; 7 , 8] - Scalar multiplication 10 ∗ [1 , 2; 3 , 4] * Matrix multiplication * [1 , 2; 3 , 4; 5 , 6] ∗ [1 , 2 , 3; 4 , 5 , 6] Matrix norm norm ([1 , 2; 3 , 4]) norm()
Matlab basics Saving and Loading Saving and loading Keep a diary of everything you type by issuing diary filename.txt Turn off and on using diary off and diary on respectively. Save your session by issuing: save thisSession where thissession is a filename. Load your session by issuing: load savedSession where savedSession is a previously saved session. See which variables are in use by using the whos command.
Outline 1 Introduction 2 Matlab basics Built-in functions Vectors Matrices Saving and Loading 3 Plotting 4 Systems of Linear Equations 5 Matlab scripts 6 Next time
Plotting Plotting elementary functions An example Say we want to plot y = sin (3 π x ) for 0 ≤ x ≤ 1. We do this by sampling the function for a sufficiently large number of points and joining these points with lines. Say we want to have the points evenly separated. For N number of points, we then have vector >> N=100; x = 0:1/N:1; . We can then say >> y = sin (3 ∗ pi ∗ x) . Now that we have x and y vectors, we can plot them using >> plot (x,y) .
Recommend
More recommend