matlab basics 3 vectors or arrays - university of...

14
MATLAB Basics 3 vectors or arrays Anthony Rossiter University of Sheffield 1 Slides by Anthony Rossiter For a neat organisation of all videos and resources http://controleducation.group.shef.ac.uk/indexwebbook.html

Upload: others

Post on 19-Apr-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

MATLAB Basics 3 vectors or arrays

Anthony Rossiter

University of Sheffield

1

Slides by Anthony Rossiter

For a neat organisation of all videos and resources

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

Page 2: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

Introduction

1. The previous video demonstrates how to use basic MATLAB functionality.

2. For more advanced problem solving there is often a requirement to repeat the same computation on a number of different variables.

3. For example, how would you form a plot of y=f(x) for -1<x<4? You would calculate f(x) at a number of different values for x.

4. MATLAB supports a vector/array notation to make handling these computations much easier.

Slides by Anthony Rossiter

2

Page 3: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

Defining an ROW array

MATLAB uses square brackets and commas to store a set of numbers in a single variable.

Slides by Anthony Rossiter

3

v is a 1 by 6 array because it stores 6

values in a row vector form.

Page 4: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

Defining a COLUMN array

MATLAB uses square brackets and semi-colons to store a set of numbers in a column form.

Slides by Anthony Rossiter

4

v is a 6 by 1 array because it stores 6 values in a column

vector form.

Page 5: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

Form a sketch of a simple function

MATLAB function commands such as sin, cos, log etc support data entry in array form to make computations much more efficient.

Slides by Anthony Rossiter

5

Page 6: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

Form a sketch of a simple function

MATLAB function plot will give a plot of 2 arrays assuming both arrays are the same length.

Slides by Anthony Rossiter

6

Plots elements [v(1),y(1)], [v(2),y(2)],…,[v(6),y(6)]

Page 7: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

Longer arrays

Slides by Anthony Rossiter

7

• In practice, we may want 50-100 values to form a plot and this is tedious to enter by hand.

• MATLAB provides a convenient mechanism to automate the generation of suitable argument (or domain) values. Lower x

value

Upper x value

Number of values

Page 8: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

Sketch with lots of values

Slides by Anthony Rossiter

8

Illustration with limits -1 and 8 and 1000 values in total.

Note how quick and easy this is to write.

Lower x value

Upper x value

1000 values

Page 9: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

Accessing vector array values

MATLAB uses a common sense notation to access values. The value in the ith position is accessed with the notation x(i).

Slides by Anthony Rossiter

9

5 values

Value in 2nd position accessed by x(2)

Value in 5th position accessed by x(5)

Page 10: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

Accessing vector array values 2

MATLAB has a short cut to find the last value in an array which is useful when you do not know the number of terms in the array.

Slides by Anthony Rossiter

10

Value in last position accessed by x(end)

Value in last position accessed

by y(end)

Page 11: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

Switching off display to command window

Where a vector has a large number of terms, better not to display to screen as this will flood the screen.

Here f=0:100

To switch off display, use a semi-colon at the end of the command.

Slides by Anthony Rossiter

11

NO DISPLAY!

Page 12: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

LIVE DEMONSTRATIONS WITH MATLAB

Slides by Anthony Rossiter

12

Open the following and try the instructions

matlab_basics3.m

Page 13: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

Conclusions

Demonstrated the vector/array notation in MATLAB.

1. Allows efficiency of computation and data storage.

2. The same ‘function’ can be executed on many different values using a single operation.

3. Required for the plotting function.

4. Easy to extract specific values as required.

Users recommended to finish commands with a semi-colon to prevent flooding the command window with the data output.

Slides by Anthony Rossiter

13

Page 14: MATLAB Basics 3 vectors or arrays - University of Sheffieldcontroleducation.group.shef.ac.uk/MATLAB/MATLAB basics 3 - vecto… · MATLAB Basics 3 vectors or arrays Anthony Rossiter

© 2016 University of Sheffield This work is licensed under the Creative Commons Attribution 2.0 UK: England & Wales Licence. To view a copy of this licence, visit http://creativecommons.org/licenses/by/2.0/uk/ or send a letter to: Creative Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA. It should be noted that some of the materials contained within this resource are subject to third party rights and any copyright notices must remain with these materials in the event of reuse or repurposing. If there are third party images within the resource please do not remove or alter any of the copyright notices or website details shown below the image. (Please list details of the third party rights contained within this work. If you include your institutions logo on the cover please include reference to the fact that it is a trade mark and all copyright in that image is reserved.)

Anthony Rossiter Department of Automatic Control and

Systems Engineering University of Sheffield www.shef.ac.uk/acse

For a neat organisation of all videos and resources

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