Matrices 4: use of MATLAB - University of...

Post on 23-Mar-2020

9 views 0 download

transcript

Matrices 4: use of MATLAB

Anthony Rossiter http://controleducation.group.shef.ac.uk/indexwebbook.html

http://www.shef.ac.uk/acse

Department of Automatic Control and Systems Engineering

Introduction

• The previous videos introduced definitions of matrices, basic notation and special cases such as square matrices, symmetry and vectors and concepts of addition and subtraction.

• This video shows how MATLAB can be used to define and store matrices, to extract specified components and for addition/subtraction.

Matlab environment

The main thing to note is that the default variable in MATLAB is a matrix (or vector if the row or column dimension is one).

Any BODMAS type operation that is valid with matrices can be carried out with the same syntax.

MATLAB also includes a large number of matrix analysis tools that you will find useful.

This video introduces the basics.

Defining a matrix Matrices are defined by their rows and columns.

Data entry into MATLAB can be managed by defining the rows or columns.

MATLAB SYNTAX: Columns are separated by commas (or spaces) and rows are separated by semi-colons.

I advise against using spaces in general as these are not always obvious and lead to

silly typos.

MATLAB SYNTAX example

Entry of a 2 by 3 matrix.

Define 1st row separating elements by commas.

Then semi-colon, followed by 2nd row separated by commas.

MATLAB SYNTAX example 2

Entry of a 3 by 6 matrix.

Define each row in turn separating elements by commas.

Use a semi-colon between rows.

Interim summary

Data entry in this fashion is somewhat clumsy and tedious.

• It may be easier to allocate values directly, especially where a matrix is sparse.

• Some common matrices are predefined in MATLAB and hence shortcuts are provided.

Create a matrix with following information

2nd row and 3rd column is 5

4th row and 2nd column is 2

1st row and 5th column is 6

Remainder are 0.

Note MATLAB syntax matches that used in mathematics, i.e. row index and then column

index.

Modifying matrix elements

When a matrix already exists, you can edit a coefficient directly by accessing its position.

Modifying matrix dimensions

When a matrix already exists, you can change its dimension by specifying a new coefficient.

Unknown coefficients are automatically filled with zeros.

Extracting coefficients Individual coefficients can be extracted using equivalent notation.

Specify the row and column of the element desired.

MATLAB provides shortcuts so that you can quickly extract whole rows or columns.

Extracting whole row/column

Matrix Transposition

In MATLAB transposition is defined using two alternatives, here an apostrophe or the file transpose.m

WARNING: THE use of the apostrophe only gives transpose for real matrices as in fact it gives complex conjugate transpose. A common coding errors is to forget this subtlety.

Examples of transposition

Example of NOT transposition

Complex conjugate transpose!

Special matrices

MATLAB has shortcuts for:

1. identify matrices.

2. matrices of zeros.

3. matrices of ones.

4. diagonal matrices.

Also has shortcuts to produce random matrices and other specialised forms not discussed here.

Identity matrix - must define required dimension

Matrix of ones – must define required dimensions

Matrix of zeros – must define required dimensions

Diagonal matrix – supply diagonal elements

Addition and subtraction of matrices Add (or subtract) components in same position,

that is with the same {row, column} index.

Matrices must be same dimensions or you cannot add them!

24

53;

67

21;

43

32BABA

jijiji BACBAC ,,,

MATLAB handles matrix addition/subtraction automatically using normal + and - notation

Addition example 1

Addition example 2

Addition example 3

Using matrices to store data

A common use of a matrix in MATLAB is to store tabular data.

One in a matrix, it is easy to use MATLAB commands to plot this data as required.

0 20 40 60 80 100 1200

5

10

15

20

25

30

Time (sec)

Temperature

Voltage

MATLAB example

JUMP TO MATLAB FOR QUICK LIVE DEMONSTRATION

File is called matrixentry.m

Summary

• Demonstrated the core matrix capability of MATLAB.

• Variables in MATLAB are automatically interpreted as matrices.

• Normal matrix algebra rules apply.

• Coefficients are defined and accessed using the index of (row,column)