1
MATLAB Basics 2
simple calculations and use
Anthony Rossiter
University of Sheffield
For a neat organisation of all videos and resources
http://controleducation.group.shef.ac.uk/indexwebbook.html
Slides by Anthony Rossiter
2
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.
We give a quick overview of MATLAB tools and usage and then
do a live demonstration of all aspects covered at the end.
Slides by Anthony Rossiter
3
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
4
Using variables
One advantage of MATLAB
compared to a calculator
is the ease with which Use
computations can be name=
allocated to variables and to allocate to a
variable name.
therefore the answers
stored forVariables
later use.
automatically stored
in workspace for re-
use.
A simple illustration is
given here.
Slides by Anthony Rossiter
5
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
6
Long expressions
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
Changed here only
simply edit as
required.
Slides by Anthony Rossiter
7
Being efficient
• Rather than typing into
the command window Select here.
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
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.
Slides by Anthony Rossiter
9
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.
ALWAYS open files
using this tab!
(or open from within
the file editor
window)
Slides by Anthony Rossiter
10
Algebra
MATLAB will also do
symbolic algebra.
To do this, variables
must be defined as
‘symbolic’ – the default
is variables contain a
numeric value.
Note different
variable types.
Slides by Anthony Rossiter
11
Evaluating symbolic expressions
A file called subs.m
will substitute Create symbolic
variable.
numerical values into
symbolic expressions.
This is easiest
understood by a
demonstration.
m( f ) f 3 f 1 2
Expression and
value go here.
m ( 4) 4 3 4 1
2
Symbolic variables covered
slowly in resource 7.
Slides by Anthony Rossiter
12
LIVE DEMONSTRATIONS WITH
MATLAB
Open the following and try the instructions
matlab_basics2.m
matlab_basics2b.m
Slides by Anthony Rossiter
13
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
For a neat organisation of all videos and resources
http://controleducation.group.shef.ac.uk/indexwebbook.html
Anthony Rossiter
Department of Automatic Control and
Systems Engineering
University of Sheffield
www.shef.ac.uk/acse
© 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.)