+ All Categories
Home > Documents > Matrices 4: use of MATLAB - University of...

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

Date post: 23-Mar-2020
Category:
Upload: others
View: 9 times
Download: 0 times
Share this document with a friend
28
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
Transcript
Page 1: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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

Page 2: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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.

Page 3: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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.

Page 4: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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.

Page 5: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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.

Page 6: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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.

Page 7: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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.

Page 8: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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.

Page 9: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

Modifying matrix elements

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

Page 10: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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.

Page 11: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

Extracting coefficients Individual coefficients can be extracted using equivalent notation.

Specify the row and column of the element desired.

Page 12: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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

Extracting whole row/column

Page 13: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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.

Page 14: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

Examples of transposition

Page 15: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

Example of NOT transposition

Complex conjugate transpose!

Page 16: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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.

Page 17: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

Identity matrix - must define required dimension

Page 18: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

Matrix of ones – must define required dimensions

Page 19: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

Matrix of zeros – must define required dimensions

Page 20: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

Diagonal matrix – supply diagonal elements

Page 21: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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

Page 22: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

Addition example 1

Page 23: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

Addition example 2

Page 24: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

Addition example 3

Page 25: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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.

Page 26: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

0 20 40 60 80 100 1200

5

10

15

20

25

30

Time (sec)

Temperature

Voltage

MATLAB example

Page 27: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

JUMP TO MATLAB FOR QUICK LIVE DEMONSTRATION

File is called matrixentry.m

Page 28: Matrices 4: use of MATLAB - University of Sheffieldcontroleducation.group.shef.ac.uk/maths/matrices4_use of MATLAB.pdf · Matlab environment The main thing to note is that the default

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)


Recommended