Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simulation And Modelling

Similar presentations


Presentation on theme: "Simulation And Modelling"— Presentation transcript:

1 Simulation And Modelling
Sabbir Muhammad Saleh Lecturer (00627) University of South Asia Mob Mail-

2 Introduction to MATLAB
Engineering H192 Winter 2005 Introduction to MATLAB Instructor notes: This is the first set of slides for Matlab in the H192 C/C++ programming course. It provides an overview of information that will be helpful in getting started working with Matlab and in the Matlab environment. Lecture 18

3 Engineering H192 Winter 2005 MATLAB MATLAB is a program for doing numerical computation. It was originally designed for solving linear algebra type problems using matrices. It’s name is derived from MATrix LABoratory. MATLAB has since been expanded and now has built-in functions for solving problems requiring data analysis, signal processing, optimization, and several other types of scientific computations. It also contains functions for 2-D and 3-D graphics and animation. Instructor notes: As this slide points out, Matlab started out solving linear algebra problems, systems of linear equations and its name comes from matrix laboratory. It has been expanded into a powerful too that can be used in a wide variety of engineering applications. There are built in “tool kits” for specific applications. It has powerful graphical capabilities that provide for 2D and 3D graphics and even animation. Lecture 18

4 Engineering H192 Winter 2005 MATLAB The MATLAB environment is command oriented somewhat like UNIX. A prompt appears on the screen and a MATLAB statement can be entered. When the <ENTER> key is pressed, the statement is executed, and another prompt appears. If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt. The following slide is the text from a MATLAB screen. Instructor notes: Matlab is part language and part environment. You can enter Matlab commands directly into the command window and Matlab will execute them as soon as the <ENTER> key is pressed. You can also write programs in Matlab which are a collection of Matlab commands and run those programs in the Matlab environment. This is much like writing a program in C or C++ and running it in the UNIX environment or the Visual Studio environment, for example. One of the functions of the semicolon in Matlab, depending on the immediate context, is that of output suppression operator. By placing a semicolon at the end of a line, the output generated by that line will not be displayed in the command window. The line will still be executed, but the results will not be displayed. This is particularly useful when producing large arrays of data that you don’t want displayed in the command window because of their size. Lecture 18

5 Engineering H192 Winter 2005 MATLAB To get started, type one of these commands: helpwin, helpdesk, or demo EDU» a=5; EDU» b=a/2 b = 2.5000 EDU» Instructor notes: This slide shows a quick example of using and not using the semicolon to suppress output to the command window. As you can see the output from a = 5 is suppressed, while the output from b = a/2 is not. It may be a good idea to mention that while suppressing output for a simple situation such as this is not really necessary, when large arrays are used it is very useful and prevents numerous lines of data from scrolling by. In addition it indicates one method of invoking Matlab’s help system. All three commands, helpwin, helpdesk, and demo, take you to essentially the same place in the help system. You can also invoke the helps system by using the mouse and clicking on Help:Matlab Help Lecture 18

6 MATLAB Variable Names Variable names ARE case sensitive
Engineering H192 Winter 2005 MATLAB Variable Names Variable names ARE case sensitive Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer) Variable names must start with a letter followed by letters, digits, and underscores. Instructor notes: Variable names in Matlab are case sensitive. As of Matlab version 6.5 variable names can contain up to 63 characters. The previous limit was 31. Variable names must start with a letter, but after that may be comprised of digits, underscores, and other letters. It is a good idea to make variable names descriptive of what they represent. In addition, it is a good idea not to use, as variable names, any of the default variables for which Matlab has already established values. In Matlab it is possible to use these default names, but the result will be that the default value stored in that variable will no longer be available until Matlab is stopped and restarted. So, if you redefine pi, for example as pi = 10, then the default value of pi would not be available until Matlab was stopped and restarted. It’s a good idea to avoid using variables that have the same names as the special variables, even though you are allowed to do it. It is also a good idea to avoid using variable names that are the same as Matlab built in functions, such as sum(), for example. Again, you are allowed to do it, but if you define a variable sum, then you will lose the built in function sum () until Matlab is stopped and restarted. Lecture 18

7 MATLAB Special Variables
Engineering H192 Winter 2005 MATLAB Special Variables ans Default variable name for results pi Value of  eps Smallest incremental number inf Infinity NaN Not a number e.g. 0/0 i and j i = j = square root of -1 realmin The smallest usable positive real number realmax The largest usable positive real number Instructor notes: Here is a list of Matlab’s special variables. Probably the most common ones that would be inadvertently redefined would be i and j, particularly when used in a for loop. This will most often not be a problem unless you are also working with imaginary numbers. realmin is approximately 2 *10^(-308). realmax is approximately 1.8*10^(308), and eps is approximately 2 *10^(-16). This is the smallest difference that Matlab can distinguish between two numbers. You will get NaN when you do an operation that causes a divide by zero. You will get inf when you decide something very big by something very small. For example, realmax/realmin. Lecture 18

8 MATLAB Math & Assignment Operators
Engineering H192 Winter 2005 MATLAB Math & Assignment Operators Power ^ or .^ a^b or a.^b Multiplication * or .* a*b or a.*b Division / or ./ a/b or a./b or \ or .\ b\a or b.\a NOTE: 56/8 = 8\56 - (unary) + (unary) Addition + a + b Subtraction - a - b Assignment = a = b (assign b to a) Instructor notes: Here we see Matlab’s mathematical and assignment operators. The carat is used for exponentiation, the asterisk for multiplication and the forward slash and backward slash for division. Note that the division operation is different depending on which way the slash “leans”. It’s helpful to remember that it always leans toward the number doing the dividing, or, depending on how you like to think about it, away from the number being divided. We also have the plus sign for addition, the minus sign for subtraction and the equals sign for assignment. It may be helpful to remind students that the equals sign is doing an assignment and not a comparison. There can never be more than one variable to the left of the equals sign. While it’s OK to have this in algebra, it does not work that way in the Matlab environment. You will note that an additional set of operators for exponentiation, multiplication, and division exist all of which are preceded by a decimal point. These are referred to as “dot operators”. They allow Matlab to do element by element computations, rather than following the rules of linear algebra, when the variables involved are vectors and matrices. If one or more of the pair of variables involved in an operation is a scalar, then the dot and normal operators will work the same. However, if both elements of a pair of variables are vectors or matrices of the same size, then the dot operators must be used in order to perform element by element math rather than strict linear algebra. Lecture 18

9 Engineering H192 Winter 2005 Other MATLAB symbols >> prompt continue statement on next line , separate statements and data % start comment which ends at end of line ; (1) suppress output (2) used as a row separator in a matrix : specify range Instructor notes: Two greater than signs are the prompt in Matlab’s command window. If you’re entering a line or equation that is longer than the available space in the window, then an ellipsis (three dots) can be used to continue that line on to the next line. A comma is used to separate individual statements or data. For example, when entering elements in a vector, you can use a comma to separate elements of the vector. The percent sign indicates that everything that follows it until the end of the line is a comment. It is particularly useful when writing programs in Matlab or when you want to include information like name, problem number, seat number, etc. in the command window prior to printing. The semicolon has two functions. At the end of a line, it suppresses output. Very useful when the variables involved are large vectors or matrices. It also acts as a row separator when entering elements of a matrix. The semicolon indicates the end of one row and the beginning of the next. The colon is used for specifying sub-parts of a matrix, for example when wanting to extract two columns from a matrix of several columns More of these two will be seen in later slides. Lecture 18

10 Engineering H192 Winter 2005 MATLAB Matrices MATLAB treats all variables as matrices. For our purposes a matrix can be thought of as an array, in fact, that is how it is stored. Vectors are special forms of matrices and contain only one row OR one column. Scalars are matrices with only one row AND one column Instructor notes: All variables in Matlab are matrices. Vectors are special matrices that have either one row or one column. Scalars are special matrices that have only one row and one column. A variable’s dimension can easily be changed just by redefining its elements or by assigning a variable of different dimensions to it. It’s important to note that a variable’s dimensions can change at any point during a Matlab session as needed. However, they cannot change on the fly within a given equation. For example if the 2x3 matrix m1 were added to the 3x2 matrix m2, Matlab would give an error indicating that the dimensions did not match correctly. Matlab would not automatically alter the dimension of one of the matrices so that the two could be correctly added together. Lecture 18

11 Engineering H192 Winter 2005 MATLAB Matrices A matrix with only one row AND one column is a scalar. A scalar can be created in MATLAB as follows: EDU» a_value=23 a_value = 23 Instructor notes: A scalar is a specialized matrix with one row and one column. One way to create a scalar in Matlab is simply to assign a value to a variable. Lecture 18

12 Engineering H192 Winter 2005 MATLAB Matrices A matrix with only one row is called a row vector. A row vector can be created in MATLAB as follows (note the commas): EDU» rowvec = [12 , 14 , 63] rowvec = Instructor notes: A matrix with a single row is called a row vector. Row vectors in Matlab can be created by assigning a variable to a list of individual elements. The elements must be contained within square brackets and can be separated by commas or spaces or both. Matlab is not overly picky about the separators between elements of a given row vector or row within a matrix. To reemphasize the semicolon as output suppression operator, you might want to point out that putting a semicolon at the end of the line assigning elements to rowvec would have prevented its contents from being displayed. Lecture 18

13 Engineering H192 Winter 2005 MATLAB Matrices A matrix with only one column is called a column vector. A column vector can be created in MATLAB as follows (note the semicolons): EDU» colvec = [13 ; 45 ; -2] colvec = 13 45 -2 Instructor notes: A matrix with a single column is called a column vector. Column vectors in Matlab can be created by assigning a variable to a list of individual elements. The elements must be contained within square brackets and can be separated by semicolons or <ENTER>s or both. Matlab is not overly picky about the separators between elements of a given column vector or column within a matrix. Lecture 18

14 Engineering H192 Winter 2005 MATLAB Matrices A matrix can be created in MATLAB as follows (note the commas AND semicolons): EDU» matrix = [1 , 2 , 3 ; 4 , 5 ,6 ; 7 , 8 , 9] matrix = Instructor notes: Creating a matrix in Matlab is essentially a combination of creating row vectors and column vectors. Inside of square brackets the elements of a given row are separated by commas or spaces and the rows are separated from each other by semicolons or <ENTER>s. Thus, in this example we see rows with three elements each separated by semicolons. It is important to note that each row must have the same number of elements, otherwise Matlab will give an error message. Lecture 18

15 Extracting a Sub-Matrix
Engineering H192 Winter 2005 Extracting a Sub-Matrix A portion of a matrix can be extracted and stored in a smaller matrix by specifying the names of both matrices and the rows and columns to extract. The syntax is: sub_matrix = matrix ( r1 : r2 , c1 : c2 ) ; where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the beginning and ending columns to be extracted to make the new matrix. Instructor notes: Now we get to see the colon, or array addressing operator, in action. This operator allows us to extract a sub-matrix from another matrix. You can imagine this being particularly useful when you have a matrix that contains a column of sampling times and multiple columns of data. You can easily extract the time column and a data column for further analysis or graphing. A submatrix is extracted by using the original matrixe’s name followed, in parentheses, by the beginning row you want extracted separated by a colon from the ending row you wanted extracted. Next comes a comma to separate the rows from the columns. And then, the beginning column you want extracted separated by a colon from the ending row you want extracted. If r1 equals r2 then only one row will be extracted. Likewise, if c1 equals c2, then only one column will be extracted. So, if r1 equals r2 AND c1 equals c2, the result will be a scalar. There are some shortcut notations. If you include the colon, but no r1 and r2 or c1 and c2, then you will get all of the elements of the row or column, respectively. If matrix were followed by two colons separated by a comma within parentheses, then in this example sub_matrix would be identical to matrix. If you specify just a single row or single column with no colon to specify a range of rows or columns, then you will get only that specific row and/or column. If you specify a row or column that is outside of the allowable range of rows or columns, Matlab will give you an error message. Lecture 18

16 Engineering H192 Winter 2005 MATLAB Matrices A column vector can be extracted from a matrix. As an example we create a matrix below: EDU» matrix=[1,2,3;4,5,6;7,8,9] matrix = Here we extract column 2 of the matrix and make a column vector: EDU» col_two=matrix( : , 2) col_two = 2 5 8 Instructor notes: Now some extraction examples. First the matrix is set up. It might be good to reemphasize that those commas could just as well be spaces and the semicolons could be replaced by <ENTER>s. The result would be the same. In the second panel the second column is extracted. The colon with no rows indicated means all rows will be extracted, and the 2 with no colon and ending row indicates that only the second column is extracted. This could also have been written col_two = matrix(1:3, 2:2) or matrix(:, 2:2). It might be good to point this out so that students are comfortable with the strict way of writing it before trying to use the shortcut method. Lecture 18

17 Engineering H192 Winter 2005 MATLAB Matrices A row vector can be extracted from a matrix. As an example we create a matrix below: EDU» matrix=[1,2,3;4,5,6;7,8,9] matrix = Here we extract row 2 of the matrix and make a row vector. Note that the 2:2 specifies the second row and the 1:3 specifies which columns of the row. EDU» rowvec=matrix(2 : 2 , 1 : 3) rowvec = Instructor notes: The row vector example is much like the column vector example. As in the previous slide the matrix is first initialized, then a row vector is extracted from it. When the extraction takes place, you’ll see that it indicates from row 2 to row 2 using the colon. It could also have been written as matrix (2, 1:3) and the result would be the same. Lecture 18

18 Thank You


Download ppt "Simulation And Modelling"

Similar presentations


Ads by Google