1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept

Embed Size (px)

DESCRIPTION

Assignment Write a function called odd_index that takes a matrix, M, as input argument and returns a matrix that contains only those elements of M that are in odd rows and columns. In other words, it would return the elements of M at indices (1,1), (1,3), (1,5), …, (3,1), (3,3), (3,5), …, etc. Note that both the row and the column of an element must be odd to be included in the output. The following would not be returned: (1,2), (2,1), (2,2) because either the row or the column or both are even. As an example, if M were a 5- by-8 matrix, then the output must be 3-by-4 because the function omits rows 2 and 4 of M and it also omits columns 2, 4, 6, and 8 of M. 3

Citation preview

1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept. Problem: Guessing Birth Date 2 The program can guess your birth date. Run to see how it works. GuessBirthDate Assignment Write a function called odd_index that takes a matrix, M, as input argument and returns a matrix that contains only those elements of M that are in odd rows and columns. In other words, it would return the elements of M at indices (1,1), (1,3), (1,5), , (3,1), (3,3), (3,5), , etc. Note that both the row and the column of an element must be odd to be included in the output. The following would not be returned: (1,2), (2,1), (2,2) because either the row or the column or both are even. As an example, if M were a 5- by-8 matrix, then the output must be 3-by-4 because the function omits rows 2 and 4 of M and it also omits columns 2, 4, 6, and 8 of M. 3 In the previous lectures 4 Matlab Windows (workspace, m-file,) Working with Arrays One-Dimensional Array Two-Dimensional Array Elements of Arrays Built-in Functions for Arrays Agenda 5 Mathematical Operations with Arrays Addition and Subtraction. Multiplication. Division: solving systems of equations Built-in functions to analyze arrays Element by element operations Generating random values. 6 Matlab has two ways: 1)following linear algebra rules: - Uses the standard symbols: ( *,/,^ ) 2)Element-by-element operations: -Uses the symbols with a period: (. *,./,.^ ) - These two ways are independent and have totally different applications. Operations with Arrays 7 The operations + and are applied where: Arrays have identical size. A scalar value is added/subtracted to/from an array. Addition and Subtraction 8 9 10 If A and B are two matrices, the operation A*B can be carried out only if the number of columns in matrix A is equal to the number of rows in matrix B. Multiplication 11 Multiplication 12 Multiplication 13 Multiplication 14 We will not care about the linear algebra rules of arrays division. We just study the application of such operation in solving a system of equations. For example: a system of three equations Division 15 Equation solving (using left division): We can also use the inverse: - To calculate the inverse: Division or 16 Array inverse 17 Array inverse 18 Systems of three equations Solution: First: write the equations in matrix notation: 19 Systems of three equations Solution: Second: write Matlab commands: 20 Built-in Function to Analyze Arrays 21 Built-in Function to Analyze Arrays 22 Built-in Function to Analyze Arrays 23 ELEMENT-BY-ELEMENT OPERATIONS Useful when we need to perform some mathematical operation (function) over many quantities. 24 ELEMENT-BY-ELEMENT OPERATIONS 25 Application: verify a function at different values. ELEMENT-BY-ELEMENT OPERATIONS 26 This is useful in the simulation of many physical processes and engineering applications. GENERATION OF RANDOM NUMBERS The rand command: generates uniformly distributed random numbers with values between 0 and 1. 27 Generating random values in a specific range (a,b): GENERATION OF RANDOM NUMBERS 28 GENERATION OF RANDOM NUMBERS Generating random integers : The randi command: generates uniformly distributed random integers 29 GENERATION OF RANDOM NUMBERS Generating random integers : 30 GENERATION OF RANDOM NUMBERS Generating normally distributed random values : 31 GENERATION OF RANDOM NUMBERS Generating normally distributed random values : controlling the standard deviation and mean by multiplying the number generated by the randn function by the desired standard deviation, and adding the desired mean. 32 Three forces are applied to a bracket as shown. Determine the total (equivalent) force applied to the bracket. (A force is a vector (a physical quantity that has a magnitude and direction) Example 33 Example solution In order to use Matlab we have first to state all necessary equations and values !. where F is the magnitude of the force and is its angle relative to the x axis, F x and F y are the components of F in the directions of the x and y axes, i and j are unit vectors. If F x and F y are known, then F and can be determined by: The total (equivalent) force applied on the bracket is obtained by adding the forces that are acting on the bracket. 34 Example solution m-file: 35 Example solution Result: (in Command Window) Material & problems 36 We have covered Chapter 3 from Gilat. Problems you can try: Sample Problem 3-5 (page 85), 1-- 4, 12,13,23,27,31,32