1
Maths for Engineers and Scientists 4: Matlab
Contents
1 Introduction to Matlab 1
1.1 What is it? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 On-line help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Using the Editor to create a new
le . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 M-
les . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 Laplace and Inverse Laplace transforms . . . . . . . . . . . . . . . . . . . . . . . 8
1.6 The Matlab ODE solvers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2 Problem sets 11
2.1 Problem set 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 Problem set 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 Problem set 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.4 Problem set 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.5 Problem set 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1 Introduction to Matlab
These notes mostly refer to Matlab under Windows on PC Caledonia. If you would like run
Matlab at home you need to buy the program. There is a free alternative to Matlab called
Octave , which is very similar toMatlab . Most of the programs introduced in this document
(apart from those requiring Symbolic Math Toolbox for Matlab ) will run under Octave with
at most minor modi
cations.
The following notes provide a quick summary of Matlab to allow a quick start. The booklet
An Introduction to Matlab by David F. Gri
ths (downloadable through Vision) provides a
broader introduction to Matlab .
1.1 What is it?
Matlab is essentially a programming language and is similar to C, Pascal, Fortran and other
widely-used programming languages. (Technically it is a bit of a mixture between a compiler
and an interpreter.) It also has many built-in functions for
nding approximate solutions of
mathematical problems, doing linear algebra etc. and producing good graphs of the results.
Matlab runs on most standard computer systems (PCs, Macs, Unix workstations etc.) and
looks and behaves almost the same on each one.
Matlab is widely used in higher education to teach numerical analysis and other topics because
it is much easier to learn and to use than most other programming languages. However, once
you have mastered Matlab , you will
nd it easy to learn any programming languages required
1
for work in the real world or further study. Matlab is increasingly being used in industry,
both as a quick way to develop and test complex software, and for the facilities provided by the
many add-on toolboxes tailored for dierent applications. These toolboxes include
nancial
modelling, image enhancement, systems control, simulation and optimisation.
1
real world = outside university
, 2
1.2 On-line help
• If you know the name of the command you want to
nd out about, type help *** in the
Matlab command window to get information on the Matlab
command *** or M-
le
***.m (here *** stands for any command name e.g. mean, std, sum etc. listed in Section
28 of the Introduction to Matlab
). You may also type doc *** in order to get the same
information in a separate window.
• Other useful help facilities for Matlab can be found in the Mathematics folder on PC-
Caldonia.
1.3 Using the Editor to create a new
le
• Make sure you are in your home directory.
• Click on File (at the top left of the Matlab command window) and select New and then
M-file.
• The Matlab Editor/Debugger window should open, and type the line below into it
avec = [1, 2, 3]
• Now save the
le by clicking on the File option at the top left of the editor window. Select
Save As and you should see a dialogue box opening. You need to specify a name for your
le and say what directory you want to put it in. You choose the directory in the Save
in box: pull down the arrow at its right side until you come to something that starts with
your username (it is likely to be there already).
Type myprog.m in the File name box and then click on the Save button to save your
program. It is called myprog.m and it is stored in your home directory (corresponding to
h: in Matlab).
• To test this out (and check whether you have saved the
le correctly), go back to the
Matlab Command Window prompt and type myprog to try to run the M-
le myprog.m.
If the program has been saved correctly you should get the response
avec =
1 2 3
If it still doesn't work then go back and carefully follow the itemised steps above again. It
is very important that you learn how to save
les before you create anything complicated.
Matrices
Matlab only really deals with one type of object rectangular matrices, whose entries can
either be numbers (real or complex), or text string characters. Matrices with only one row or
column can be treated as vectors, and 1×1 matrices as scalars. (See Sections 5, 8 and 16 of
An Introduction to Matlab .)
Type the lines
, 3
x = [-2, 4, -8]
y = [3, 5, 7]
z = [1; 2; 3]
a = 5
A = [-1 0; 3 1]
to create two 1×3 (row) vectors (x and y ), a column vector (z ), a scalar (a) and a 2×2 matrix
(A). Note that unlike some programming languages the case of variable names does matter in
Matlab , so that a and A represent dierent quantities. Matrices are usually entered in for
loops ( An Introduction to Matlab Section 19) or using colon notation. Note that ending a
line with a semicolon ; stops the answer being displayed. Try it out.
Individual matrix entries can be referenced by indices in brackets: e.g. typing
x(2)
x(1,2)
A(2,2)
results in 4, 4 (the
rst two commands refer to the same element of x), and 1. Note that a
matrix or vector will only accept positive integers as indices, so that B(12,197) is OK, whilst
B(0,1), B(2,-3), or B(1.3,1) are not.
Matrix operations
Most of matrix operations are listed in Section 16 of An Introduction to Matlab . Note that
many of these can be made to work entry-wise by typing a dot . before the operation e.g.
A*A is the matrix A2 , whilst A.*A is the matrix with (i, j) entry equal to A(i, j)2 . Con
rm this
in Matlab with the 2×2 matrix A below:
>> A = [-1 0 ; 3 1]
A =
-1 0
3 1
>> A*A
ans =
1 0
0 1
>> A.*A
ans =
1 0
9 1
See Section 14 and 16.9 of An Introduction to Matlab .
Matrix eigenvalues and eigenvectors
To
nd eigenvalues of a matrix A we can use
>> A = [-1 0 ; 3 1]