Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.

Similar presentations


Presentation on theme: "Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication."— Presentation transcript:

1

2 Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication Number Display (Scientific Notation) Script M-Files

3 Types of M-files Two types Script Function

4 Script M-file The script can use any variables that have been defined in the workspace, and any variables created in the script are added to the workspace when the script executes A script created in the MATLAB edit window can be executed by selecting the Save and Run icon from the menu bar A script can be executed by typing a file name or by using the run command from the command window No matter how you do it, you can only run an M-file if it is in the current folder

5 Editor/Debugger Window

6

7 Cell Mode It is a utility that allows the user to divide M-files into sections, or cells, that can be executed one at a time This feature is particularly useful as develop MATLAB ® programs are developed To activate the cell mode, select Cell -> Enable Cell Mode from the menu bar in the edit window Once the cell mode has been enabled, the cell toolbar appears To divide M-file program into cells, create cell dividers by using a double percentage sign followed by a space To name the cell, just add a name on the same line as the cell divider: % Cell Name

8

9 Continued…. It’s important to include the space after the double percentage sign (%). If don’t, the line is recognized as a comment, not a cell divider Once the cell dividers are in place, if cursor is positioned anywhere inside the cell, the entire cell turns pale yellow Now use the evaluation icons on the cell toolbar to evaluate a single section, evaluate the current section and move on to the next section, or evaluate the entire file Also on the cell toolbar is an icon that lists all the cell titles in the M-file

10

11 Continued…. By dividing the program into cells, it was possible to work on each problem separately Be sure to save any M-files developed this way by selecting Save or Save As from the file menu: File -> Save or File -> Save As The reason for using these commands is that in cell mode, the program is not automatically saved every time when it is run

12 Summary of Chapter

13 Chapter 3 Built in MATLAB Function

14 Using Built-in Functions Many of the names for MATLAB’s built-in functions are the same as those defined not only in the C programming language, but in Fortran and Java as well For example: to take the square root of the variable x, we type b = sqrt(x) A big advantage of MATLAB is that function arguments can generally be either scalars or matrices In our example, if x is a scalar, a scalar result is returned Thus, the statement x = 9; b = sqrt(x) returns a scalar: b = 3

15 Continued…. However, the square-root function, sqrt, can also accept matrices as input In this case, the square root of each element is calculated, so x = [4, 9, 16]; b = sqrt(x) returns b = 2 3 4 All functions can be thought of as having three components: a name, input, and output In the preceding example, the name of the function is sqrt, the required input goes inside the parentheses and can be a scalar or a matrix, and the output is a calculated value or values In this example, the output was assigned the variable name b.

16 Continued…. Some functions require multiple inputs For example: the remainder function, rem, requires two inputs: a dividend and a divisor. We represent this as rem(x,y), so rem(10,3) calculates the remainder of 10 divided by 3: ans = 1 The size function is an example of a function that returns two outputs, which are stored in a single array. It determines the number of rows and columns in a matrix. Thus, d = [1, 2, 3; 4, 5, 6]; f = size(d) returns the 1 X 2 result matrix f = 2 3 You can also assign variable names to each of the answers by representing the left-hand side of the assignment statement as a matrix For example: [rows,cols] = size(d) gives rows =2 cols =3

17 Continued…. A useful feature of the more recent versions of MATLAB is the adaptive help capability As a function name is typed, a screen tip appears showing the correct function format It also includes a link to the function’s help page. A more complicated expressions can be created by nesting functions For instance: g = sqrt(sin(x)) finds the square root of the sine of whatever values are stored in the matrix named x If x is assigned a value of 2, x = 2; the result is g =0.9536 Nesting functions can result in some complicated MATLAB code

18 Using the HELP Feature MATLAB includes extensive help tools, which are especially useful in understanding how to use functions There are two ways to get help from within MATLAB A command-line help function An HTML-based set of documentation available by selecting Help from the menu bar, selecting the help icon or by using the F1 function key There is also an online help set of documentation, available through the Start button or the Help icon on the menu bar However, the online help usually just reflects the HTML-based documentation Both help options should be used, since they provide different information and insights into how to use a specific function

19 Continued…. To use the command-line help function, type help in the command window help A list of help topics will appear: HELP topics: MATLAB\general – General-purpose commands MATLAB\ops – Operators and special characters MATLAB\lang – Programming language constructs MATLAB\elmat – Elementary matrices and matrix manipulation MATLAB\elfun – Elementary math functions MATLAB\specfun – Specialized math functions and so on To get help on a particular topic, type help

20 Example to get help on the tangent function, type help tan The following should be displayed: TAN Tangent of argument in radians. TAN(X) is the tangent of the elements of X See also atan, tand, atan2

21 Window HELP Screen To use the windowed help screen, select Help -> Product Help from the menu bar You can the navigate to the appropriate topic To access this version of the help utility directly from the command window, type doc Thus, to access the windowed help for tangent, type doc tan

22 MATLAB Help Window

23 Elementary Math Functions Elementary math functions include Logarithms Exponentials absolute value rounding functions, functions used in discrete mathematics

24 Common Computations

25 Rounding Functions

26 Example

27 Discrete Mathematics

28 rats Function MATLAB includes the rats function, which expresses a floating-point number as a rational number— that is, a fraction Discrete mathematics is the mathematics of whole numbers This is a additional function in MATLAB Factoring, calculating common denominators, and finding least common multiples are procedures usually covered in intermediate algebra courses

29 factorial Function A factorial is the product of all the positive integers from 1 to a given value Thus 3 factorial (indicated as 3!) is 3x2x1=6 Many problems involving probability can be solved with factorials For example: the number of ways that five cards can be arranged is 5x4x3x2 x1=5!=120. When you select the first card, you have five choices; when you select the second card, you have only four choices remaining, then three, two, and one. This approach is called combinatorial mathematics, or combinatorics To calculate a factorial in MATLAB use the factorial function Thus factorial(5) ans =120 gives the same result as 5*4*3*2*1 ans =120

30 Continued…. The value of a factorial quickly becomes very large Ten factorial is 3,628,800 MATLAB can handle up to 170! Anything larger gives Inf for an answer, because the maximum value for a real number is exceeded factorial(170) ans =7.2574e+306 factorial(171) ans =Inf Factorials are used to calculate the number of permutations and combinations of possible outcomes A permutation is the number of subgroups that can be formed when sampling from a larger group, when the order matters

31 Trigonometric Functions

32 Continued….

33

34 Data Analysis Function Analyzing data statistically in MATLAB is particularly easy, because Whole data sets can be represented by a single matrix The large number of built-in data analysis functions

35 Maximum and Minimum

36

37

38 Mean and Median There are several ways to find the “average” value in a data set In statistics, the mean of a group of values is call the average The mean is the sum of all the values, divided by the total number of values Another kind of average is the median, or the middle value There are an equal number of values both larger and smaller than the median The mode is the value that appears most often in a data set MATLAB provides functions for finding the mean, median, and the mode All of these functions are column dominant and will return an answer for each column in a two-dimensional matrix

39

40 Sums and Products


Download ppt "Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication."

Similar presentations


Ads by Google