1 / 21

Reading Assignment: MATLAB handout MATLAB Assignment: MATLAB Assignment #2

MATLAB Lecture #2 EGR 110 – Engineering Graphics. Reading Assignment: MATLAB handout MATLAB Assignment: MATLAB Assignment #2. MATLAB Script Files (.m files) We have seen how to execute MATLAB commands in the Command Window and how to re-execute commands using the Command History Window.

iria
Télécharger la présentation

Reading Assignment: MATLAB handout MATLAB Assignment: MATLAB Assignment #2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MATLAB Lecture #2 EGR 110 – Engineering Graphics Reading Assignment:MATLAB handout MATLAB Assignment:MATLAB Assignment #2 MATLAB Script Files (.m files) We have seen how to execute MATLAB commands in the Command Window and how to re-execute commands using the Command History Window. We will now see how to write a program in MATLAB. The program (called a script file or .m file) is a file containing a series of statements much like those used previously in the Command Window. A program has the advantage of being reusable. You can easily try a sequence of commands and then modify them if the output isn’t correct. The program can be executed by typing its name in the Command Window, thus executing the sequence of statements in the program. The Current Directory When the name of a program is entered into the Command Window, MATLAB looks for the program in the Current Directory and executes it if it is located there. Change current folder here MATLAB files in current folder The Current Folder is shown here. Run program example.m in the current folder

  2. MATLAB Lecture #2 EGR 110 – Engineering Graphics Changing the Current Folder It is recommended that you change the current folder to the folder on your personal drive where you will save your files. Current Folder has been changed Change drive and folders Add a New Folder

  3. MATLAB Lecture #2 EGR 110 – Engineering Graphics Creating a script (.m) file MATLAB editor Type edit in the Command Window to run the MATLAB editor • Notes: • Type edit to open the editor • Type edit MyFile.mto create a new file named MyFile.m (if it does not exits) • Type edit MyFile.mto open an existing file named MyFile.m

  4. MATLAB Lecture #2 EGR 110 – Engineering Graphics Enter the sample program below into the MATLAB editor. Save the file as Example1 (do not use spaces). The file will automatically be given a .m extension. Note that comments begin with %.

  5. MATLAB Lecture #2 EGR 110 – Engineering Graphics Running the program (script file) Run a program by typing its name in the Command Window Program output

  6. MATLAB Lecture #2 EGR 110 – Engineering Graphics Specifying input values from the command line Try modifying the previous program to remove the statement rad = 4; (or change it to a comment). Save the program as Example 1b. Try out the program by entering a value for rad and then running the program. Value of rad specified Program executed Program output So running a script file is just like executing a sequence of commands in the Command Window. You can execute any sequence of commands and script files.

  7. MATLAB Lecture #2 EGR 110 – Engineering Graphics Using MATLAB’s INPUT function The following MATLAB function is useful for prompting the user to enter inputs: input (‘message’) – returns a value entered from the keyboard. Example: Height = input(‘Enter the height of the triangle’); Example: Write a MATLAB program to calculate the area of a right triangle after prompting the user to enter values for the base and height of the triangle (in feet). Display the input values and the output (with units). Solution – next page

  8. MATLAB Lecture #2 EGR 110 – Engineering Graphics

  9. Col 1 Col 2 Col 3 Row 1 Row 2 MATLAB Lecture #2 EGR 110 – Engineering Graphics Matrix Operations MATLAB is designed around using matrices for calculations. Even when an expression like x = 2.5 is used in MATLAB, the scalar variable x is stored as a (1 x 1) matrix. It is often important to understand how matrix operations work in order to understand calculations in MATLAB. Matrix Operations - Review A matrix (or an array) is a collection of elements arranged in rows and columns. A matrix in general has m rows and r columns. The dimension of a matrix is the number of rows by the number of columns (m x r). Matrix A above has dimension (2 x 3).

  10. MATLAB Lecture #2 EGR 110 – Engineering Graphics Several examples of matrices (and their dimensions) are shown below. A matrix with only one column or only one row is often called a vector. Matrix C below is a column vector and matrix D below is a row vector. Matrix A has dimension (2 x 3) Matrix B has dimension (3 x 2) Matrix C has dimension (3 x 1) Matrix D has dimension (1 x 3) Specifying a matrix in MATLAB Matrices are specified in MATLAB as follows: • use brackets [ ] around the elements of the matrix • commas or spaces are used to separate the elements in a row • a semicolon (;) is used separate each row Matrices A, B, C, and D would be specified as follows in MATLAB: A = [1 0 –7;4 15 2] or A = [1,0,–7;4,15,2] B = [1 3;7 4;6 –5] C = [2;4;6] D = [11 9 –7]

  11. MATLAB Lecture #2 EGR 110 – Engineering Graphics Elements in a matrix The elements in a matrix can be referred to by row and column number. Specifying elements in a matrix in MATLAB A specific element in a matrix in MATLAB can be referred to as follows: MatrixName(RowNumber, RolumnNumber) For example, referring to matrix A defined above: A(1,2) = 0, A(2,1) = 4, and A(2,3) = 2 See the MATLAB example on the following page

  12. MATLAB Lecture #2 EGR 110 – Engineering Graphics

  13. MATLAB Lecture #2 EGR 110 – Engineering Graphics Matrix Addition and Subtraction Matrix addition and subtraction are only defined for matrices with the same dimensions. If A and B are (m x r) matrices, then C = A + B implies that C(i,j) = A(i,j) + B(i,j) and D = A - B implies that D(i,j) = A(i,j) - B(i,j)

  14. these must be equal n n r m r m = * Matrix A (m x r) Matrix C (m x n) Matrix B (r x n) MATLAB Lecture #2 EGR 110 – Engineering Graphics Matrix Multiplication The matrix multiplication A*B is only defined if matrix A has dimension (m x r) and matrix B has dimension (r x n). The result will be a matrix with dimension (m x n). So the column dimension of the first matrix must equal the row dimension of the second matrix.

  15. MATLAB Lecture #2 EGR 110 – Engineering Graphics Exercise – Determine the dimensions of the resulting matrix for each case shown. Enter NA (not applicable) if the matrix multiplication is not allowed. How does matrix multiplication work? Verification using MATLAB:

  16. MATLAB Lecture #2 EGR 110 – Engineering Graphics Transpose of a Matrix The transpose of a matrix is found by swapping its rows and columns. If a matrix has dimension (m x n) then its transpose has dimension (n x m). AT = transpose of matrix A and if B = AT, then B(i,j) = A(j,i)

  17. MATLAB Function or Operation Description MATLAB Example Using matrices A and B +, - Matrix addition, subtraction (matrices A and B must have the same dimensions) C = A + B * Matrix multiplication (only allowed if A has dimensions (m x r) and B has dimensions (r x n). Result in an (m x n) matrix. C = A*B (matrix name)‘ Transpose of the matrix C = A’ inv(matrix name) Inverse of the matrix (only for a square matrix) C = inv(A) det(matrix name) Determinant of the matrix (only for a square matrix) C = det(A) MATLAB Lecture #2 EGR 110 – Engineering Graphics Matrix Functions & Operations in MATLAB The following functions are operations are useful for working with matrices in MATLAB.

  18. MATLAB Lecture #2 EGR 110 – Engineering Graphics Sample MATLAB program demonstrating matrix operations (The program output is shown on the next page.)

  19. MATLAB Lecture #2 EGR 110 – Engineering Graphics Output of the program on the previous page.

  20. MATLAB Lecture #2 EGR 110 – Engineering Graphics

  21. MATLAB Lecture #2 EGR 110 – Engineering Graphics

More Related