You are on page 1of 15

MATLAB

MATLAB stands for Matrix Laboratory. Available in the form of an interpreter, compiler, and a package High performance Rapid Application Development tool for Scientific Computing Available in the form of tool boxes Its having an interface of Fortran and C & C++ programming language Tool boxes available are: MATLAB Simulink Stateflow Real time workshop Control System System identification Robust Control Optimization Spline Signal Processing Mu Analysis Neural Network Image Processing Non Linear Control Design Statistics Higher Order Spectral Analysis Frequency Domain identification Model Predictive Control Fuzzy Logic Digital Signal Processing Fixed Point Blockset QFT Control Design LMI Control Financial Ordinary Differential Equation Partial Differential Equation Wavelet
1

Mapping Nag Foundation Symbolic Math Power System Blockset Communications Data Base Other Libraries We shall be using the general MATLAB engine. In this language, there is no type declaration and no dimensioning of variables, each is going to be viewed as a matrix. Variables Consists of a letter, followed by any number of letters, digits or underscores. Uses only the first 31 characters of a variable name. Matlab is case sensitive. Numbers Imaginary numbers use either i or j as a suffix. E.g 3.105i Operators +, -, *, /, ^, , () Functions some special functions provide values of useful constants. pi 3.141. i imaginary unit -1 j same as i eps Floating point relative precision 2-52 realmin Smallest floating-point number 2-1022 realmax Largest floating-point number (2-)21023 Inf Infinity NaN Not-a-Number

Working with matrices Matlab provide functions that generates basic matrices ZEROS Zeros array. ZEROS(N) is an N-by-N matrix of zeros. ZEROS(M,N) or ZEROS([M,N]) is an M-by-N matrix of zeros. ZEROS(M,N,P,...) or ZEROS([M N P ...]) is an M-by-N-by-P-by-... array of zeros. ZEROS(SIZE(A)) is the same size as A and all zeros. ONES Ones array. ONES(N) is an N-by-N matrix of ones. ONES(M,N) or ONES([M,N]) is an M-by-N matrix of ones. ONES(M,N,P,...) or ONES([M N P ...]) is an M-by-N-by-P-by-... array of ones. ONES(SIZE(A)) is the same size as A and all ones. RAND Uniformly distributed random elements from a ( 0 1 ) range. RAND(N) is an N-by-N matrix with random entries, chosen from a uniform distribution on the interval (0.0,1.0). RAND(M,N) and RAND([M,N]) are M-by-N matrices with random entries. RAND(M,N,P,...) or RAND([M,N,P,...]) generate random arrays. RAND with no arguments is a scalar whose value changes each time it is referenced. RAND(SIZE(A)) is the same size as A. RANDN Normally distributed random elements ( mean is 0 and variance 1) RANDN(N) is an N-by-N matrix with random entries, chosen from a normal distribution with mean zero and variance one. RANDN(M,N) and RANDN([M,N]) are M-by-N matrices with random entries. RANDN(M,N,P,...) or RANDN([M,N,P...]) generate random arrays. RANDN with no arguments is a scalar whose value changes each time it is referenced. RANDN(SIZE(A)) is the same size as A.

EYE Identity matrix EYE(N) is the N-by-N identity matrix. EYE(M,N) or EYE([M,N]) is an M-by-N matrix with 1's on the diagonal and zeros elsewhere. EYE(SIZE(A)) is the same size as A. CLEAR Clear variables and functions from memory. CLEAR removes all variables from the workspace. CLEAR VARIABLES does the same thing. CLEAR GLOBAL removes all global variables. CLEAR FUNCTIONS removes all compiled M-functions. CLEAR MEX removes all links to MEX-files. CLEAR ALL removes all variables, globals, functions and MEX links. CLEAR VAR1 VAR2 ... clears the variables specified. The wildcard character '*' can be used to clear variables that match a pattern. For instance, CLEAR X* clears all the variables in the current workspace that start with X. If X is global, CLEAR X removes X from the current workspace, but leaves it accessible to any functions declaring it global. CLEAR GLOBAL X completely removes the global variable X. CLEAR FUN clears the function specified. If FUN has been locked by MLOCK it will remain in memory. CLEAR ALL also has the side effect of removing all debugging breakpoints since the breakpoints for a file are cleared whenever the m-file changes or is cleared. Use the functional form of CLEAR, such as CLEAR('name'), when the variable name or function name is stored in a string.

Load command load filename A=[ 2 4 5 6]; Matrices functions sum , transpose and diag sum(A) gives sum of A column wise A gives the transpose of A Diag(A) gives the diagonal elements value. The colon operator Colon plays a very important role in Matlab. This occurs in several different forms. e.g, 1:10 gives integers 1 to 10 i.e 1 2 3 4 5 6 7 8 9 10 100 : -7.50 and 0 : pi/4 : pi subscript expressions A(1:k,j) refers first k elements of the jth columns of A. sum(A(1:4,4)) gives sum of the fourth column. The colon by itself refers to all the elements in a row or column of a matrix and the keyword end refers to the last row or column. sum(A(: , end)) gives the sum of the elements in the last column of A.

concatenation The pair of [ ] , is the concatenation operator. Let us start with 4-by-4 A A= 16 3 5 10 9 6 4 15 2 13 11 8 7 12 14 1

and form B = [ A A+32; A+48 A+16 ] The result is an 8-by-8 matrix obtained by joining the four submatrices. [ 16 3 2 13 | 48 35 34 45 5 10 11 8 | 37 42 43 40 9 6 7 12 | 41 38 39 44 4 15 14 1 | 36 47 46 33 --------------------|----------------------64 51 50 61 | 32 19 18 29 53 58 59 56 | 21 26 27 24 57 54 55 60 | 25 22 23 28 52 63 62 49 | 20 31 30 17 ]

B=

sum(B) ans = 260 260 260 260 260 260 260 260 Deleting rows and columns % storage in Fortran is COLUMN wise % storage in C & C++ is ROW wise % storage in MATLAB is COLUMN wise X= A; X(: , 2) = []; X(1, 2) = [] X(2 : 2 : 10) = [ ] X = 16 9 2 7 13 12
6

% deleting the 2nd column of X. % result in an error % however using a single subscript reshape the % remaining elements into a row.

To log the session journal in a file diary filename

To stop recording diary off echo on echo off Help and online documentation help topicname or functionname Integration , Differentiation int(f) or diff(f) int(f,0,2*pi)

syms a b c x S= a*x^2+b*x+c; int(S) ans = 1/3*a*x^3+1/2*b*x^2+c*x diff(S) ans = 2*a*x+b

Solving algebraic equations


If S is a symbolic expression solve(S) find the values of symbolic variable in S.

syms a b c x S= a*x^2+b*x+c; solve(S) ans = [ 1/2/a*(-b+(b^2-4*a*c)^(1/2))] [ 1/2/a*(-b-(b^2-4*a*c)^(1/2))] ordinary differential equations e.g dy --- = dx 1+y^2 gives ans = tan(t + c1)

dsolve( Dy=1+y^2),

with initial conditions y(0)=1 y = dsolve(Dy=1+y^2,y(0)=1) gives y= tan(t + 1/4*pi) y = dsolve('Dy=1+y^2','y(0)=1','x') gives y = tan(x + 1/4*pi)
8

Array Construction Two important functions are linspace & logspace LINSPACE Linearly spaced vector. LINSPACE(x1, x2) generates a row vector of 100 linearly equally spaced points between x1 and x2. LINSPACE(x1, x2, N) generates N points between x1 and x2. LOGSPACE logarithmically spaced vector. LOGSPACE(d1, d2) generates a row vector of 50 logarithmically equally spaced points between decades 10^d1 and 10^d2. If d2 is pi, then the points are between 10^d1 and pi. LOGSPACE(d1, d2, N) generates N points. Relational operators The six relational operators are <, <=, >, >=, ==, and ~=. A < B does element by element comparisons between A and B and returns a matrix of the same size with elements set to one where the relation is true and

elements set to zero where it is not. A and B must have the same dimensions (or one can be a scalar). Logical operators & Logical AND. A & B is a matrix whose elements are 1's where both A and B have non-zero elements, and 0's where either has a zero element. A and B must have the same dimensions (or one can be a scalar). | Logical OR. A | B is a matrix whose elements are 1's where either A or B has a non-zero element, and 0's where both have zero elements. A and B must have the same dimensions (or one can be a scalar). ~ Logical complement (NOT). ~A is a matrix whose elements are 1's where A has zero elements, and 0's where A has non-zero elements. xor Exclusive OR. xor(A,B) is 1 where either A or B, but not both, is nonzero.
10

Control statements For FOR used to repeat statements a specific number of times. The general form of a FOR statement is: FOR variable = expr, statement, ..., statement END The columns of the expression are stored one at a time in the variable and then the following statements, up to the END, are executed. The expression is often of the form X:Y, in which case its columns are simply scalars. Some examples (assume N has already been assigned a value). FOR I = 1 : N, FOR J = 1 : N, A (I , J) = 1/(I+J-1); END END FOR S = 1.0 : -0.1 : 0.0, END steps S with increments of -0.1 FOR E = EYE(N), ... END sets E to the unit N-vectors. Long loops are more memory efficient when the colon expression appears in the FOR statement since the index vector is never created.
11

The BREAK statement can be used to terminate the loop prematurely. While WHILE is used to repeat statements an indefinite number of times. The general form of a WHILE statement is: WHILE expression statements END The statements are executed while the real part of the expression has all non-zero elements. The expression is usually the result of expr rop expr where rop is ==, <, >, <=, >=, or ~=. The BREAK statement can be used to terminate the loop prematurely. For example (assuming A already defined): E = 0*A; F = E + eye(size(E)); N = 1; while norm(E+F-E,1) > 0, E = E + F; F = A*F/N; N = N + 1; End

12

If_else_end construction IF statement condition. The general form of the IF statement is IF expression statements ELSEIF expression statements ELSE statements END The statements are executed if the real part of the expression has all non-zero elements. The ELSE and ELSEIF parts are optional. Zero or more ELSEIF parts can be used as well as nested IF's. The expression is usually of the form expr rop expr where rop is ==, <, >, <=, >=, or ~=. Example if I == J A(I,J) = 2; elseif abs(I-J) == 1 A(I,J) = -1; else A(I,J) = 0; end

13

SWITCH The general form of the SWITCH statement is: SWITCH switch_expr CASE case_expr, statement, ..., statement CASE {case_expr1, case_expr2, case_expr3,...} statement, ..., statement ... OTHERWISE, statement, ..., statement END Solving set of Linear equations A .X = b This will have a unique answer whenever the determinant of the matrix A is non zero. i.e A = [ 1 2 3; 4 5 6; 7 8 0 ];

B = [366; 804; 351] det(A) ans = 27

14

We can find the solution by a number of methods. 1. Gaussian elimination 2. LU factorization (also called left division of A into b, X = A\b) 3. Direct use of A-1 X = inv(A) * b X= [25.00; 22.00; 99.00] Runge-Kutta Method A fourth order Runge-Kutta method for solving simultaneous first order differential equations is given by Yn+1 = Yn + 1/6(k1 + 2 k2 + 2 k3 + k4 ) Where k1 = h f(xn , yn ) k2 = h f(xn + 1/2h , yn + 1/2 k1) k3 = h f(xn + 1/2h, yn + 1/2 k2 ) k4 = h f(xn + h , yn + k3) This has a truncation error O(h5) and is useful for starting a fourth order multi-step method.

15

You might also like