Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: Learn Matlab
Description: Detailed notes on the basics of Matlab. These notes are for anyone looking to start learning Matble and is most relevant for scientists and engineers.
Description: Detailed notes on the basics of Matlab. These notes are for anyone looking to start learning Matble and is most relevant for scientists and engineers.
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
1
Maths for Engineers and Scientists 4: Matlab
Contents
1
Introduction to
2
Matlab
1
1
...
1
...
2
1
...
2
1
...
6
1
...
8
1
...
9
Matlab
1
Problem sets
11
2
...
11
2
...
13
2
...
15
2
...
16
2
...
18
1
Matlab
Introduction to
Matlab
Matlab
These notes mostly refer to
under Windows on PC Caledonia
...
There is a free alternative to
Octave
, which is very similar to
at most minor modications
...
Most of the programs introduced in this document
Matlab
Matlab
Symbolic Math Toolbox for
(apart from those requiring
Matlab
Octave
Matlab
Matlab
) will run under
with
to allow a quick start
...
Griths (downloadable through Vision) provides a
broader introduction to
...
1 What is it?
Matlab
is essentially a programming language and is similar to C, Pascal, Fortran and other
widely-used programming languages
...
)
It also has many built-in functions for nding approximate solutions of
mathematical problems, doing linear algebra etc
...
Matlab
Matlab
runs on most standard computer systems (PCs, Macs, Unix workstations etc
...
it is
is widely used in higher education to teach numerical analysis and other topics because
much
easier to learn and to use than most other programming languages
...
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
...
1
real world = outside university
2
1
...
m (here *** stands for any command name e
...
mean, std, sum etc
...
You may also type doc *** in order to get the same
If you know the name of the command you want to nd out about, type
Matlab
Matlab
Matlab
information in a separate window
...
Matlab
can be found in the Mathematics folder on PC-
1
...
Click on File (at the top left of the Matlab command window) and select New and then
M-file
...
Select
Save As and you should see a dialogue box opening
...
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)
...
m
in the File name box and then click on the Save button to save your
program
...
m
and it is stored in your home directory (corresponding to
in Matlab)
...
m
...
It
is very important that you learn how to save les before you create anything complicated
...
Matrices with only one row or
column can be treated as vectors, and
An Introduction to
Type the lines
Matlab
...
(See Sections 5, 8 and 16 of
3
x
y
z
a
A
=
=
=
=
=
[-2, 4, -8]
[3, 5, 7]
[1; 2; 3]
5
[-1 0; 3 1]
to create two
1×3
(row) vectors (x and
y ),
a column vector (z ), a scalar (a) and a
(A)
...
Matrices are usually entered in for
Matlab
Section 19) or using colon notation
...
Try it out
...
g
...
Note that a
so that B(12,197) is OK, whilst
results in 4, 4 (the rst two commands refer to the same element of
matrix or vector will only accept positive integers as indices,
B(0,1), B(2,-3),
or
B(1
...
Matrix operations
Most of matrix operations are listed in Section 16 of
An Introduction to
many of these can be made to work entry-wise by typing a dot
A*A
in
is the matrix
Matlab
A2 ,
with the
whilst
2×2
A
...
*A
ans =
1
9
0
1
See Section 14 and 16
...
...
Note that
before the operation e
...
entry equal to
A(i, j)2
...
we type
>> A = [-1 0 ; 3 1]
A =
-1
3
0
1
>> [V,D]=eig(A)
V =
0
1
...
5547
-0
...
A
and the diagonal matrix
D
contains the
Functions
These are introduced in Section 7 and listed in Section 28 of
An Introduction to
Matlab
...
Try out the following
examples (remember that you can nd out what the command
exp(x)
sy = sqrt(y)
abs(A)
floor(sy)
ceil(sy)
max(x)
***
does by typing
help ***)
...
This code:
•
initialises the variable
•
for each integer
The command
i
sum20
to zero;
between 1 and 20 performs the command
i = 1:20
sum20 = sum20 + i
...
It means all the integers between
a:b:c means all
the numbers a, a+b, a+2b,
...
The syntax is first:increment:last
...
g
...
1 and 20, starting with 1 and ending with 20
...
1:2:10
5:-7:-30
14
...
2:8
...
24:9
...
24 and not 9
...
else
The
if
Matlab
Sections 19 and 20 deal with for loops and related things in detail
...
a = 3;
b = 2;
if (aj = -1;
else
j = 0;
end
What is the value of
j
in the above example?
The code below produces a vector
v = zeros(1,10);
for i = 1:10
if (i < 2)
v(i) = 1;
elseif (i > 1 && i <= 5)
v(i) = 2;
else
v = 1 2 2 2 2 3 3 3 3 3
...
2
of
An Introduction to
Matlab
explains the
if
statement in more detail
...
, (xn , yn )
...
The simplest plot command has the form
length,
n
say
...
01:2*pi];
v = sin(t);
plot(t,v)
Section 10 of
An Introduction to
Matlab
describes how to make multi-plots, 3D plots and how
to change the axes scaling and line styles
...
g
...
e
...
Another example is
st = 'my name is Frankenstein'
Note that both quotes are the same
...
which converts a number into a string
...
Note that in the above example using
title, the string has more than one element
...
1
...
You can either type instructions directly at the prompt
or make up a le or les containing the instructions you want
le (called an M-le) to
Matlab
Matlab
to execute and feed this
...
Typing commands at the prompt is ne if you want to execute single commands, but
errors usually creep into the typing of longer expressions and sequences of operations and it is a
pain having to retype things used over and over again
...
3 above
...
Script M-les act as a shorthand for a sequence of
Matlab
commands and are
a bit easier to use than function M-les
...
7
A sample script M-le
Suppose we want to write an M-le to nd the sum of the rst 20 integers, instead of typing the
commands at the
M-les end in
...
The following le which we shall call
S20
...
Follow the instructions in Section 1
...
% S20
%
%
%
Find the sum of the first 20 integers
Computes and outputs the sum 1 + 2 +
...
% Note that this line is ignored
...
The semicolon suppresses the output
...
Typing
help S20
will print out the lines at the top of the le that start with a
up to the rst line that doesn't start with a
anyone else who uses your
Matlab
%
...
It is good style
to always start an M-le with a comment line
...
%,
Sample function M-les
The following le (called
mkvec
...
function v = mkvec(x)
%MKVEC Construct the vector [1, x, x^2, x^3] from x
%
mkvec(x) returns the vector [1, x, x^2, x^3]
v = [1, x, x^2, x^3];
input argument (x), the 4component vector output argument v and a function
statement (the rst line)
...
It contains an
You should get the result
ans =
1
2
4
8
8
You can use the M-le to create named vectors for example if you type
w = mkvec(6)
then
you obtain
w =
1
6
36
216
instead
...
g
...
m
below:
function v = mkvec2(x,n)
%MKVEC2 Construct the vector [1, x, x^2,
...
, x^n]
for k = 0:n
v(k+1) = x^k;
end
and more than one output argument e
...
the le
function
%MKVEC3
%
%
mkvec3
...
, x^n] from x and n
[v, len] = mkvec3(x,n) returns the vector
v = [1, x, x^2,
...
for k = 0:n
v(k+1) = x^k;
end
len = length(v);
You can also use dot operations to avoid loops in the previous example, e
...
mkvec3a
...
, x^n] from x and n
[v, len] = mkvec3(x,n) returns the vector
v = [1, x, x^2,
...
not use loops
k = 0:n;
v = x
...
1
...
The syntax is quite similar to
with(inttrans):
f:=t:
laplace(f, t, s);
we can type
Matlab
...
6 The
Matlab ODE solvers
The Matlab ODE solvers are written to solve systems of DE's of the form
dx1
dt
dx2
dt
dx3
dt
=
f1 (t, x1 , x2 , x3 ,
...
)
=
f3 (t, x1 , x2 , x3 ,
...
i
...
dx
= F (t, x)
...
g
...
ode45, ode23, ode113,
(each solver has its advantages and disadvantages)
...
1 Problem set 1
1
...
6, 0
...
, 1
...
5)
(c) Find the transpose of the vector
(d) If
A
and
B
n×n
2×2
are
it out for some
x
using 2 colons and three numbers
...
matrices, what is the dierence between
A∗B
and
A
...
x = (0
...
7,
...
4, 4
...
2
...
(b) Find its length
...
Use
length
and
sum
and see Section 14
...
...
using the line style stars and put appropriate labels on the
graph
...
1, 0
...
, 10)
...
Generate the vector
(b) Plot vector
An Introduction to
An Introduction to
Matlab
...
1/3 1/4 1/5
(b) Enter a vector
b = (3, 1
...
4333)T
(c) Find the solution
Note that the matrix
x
A
to the linear system
is the
simply by using the command
5
...
Create
(a) a
9×9
identity matrix,
(b) a
7×9
zero matrix,
(c) a
4×6
matrix of ones
...
b
is a column vector)
...
(hint: use
\)
...
(See
output ? Try it out by typing
help format)
...
e
...
(a) Use the editor to create a function M-le called
positive integer
and as output the result
N,
p=
N
X
p
anysump
...
j=1
(b) Compute from the Matlab command window, the value of the sum for
8
...
Use
(b) Add labels to the
x
and
y
x = −5 : 0
...
x
to plot
f (x) =
coordinates
...
(c) Annotate the graph to show which curve is being depicted
...
(a) Follow the examples in Section 10 of
x + x2
An Introduction to
− x3 /10 − 10 with a dashed line style and
the same graph
...
05 : 10
and
y
for the
x
Matlab
g(x) = cos(x)
to plot
f (x) =
with a solid line in
coordinates
...
(c) Annotate the graph to show which curve is which and place the text f=g here near
where the curves cross
...
Write a function M-le that takes as input the positive integer
outputs the sum
n
X
n
and number
r,
and
kr
...
Plot an
(x, y)
n (n + 1) (2n + 1)
6
n
...
Use the information from the
nd the root of the equation
sin(x) + 1 − x = 0
...
2 Problem set 2
x ∈ [−π, π] (labelled
Figure window (subplot)
...
Produce 4 separate graphs against
tions (a)-(d) below all in the same
2
...
(a) Write a function M-le using
vector
b
for loops to dene the N × N
β
γ
α
β
...
...
0 ···
0
...
...
...
0
β γ
α β
γ
...
α
0
which will work for any integer
matrix
tridiagonal matrix
given by
N ≥ 3
...
The
is called tridiagonal because all its entries are zero apart from those on or
immediately next to the diagonal
...
in the
of
the case
N = 4,
and
(Use the backslash
\
command
...
(c) Use the command
the eigenvalues and eigenvectors of the matrix
3
...
(a) Write an function M-le where the input is the size
N
and the output is the time
(cputime) in seconds it takes by each of the two methods to compute the solution of
Ax = b
for randomly chosen
A
and
b (rand)
...
, 800
...
4
...
Step 1 Make a guess at the solution (call it
Step 2 calculate the sequence of values
xk+1 = xk −
x1 ),
x2 , x3 ,
...
,
14
|xk+1 − xk | < TOL,
k = 100 without nding
TOL
Step 3 stop when
where
or if we reach
a solution
...
It will make life easier for you if you write the M-le in such a way that
the equation to be solved can be changed easily
...
fzero rst of all, then apply
−6 and try two
your Newton function to the equation cos(x) = x with TOL= 10
dierent starting guesses x1 = 1
...
4 (which doesn't)
...
Is there a pattern in which starting guesses work and which do
not? (for, if, break)
...
3 Problem set 3
1
...
subplot(2,2,2)
and mark
it with a star at its maximum
...
The derivative of a well-behaved function
various
nite dierence approximations:
(F) : f 0 (x) ≈
f (x + h) − f (x)
,
h
f (x)
with respect to
(C) : f 0 (x) ≈
x
can approximated by
f (x + h) − f (x − h)
...
The number
relatively small, and the approximations should, in theory, get better as
(a) Write a function M-le that takes as input
central approximate derivatives of
(b) Set
x = 1/2
x and h,
f (x) = sin−1 (x)
...
and outputs the forward and
and plot the modulus of the error in the results against
h
for
h =
10−10 , 10−9 ,
...
Which gives better results and why do they stop getting better
as
h
decreases? Use the
loglog
function to plot, and label the graph appropriately
...
4 Problem set 4
1
...
To nd
a
f (x) dx,
the interval
a≤x≤b
is split into
N
strips of equal width, and the function is assumed to be constant over the strip making
it easy to integrate
...
The approximation formula that results is
Zb
I≡
f (x) dx ≈ h
N
X
f (xj ) ≡ IN
j=1
a
h = (b − a)/N and the mid points of the strips are
xj = a + h(j − 21 )
...
R2
3
(a) Write an M-le to approximate the integral
1 sin(x ) dx with N = 8, 16, 32,
...
(b) Tabulate the result to full precision and make an estimate of the error in the results
...
One of the best known examples in dynamical systems and chaos theory is the Logistic
xk+1 = λxk (1 − xk ) for k = 1, 2,
...
Map
...
(b) Use this to plot
same gure
...
7, N = 100
x1 ∈ (0, 1)
...
, xN )
...
7, 0
...
7 + 10−6 , 0
...
7 case
...
7, N = 500
with
pattern the solution settles into
...
3, 3
...
739, 3
...
)
3
...
h > 0
...
Step 3 Find an approximation of y step-by-step from the recursion
yn+1 = yn + hf (yn , tn )
with initial value
If
h
y0 = a
...
17
(a) Write an M-function with inputs the parameter
output the two vectors
t
and
y
h
and length of the interval
l,
and
obtained by applying Euler's method to the initial
value problem
dy
= (1 − t)y,
dt
(for
y(0) = 1
...
(b) The exact solution of this ODE can actually be found analytically,
Plot a graph comparing the exact and the approximate solutions for
2
y(t) = et−t /2
...
Use
a blue lled line for the exact solution and a green dashed line for the approximated
one
...
(plot)
...
g
...
18
2
...
Use
Matlab Maple
(or
) to compute Laplace transforms from the Table of Laplace trans-
forms from the lecture notes and from the Tutorial exercises
...
Use
Matlab
(or
Maple
) to nd inverse Laplace transforms of the functions from the
Tutorial exercises
...
Solve the LCR circuit example problem from the Lecture notes using the
Matlab
built-in
ODE solvers and compare the computed solution to the exact solution from the lecture
notes
...
Use the
Matlab
build-in ODE solver to solve the following system of ODEs for
t ∈ (0, 5)
dx1
= x1 + 2x2 ,
dt
dx2
= 2x1 − 2x2 ,
dt
x1 (0) = 2
x2 (0) = 1, ,
Compare the computed solution with the exact solution from Section 3
...
5
...
g
...
01)
...
, Lecture notes
where
Figure 3
...
5 sin (2t − 6)
t < 3,
t ≥ 3
Title: Learn Matlab
Description: Detailed notes on the basics of Matlab. These notes are for anyone looking to start learning Matble and is most relevant for scientists and engineers.
Description: Detailed notes on the basics of Matlab. These notes are for anyone looking to start learning Matble and is most relevant for scientists and engineers.