Você está na página 1de 62

Centre for Computer Technology

ICT114 Mathematics for Computing


Week 6

Fundamentals of MATLAB

Objectives
Review Week5 MATLAB workspace Basic Operations Matrix and Array Operations Control statements Script files M-files

March 20, 2012

Copyright Box Hill Institute

Queue Characteristics
Queueing systems are characterized by five components
Arrival pattern of customers Service pattern Number of servers The capacity of the facility to hold customers The order in which customers are served

March 20, 2012

Copyright Box Hill Institute

Kendalls notation
Kendalls notation for specifying a queue characteristics is v/ w/ x/ y/ z v indicates arrival pattern w denotes service pattern x denotes the number of servers y represents system capacity (number of customers) z designates queue discipline
March 20, 2012

Copyright Box Hill Institute

Some notations for v/ w/ z


Queue characteristic Symbol
D M Ek (k = 1, 2, 3..) G FIFO LIFO SIRO PRI GD

Meaning
Deterministic Exponentially distributed Erlang type distributed Any other distribution First in first out Last in first out Service in random order Priority ordering Any other ordering

Interarrival time
or

Service time

Queue discipline

March 20, 2012

Copyright Box Hill Institute

Example
Write an algorithm to determine a students final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.

(Algorithms and Flowcharts)

March 20, 2012

Copyright Box Hill Institute

Example - Pseudocode

Input a set of 4 marks Calculate their average by summing and dividing by 4 if average is below 50 Print FAIL else Print PASS
(Algorithms and Flowcharts)

March 20, 2012

Copyright Box Hill Institute

Example - Algorithm
Step 1: Step 2: Step 3: Input M1,M2,M3,M4 GRADE (M1+M2+M3+M4)/4 if (GRADE < 50) then Print FAIL else Print PASS endif
(Algorithms and Flowcharts)

March 20, 2012

Copyright Box Hill Institute

Flowchart - Example
START Input M1,M2,M3,M4

GRADE(M1+M2+M3+M4)/4

Step 1: Input M1,M2,M3,M4 Step 2: GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then Print FAIL else Print PASS endif
Y (Algorithms and Flowcharts)

IS GRADE<5 0

PRINT PASS

PRINT FAIL

STOP
March 20, 2012

Copyright Box Hill Institute

Centre for Computer Technology

Fundamentals of MATLAB

Introduction
MATLAB stands for Matrix Laboratory It is a Numerical Computation software for Engineering and Scientific calculations Developed by John Little and Cleve Moler of MathWorks, Inc. The basic Data Type is a Matrix, which doesnt require dimensioning MATLAB can be used to generate a variety of Graphs

March 20, 2012

Copyright Box Hill Institute

March 20, 2012

Copyright Box Hill Institute

MATLAB Variables
Legal variable names Begin with one of az or AZ Have remaining characters chosen from az, AZ, 09, or _ Maximum length of 63 characters (ver 6.5) Should not be the name of a built-in variable, built-in-function, or user-defined function
March 20, 2012

Copyright Box Hill Institute

Basic Operations

The basic data type is a numerical rectangular matrix with real or complex numbers MATLAB statements are of the form variable = expression Expressions typed manually (typed by user) are interpreted immediately and evaluated If the statements ends with a semicolon ; , it is evaluated, but the display is suppressed
Copyright Box Hill Institute

March 20, 2012

Basic Operations

The variables are case sensitive. casesen off turns this off exit or quit, are the commands to exit MATLAB help, is for online help help function name, is to obtain information on a specific function lookfor, searches the summary information for each function clc clears the command window
Copyright Box Hill Institute

March 20, 2012

Matrices and Vectors


All

Matlab variables are matrices A Matlab vector is a matrix with one row or one column A Matlab scalar is a matrix with one row and one column

March 20, 2012

Copyright Box Hill Institute

Basic Operations

A matrix 1 2 3 A= 2 3 4 is represented by 3 4 5

A = [1 2 3; 2 3 4; 3 4 5]; or A=[123 234 3 4 5 ];


March 20, 2012

Copyright Box Hill Institute

Basic Operations

A row vector B = [ 6 9 12 15 18] is represented by B = [ 6 9 12 15 18] or B = [6, 9, 12, 15, 18] A column vector C=6 C=[6 9 9 12 is represented by 12 15 15 18 18] C = [6;9;12;15;18 ]

or

March 20, 2012

Copyright Box Hill Institute

Matrix Operations

The basic matrix operations are addition(+), subtraction(-), multiplication(*) and conjugate transpose () There are two forms of matrix division, the left inverse operator (\) and the right inverse operator (/) size(A) is used to obtain the size of a matrix A, the result will be a row matrix with two values. The is the number of rows and the second the number of columns of A
Copyright Box Hill Institute

March 20, 2012

Matrix Operations

The sum and difference of matrices with same dimensions can be determined For two matrices E and F with same dimensions E = [7 2 3; 4 3 6; 8 1 5]; F = [1 4 2; 6 7 5; 1 9 1]; The sum H = E + F The matrix H will appear as H= 8 6 5 10 10 11 9 10 6
Copyright Box Hill Institute

March 20, 2012

Matrix Operations

For the matrices E and F the difference G=EF The difference will appear as G= 6 -2 1 -2 -4 1 7 -8 4 A scalar matrix may also be added or subtracted from a matrix in a similar manner
Copyright Box Hill Institute

March 20, 2012

Matrix Operations

For two matrices X (n x m) and Y (i x j) Matrix Multiplication is defined if the innermost operands of the two operands are the same, i.e. in the above case m=i For the matrices E and F (both 3x3), the product Q = E*F will appear as Q= 22 69 27 28 91 29 19 84 26 The matrix can be multiplied by a scalar in a similar fashion
Copyright Box Hill Institute

March 20, 2012

Matrix Operations

Matrix division can be either left inverse operator (\) or right inverse operator (/) For two matrices x and y x/y is equivalent to x div y x\y is equivalent to y div x

If a*b = c and a is a nonsingular matrix, then a\c is equivalent to b= inv(a)*c a/c is equivalent to b = c*inv(a) Where inv is the MATLAB function to obtain the inverse of a matrix
Copyright Box Hill Institute

March 20, 2012

March 20, 2012

Copyright Box Hill Institute

Syntax
A

= ones(nrows,ncols) A = zeros(nrows,ncols) A = eye(n) A = eye(nrows,ncols) A = diag(v) v = diag(A)


March 20, 2012

Copyright Box Hill Institute

Example

March 20, 2012

Copyright Box Hill Institute

March 20, 2012

Copyright Box Hill Institute

Subscript Notation

If A is a matrix, A(i,j) selects the element in the ith row and jth column. Subscript notation can be used on the right hand side of an expression to refer to a matrix element. >> A = [1 2 3; 4 5 6; 7 8 9]; >> b = A(3,2) b=8 >> c = A(1,1) c=1

March 20, 2012

Copyright Box Hill Institute

Subscript Notation
Subscript notation is also used to assign matrix elements >> A(1,1) = c/b A= 0.2500 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000
March 20, 2012

Copyright Box Hill Institute

Array Operations
Array operations are the element by element arithmetic operation The normal matrix operations(*,\,/,^) preceded by (.) represents an array operation ^ represents raising to the power Addition and Subtraction operations are the same for a matrix and an array

March 20, 2012

Copyright Box Hill Institute

Array Operations

For two matrices with the same dimensions A1=[2 7 6;8 9 10]; and B1=[6 4 3;2 3 4]; C1=A1.*B1 will result in C1 = 12 28 18 16 27 40 For two matrices with the same dimensions R1=[7 3 5]; and S1=[2 3 4]; Q1=R1.^S1 will result in Q1= 49 9 625 Q2=R1.^2 will result in Q2= 49 9 25 Q3=2.^S1 will result in Q3= 4 8 16
Copyright Box Hill Institute

March 20, 2012

Array Operations

For two matrices with the same dimensions A1=[2 7 6;8 9 10]; and B1=[6 4 3;2 3 4]; D1=A1./B1 will result in D1= 0.3333 1.7500 2.0000 4.0000 3.0000 2.5000 E1=A1.\B1 will result in E1= 3.0000 0.5714 0.5000 0.2500 0.3333 0.4000
Copyright Box Hill Institute

March 20, 2012

Complex Numbers
Numbers of the form z=3+4i are called complex numbers In MATLAB the above number is represented as z=3+4*I There are no spaces around the + sign. This is because, if there is a space MATLAB considers it as two different numbers

March 20, 2012

Copyright Box Hill Institute

Complex Numbers
produces the conjugate transpose For a matrix x=1+2*i 2+1*i 3-1*i 4+5*i xt=x will result in xt= 1-2*i 3+1*i 2-1*i 4-5*i . produces the unconjugate transpose xut=x. will result in xt= 1+2*i 3-1*i 2+1*i 4+5*i

March 20, 2012

Copyright Box Hill Institute

Generating Vectors, Iterations

A very important operator in MATLAB is the colon symbol (:) It can be used - to create vectors and matrices - to specify submatrices and vectors - to perform iterations Syntax v_variable = i_val : inc/dec : f_val
v_variable is the vector variable i_val is the initial value f_val is the final value inc/dec is the increment or decrement

March 20, 2012

Copyright Box Hill Institute

Generating Vectors - Examples

v1= 1:8 will generate v1= 1 2 3 4 5 6 7 8 v2= 4:-0.5:1 will generate v2= 4.0 3.5 3.0 2.5 2.0 1.5 1.0 v3= [(5:-1:0);(8:0.2:9)] will generate v3= 5.0 4.0 3.0 2.0 1.0 0.0 8.0 8.2 8.4 8.6 8.8 9.0
Copyright Box Hill Institute

March 20, 2012

Generating Vectors

Vectors can be generated by the MATLAB fucntions linspace and logspace linspace generates linearly evenly spaced vectors logspace generates logarithmically evenly spaced vectors Syntax linspace(i_val,f_val,num) logspace(i_val,f_val,num)
i_val is the initial value f_val is the final value num is the number of elements in the vector

March 20, 2012

Copyright Box Hill Institute

Control Statements for Loops

The for loop allows a statement or a group of statements to repeat a fixed number of times Syntax for index=i_val:inc:f_val statement group y end
i_val is initial value, inc is increment, f_val is final value

eg.

sum = 0; for i = 1 : 100 sum = sum + i^2 end sum


Copyright Box Hill Institute

March 20, 2012

Nested for loops

General form for index1=i_val1 : inc1 : f_val1 for index2=i_val2 : inc2 : f_val2 statement group y end end
i_val1, i_val2 are initial values f_val1,f_val2 are final values inc1, inc2 are respective increments

March 20, 2012

Copyright Box Hill Institute

Control Statements if statements


if statements use logical operations to perform steps Syntax if logical expression1 statement group1 end
the statement group1 will execute if logical expression1 is true(1) else execute the statement following the end statement

eg. if x<-3.5 y=0;


Copyright Box Hill Institute

March 20, 2012

Nested if statements

General form if logical expression1 statement group1 if logical expression2 statement group2 end statement group3 end statement group4
Copyright Box Hill Institute

March 20, 2012

Control Statements Relational Operators


Operator < <= > >= == ~= Function Less than Less than or equal Greater than Greater than or equal Equal Not equal

When a relational operator is used to compare between pairs of corresponding elements, the resulting matrix consists of 1s(true) and 0s(false)
March 20, 2012

Copyright Box Hill Institute

Control Statements if-else statements

if-else statement executes one set of statements if a logical expression is true and a different set of statements if the logical statement is false Syntax if logical expression1 statement group1 else statement group2 end
Copyright Box Hill Institute

March 20, 2012

Control Statements - if-elseif-else statement


With if-elseif-else, group of statements are executed if other logical expressions are false Syntax if logical expression1 statement group1 elseif logical expression2 statement group2 elseif logical expression3 statement group3 else statement group3 end
Copyright Box Hill Institute

March 20, 2012

Control Statements - while loop


while loop allows to execute a group of statements as long as a specified condition is satisfied Syntax while expression1 statement group1 end statement group2

March 20, 2012

Copyright Box Hill Institute

Plotting

Two dimensional plots are created with the plot function Syntax: plot(x,y) plot(xdata,ydata,symbol) plot(x1,y1,x2,y2,...) plot(x1,y1,symbol1,x2,y2,symbol2,...)
Copyright Box Hill Institute

March 20, 2012

March 20, 2012

Copyright Box Hill Institute

March 20, 2012

Copyright Box Hill Institute

March 20, 2012

Copyright Box Hill Institute

Display fprintf
fprintf writes both to the screen (and to a file)
eg: x = 0 : 5 : 10; fprintf( The value of x is %2.2f \n , x)
MATLAB displays The value of x is 0.00 The value of x is 5.00 The value of x is 10.00
March 20, 2012

Copyright Box Hill Institute

Display display
display(x) prints the value of a variable or expression x
eg: x = 0 : 5 : 10; display(x) MATLAB displays 0.00 5.00 10.00

March 20, 2012

Copyright Box Hill Institute

MATLAB Programming
Two different kinds of MATLAB programs -- MATLAB Scripts -- MATLAB Functions Both are basically MATLAB files with .m extension. Also called as m-files

March 20, 2012

Copyright Box Hill Institute

m-files

To create an m-file click on File New select M-File from the menu The MATLAB Editor/Debugger screen is presented. The code is typed / edited here After the code is entered in the Editor/Debugger screen click on File Save As (e.g., firstgraph.m ) Save

Make sure that the file is saved in the directory that is in MATLAB's search path
Copyright Box Hill Institute

March 20, 2012

Script m-files

Script Files
Not really programs No input/output parameters

Script variables are part of workspace


Useful for tasks that never change Useful as a tool for documenting homework

March 20, 2012

Copyright Box Hill Institute

Script files - Example

Enter statements for a script file sineplot.m 1. Choose New. . . from File menu 2. Enter lines listed below x=linspace(0,2*pi); y=sin(x); plot(x,y) 3. Choose Save. . . from File menu Save as sineplot.m 4. Run it >> sineplot
Copyright Box Hill Institute

March 20, 2012

Script files - disadvantages


Create and change variables in the workspace Give no warning that workspace variables have changed Can cause bugs that are hard to track down The algorithm cannot be used for different set of data

March 20, 2012

Copyright Box Hill Institute

Function m-files

Use input and output parameters to communicate with other functions and the command window Use local variables that exist only while the function is executing. These are distinct from variables of the same name in the workspace or in other functions Input parameters allow the same calculation procedure to be applied to different data Functions can call other functions
Copyright Box Hill Institute

March 20, 2012

Syntax

function [outArgs] = funcName(inArgs)


outArgs are enclosed in [ ] outArgs is a comma-separated list of variable names [ ] is optional if there is only one parameter functions with no outArgs are legal inArgs are enclosed in ( ) inArgs is a comma-separated list of variable names functions with no inArgs are legal

March 20, 2012

Copyright Box Hill Institute

Script files - example


twosum.m - two inputs, no output
function twosum(x,y) % twosum Add two matrices -- comment % and print the result -- comment x+y >> twosum(2,2) ans = 4 Please note that % is used to write comments
March 20, 2012

Copyright Box Hill Institute

Script files - example


addmult.m two inputs, two outputs
function [s,p] = addmult(x,y) % Compute sum and product of two matrices s = x+y p = x*y

>> addmult(3,2)
March 20, 2012

Copyright Box Hill Institute

Summary
MATLAB statements are of the form variable = expression All Matlab variables are matrices Matrix and Array operations Control Statements Script files Function m-files

March 20, 2012

Copyright Box Hill Institute

Reference
Gerald W. Recktenwald, Numerical Methods with MATLAB, Implementation and Application, Prentice Hall John O. Attia, Electronics and Circuit Analysis using MATLAB, 2nd ed, Boca Raton, FL, CRC Press 2004 Stephen J. Chapman, Essentials of MATLAB Programming, Thomson Nelson

March 20, 2012

Copyright Box Hill Institute

Você também pode gostar