matlab basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/matlab/matlab... · 4....

14
MATLAB Basics 2 simple calculations and use 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 24-Jul-2020

7 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

MATLAB Basics 2 simple calculations and use

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 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

Introduction 1. Demonstrates how to use basic MATLAB

functionality.

2. An understanding of this is a precursor to the more advanced uses needed for general engineering problem solving.

3. Focus here is on BODMAS types of operations; more advanced operations are in later resources.

4. MATLAB supports both symbolic algebra and number crunching and is far easier to use than a hand calculator.

Slides by Anthony Rossiter

2

We give a quick overview of MATLAB tools and usage and then do a live demonstration of all aspects covered at the end.

Page 3: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

Basic algebra

• MATLAB will handle any basic BODMAS type of operation.

• Simply type the desired computation and the answer will appear.

• We will demonstrate live but also show some typical screen dumps here for convenience.

Slides by Anthony Rossiter

3

Page 4: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

Using variables

One advantage of MATLAB compared to a calculator is the ease with which computations can be allocated to variables and therefore the answers stored for later use.

A simple illustration is given here.

Slides by Anthony Rossiter

4

Variables automatically stored in workspace for re-

use.

Use name=

to allocate to a variable name.

Page 5: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

Mathematical functions

Most simple functions can be obtained by typing the names, e.g.

If the computation is not allocated to a variable, MATLAB stores in the variable ‘ans’.

For more functions, type >> help elfun

Slides by Anthony Rossiter

5

Page 6: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

Long expressions

Slides by Anthony Rossiter

6

It is easy to write long expressions as may arise in problem solving. These are far easier to view than on a typical calculator.

If you make a mistake, use ‘uparrow’ to recall the command and simply edit as required.

Changed here only

Page 7: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

Being efficient

• Rather than typing into the command window over and over again, it is better to save the core commands in a file.

• MATLAB provides ‘script files’ for this.

• Select ‘new script’ and save in a folder of your choice.

Slides by Anthony Rossiter

7

Select here.

Page 8: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

Slides by Anthony Rossiter

8

Select here to give the file a name and select a folder to save to.

Default ending for MATLAB files is .m

Do not change this!

We will demonstrate the process at the end of this

video.

Script files covered slowly in resource 4.

Page 9: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

Opening a saved file • Go to the file tab and open from there.

• Advisable to do this way and do not find the file on ‘windows explorer’ and open from there.

• Opening from within MATLAB ensures that some core software links work effectively.

Slides by Anthony Rossiter

9

ALWAYS open files using this tab!

(or open from within

the file editor window)

Page 10: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

Algebra

MATLAB will also do symbolic algebra.

To do this, variables must be defined as ‘symbolic’ – the default is variables contain a numeric value.

Slides by Anthony Rossiter

10

Note different variable types.

Page 11: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

Evaluating symbolic expressions

A file called subs.m will substitute numerical values into symbolic expressions.

This is easiest understood by a demonstration.

Slides by Anthony Rossiter

11

1434)4(

13)(

2

2

m

fffm Expression and value go here.

Create symbolic variable.

Symbolic variables covered slowly in resource 7.

Page 12: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

LIVE DEMONSTRATIONS WITH MATLAB

Open the following and try the instructions

matlab_basics2.m

matlab_basics2b.m

Slides by Anthony Rossiter

12

Page 13: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

Conclusions

Demonstrated the effectiveness of MATLAB for carrying out and saving simple algebra and numerical computations.

1. Allows transparency for very long expressions.

2. Allows easy recall of commands for editing.

3. Allows easy definition and saving of variables to memory (denoted the workspace).

I prefer to save regular and validated complicated expressions or sequences of calculations into a script file for re-use whenever I need them.

Slides by Anthony Rossiter

13

Page 14: MATLAB Basics 2 simple calculations and usecontroleducation.group.shef.ac.uk/MATLAB/MATLAB... · 4. MATLAB supports both symbolic algebra and number crunching and is far easier to

© 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