Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to MATLAB Lecture 02

Similar presentations


Presentation on theme: "Introduction to MATLAB Lecture 02"— Presentation transcript:

1 Introduction to MATLAB Lecture 02
Olawale B. Akinwale Department of Electronic and electrical Engineering Obafemi Awolowo university, Ile-Ife

2 MATRIX ALGEBRA 11/26/2018

3 Elementary Matrix Operations
Defining matrices and vectors (arrays) >> A = [ 3 5 2; 3,-2,1] % a comma or space can be used >> A = [ 3 5 2; ] >> b = [ ] >> b = 0:1:3 >> b = 0:3 >> B = [-2:1 ; 3 2i -29 sqrt(15)] The number of spaces between -2 and 1 here is irrelevant Note that the spacing here matters. There must be no space between the minus sign and the number 29 11/26/2018

4 Elementary Matrix Operations
Adding, subtracting, multiplying, dividing and exponentiation are just like are done with variables ( A + B, C – B, B * A, A / B and A^3). You just need to take not of the order of the matrices. If instead of doing a matrix operation, you want to do element-wise operations e.g. squaring each element of a matrix instead of multiplying the whole matrix by itself, you simply put a dot in front of the operator in question. 11/26/2018

5 Examples 11/26/2018

6 Elementary Matrix Operations
You can access the individual elements of a matrix or vector by using the appropriate subscripts in parenthesis separated by a comma. If A = [3 2; ] then A(2,1) = the element on the second row and first column which is 1. A(1,2) is 2 and A(2,2) is 20 You can select a range of elements of a matrix by using the colon operator e.g. D(1:3, 2:4) will select the elements in the first three rows and the second to fourth column of the matrix D. We could also use E(:, 5:end) to select the elements in all the rows and the fifth to the last column of E. 11/26/2018

7 Elementary Matrix Operations (cont’d)
>> sum(A) % sums up all elements of each column of A. If A is a vector, it sums up all its elements. >> prod(A) % multiplies up all elements of each column of A. If A is a vector, it multiplies all its elements >> inv(A) % finds the inverse of A >> det(A) % finds the determinant of A >> diag(A) % selects the diagonal elements of A >> diag([1 3 -2]) % creates a diagonal matrix with elements 1, 3 and -2 11/26/2018

8 Elementary Matrix Operations (cont’d)
Matrices can be used to solve equations of the form Ax = b simply by finding the inverse of A and pre- multiplying b with it i.e. x = inv(A) * b. Optionally, in MATLAB, we can issue the command x = A\b and it does the same job. For example, given 11/26/2018

9 See you in the next class
11/26/2018


Download ppt "Introduction to MATLAB Lecture 02"

Similar presentations


Ads by Google