Numerical Methods Course Notes
Numerical Methods Course Notes
Contents
0 Introduction 4:1
1 Matlab 4:4
5 Splines 4:40
A Summary 4:96
0 Introduction
Numerical methods Numerical methods are algorithms used
to obtain approximate solutions to algebraically intractable math-
ematical problems. Such problems arise when modelling many
physical, biological, chemical and financial systems and phenomena.
Computer technology facilitates the application of numerical meth-
ods and the simulation of complex systems. These simulations (or
numerical experiments) allow us to better understand, predict and
optimize such systems.
4:2 References
References
Fornberg, B. & Flyer, N. (2015), ‘Solving PDEs with radial basis
functions’, Acta Numerica 24, 215–258.
Hahn, B. H. & Valentine, D. T. (2013), Essential Matlab for
Engineers and Scientists, 5th edition edn, Academic Press.
http://www.sciencedirect.com/science/article/pii/
B9780123943989000204
Johnson, R. (2014), Matlab style guidelines 2.0, Technical re-
port, Datatool, https://au.mathworks.com/matlabcentral/
fileexchange/46056-matlab-style-guidelines-2-0.
Kreyszig, E. (2011), Advanced engineering mathematics, 10th edn,
Wiley.
1 Matlab
1.1 Assumed knowledge about Matlab/Octave
For more details about these concepts:
3
https://swcarpentry.github.io/matlab-novice-inflammation/06-cond.
html
F = nan(1,n);
F(1:2) = 1:2;
for k = 3:n
F(k) = F(k-1) + F(k-2);
end
x = linspace(-1,1);
P = nan(n+1,length(x));
P(1,:) = ones(size(x)); %or 1+0*x
P(2,:) = x;
for k = 1:n-1
P(k+2,:) = ((2*k+1)*x.*P(k+1,:)-k*P(k,:))/(k+1);
end
plot(x’,P’), legend(num2str((0:n)’))
1.3.2 Auto-replication
Matlab 2016+ automatically
1 −2 replicates array operations Although
the product 2 −3 4 is well defined mathematically, the sum
1 −2
2 + −3 4 is not defined mathematically. Nonetheless, such sums
are so useful that Matlab defines them, and many others of the
same ilk—be wary.
Re +, -, .*, ./, .^, <, >=, & et al.: for such element-by-element
operators, Matlab automatically replicates scalars and vectors as
appropriate. For example,
• 2 + −3
1 −2 1 −2 3 0
4 7→ [ 22 22 ] + −3 4 7→ −1 6
• max(1, −3 4 ) 7→ max [ 11 11 ] , −3
1 −2 1 −2
7→ [ 11 14 ]
4
• [ 1 −2 ] .* −3
1 −2
7→ 11 −2
1 −2 1 4
4 −2 .* −3 4 7→ −3 −8
• −2 + −3 4 7→ −2 −2 + −3 4 7→ −5 2
1 1 −2 1 1 1 −2 2 −1
• [ 34 ] ./ [ 1 −2 3 ] 7→ [ 34 34 34 ] ./ 11 −2 3
3 −1.5 1
−2 3 7→ 4 −2 1.33···
• but [ 1 −2 3 ] .* 1 −2
−3 4 is an error as the non-1 sizes do not
match.
• Set i=1:3
• First, i’+i is mathematical nonsense, but Matlab auto-
replicates the vectors to
h1i h1 1 1i h1 2 3i h2 3 4i
2 + [ 1 2 3 ] 7→ 222 + 123 7→ 345
3 333 123 456
1 1/2 1/3
h2 3 4i h1 1 1i h1 2 3i
1./ 345 7→ 111 ./ 234 7→ 1/2 1/3 1/4
456 111 345 1/3 1/4 1/5
∂2u 2
2∂ u
= c , 0 < x < L, t > 0,
∂t2 ∂x2
subject to boundary conditions
Example
1. Write a Matlab script that evaluates this solution for 0 ≤
x ≤ L at a single time t. Use for loops and scalar operations
only.
• string1a.m computes u at one given x and t.
• string1b.m computes u at one given t, and for all x
along the string.
2. Vectorise your script and compare the run time of the vec-
torised script with your previous script.
• string1c.m vectorises the code somewhat and executes
twenty times faster.
• string2.m fully vectorises the code somewhat and ex-
ecutes faster still (but not by much as Matlab, and
other compliers, do parse code and automatically try to
vectorise it for you).
3. Animate the solution by stepping forward through time using
a for loop.
• See string3.m
4. Write a vectorised Matlab function that evaluates the solu-
tion for 0 ≤ x ≤ L at a single time t. Document the function
using the conventions required for this course.
Vectorised function
B = @(n) L^2/(ell*(L-ell))*2./(n.^2*pi^2).*sin(pi*ell/L*n);
n = 1:nSum;
u = B(n).*sin(pi*x/L*n)*cos(c*pi/L*n’*t);
end
Answer:
Vectorised implementation
dt = 1/fs;
u = synthesize(x, 0:dt:T, ell, L, 2*f1*L, nSum);
sound(u, fs)
Solution
function s = intTrap(func, x)
% Trapezoidal rule estimate of the integral of func()
% s = intTrap(func, x) estimates the definite integral
% of func from x(1) to x(end) using the trapezoidal rule.
% Numerical Methods, Aug 2018
% Input/Output:
% func - a user defined function handle.
% x - a vector of sample points in ascending order.
% s - the trapezoidal estimate of the integral.
f = func(x);
j = 1:length(x)-1;
s = 0.5*sum((f(j+1) + f(j)).*diff(x));
1.5 Summary
• All function and script programs—as for all documents—must
have a descriptive title, author and date.
• The first comment line in a function/script must be a one line
summary so that lookfor is useful.
• Comments in code must add information.
• When a value or expression is used several times, define once
and refer to it by name. Unless a significant parameter, avoid
introducing a variable which is then only used once.
• Code with style: use meaningful names in the context of the
problem; generally avoid multiple statements on a line; use
good indentation and blank spaces; avoid long lines.
• Include clean graphics in your document: do not use a bit-
image format; the information must be a good size; label axes;
include informative caption or title (not both).
• Vectorise code where feasible: prefer to code at the level of
vectors and matrices.
• Use for-loops only where necessary; prefer vectorisation.
2 Polynomial interpolation
2.1 Interpolation
1.5 f(x)
0.5
y(x), f(x)
0
-0.5
-1
-1.5
-2
0 1 2 3 4
x
1.5
(x0 ,f 0 )
1 fj = f(x j)
0.5
y(x), f(x)
-0.5
-1 (x1 ,f 1 )
(xj,f j)
-1.5 (xN-1 ,f N-1 )
-2
0 1 2 3 4
x
1.5
(x ,f )
0 0
1
0.5
y(x), f(x)
0
-0.5
-1 (x1 ,f 1 )
(xj,f j)
-1.5 (xN-1 ,f N-1 )
-2
0 1 2 3 4
x
1.5
y(x0 )=f0
1
0.5
y(x), f(x)
0 y(x)
-0.5
y(x1 )=f1
-1
y(xj)=fj
-1.5
y(xN-1 )=fN-1
-2
0 1 2 3 4
x
1.5 f(x)
0.5
y(x), f(x)
0 y(x)
-0.5
-1
-1.5
-2
0 1 2 3 4
x
2
1.5 f(x)
0.5
y(x), f(x)
0 y(x)
-0.5
-1
-1.5
-2
0 1 2 3 4
x
Polynomial interpolation
p2 (0) = a0 = 0
p2 (1) = a0 + a1 + a2 = 1
p2 (2) = a0 + 2a1 + 4a2 = 0
1 x0 x20 · · · xn0
a0 f0
1 x1 x2 · · · xn a1 f1
1 1
.. .. .. .. .. = .. .
. . . . . .
2
1 xn xn · · · xn n an fn
Notice that
• L0 (x0 ) = 1, L1 (x0 ) = 0 =⇒ p1 (x0 ) = f0 , and
• L0 (x1 ) = 0, L1 (x1 ) = 1 =⇒ p1 (x1 ) = f1 .
Figure 1: y = ex
3
p1 (x) = 1 − x + ex
x
−1 −0.5 0.5 1
1.5
1
f(x)
0.5
-0.5
-1
-1 -0.5 0 0.5 1
x
1.5
1
f(x)
0.5
-0.5
-1
-1 -0.5 0 0.5 1
x
1.5
1
(xj,f j)
0.5
y(x), f(x)
-0.5
-1
-1.5
-2
0 1 2 3 4 5
x
2
1.5
f(x)
1
0.5
y(x), f(x)
-0.5
-1 y(x)
-1.5
-2
0 1 2 3 4 5
x
The linear polynomial that interpolates the data (xj , fj ) and (xj+1 , fj+1 )
on the jth interval is
x − xj+1 x − xj
p1 (x) = fj + fj+1
xj − xj+1 xj+1 − xj
1.5 (xj,f j)
1
(xj+1 ,f j+1 )
0.5
y(x), f(x)
-0.5
-1
-1.5
-2
0 1 2 3 4 5
x
1.5 (xj,f j)
1
(x ,f )
j+1 j+1
0.5
y(x), f(x)
-0.5
-1
-1.5
-2
0 1 2 3 4 5
x
1.5
f(x)
1
0.5
y(x), f(x)
0
-0.5
-1 y(x)
-1.5
-2
0 1 2 3 4 5
x
1.5 (xj,f j)
1
(xj+1 ,f j+1 )
0.5
y(x), f(x)
-1
-1.5
-2
0 1 2 3 4 5
x
1.5 (xj,f j)
1
(x ,f )
j+1 j+1
0.5
y(x), f(x)
0
-1
-1.5
-2
0 1 2 3 4 5
x
2
1.5
f(x)
1
0.5
y(x), f(x)
-0.5
-1 y(x)
-1.5
-2
0 1 2 3 4 5
x
f 00 (t)
1 (x) = (x − xj )(x − xj+1 ),
2
where t ∈ [xj , xj+1 ]. Suppose the points are equi-spaced and h =
xj+1 − xj . Then (Quarteroni et al. 2014, Proposition 3.3)
h2
|1 (x)| ≤ max |f 00 |
8
y = [ 3 5 5 5 2 2 ]
Scalar implementation
Answer: In a scalar implementation, we loop through each value of x
and use the min(abs()) functions to identify the index j of the element
in xj that is closest to x(i).
If a is a vector, then:
The tilde ~ tells Matlab that the output in this position in the list is
not needed.
Answer:
A =
1 2 3 4 5 6
-2 -1 0 1 2 3
-4 -3 -2 -1 0 1
-8 -7 -6 -5 -4 -3
How can you use this matrix to find the index of the nearest point
in xj to each element of x?
Answer: [~, j] = min(abs(A)) returns a row vector j containing the
index of the minimum value along each column of abs(A).
j =
1 2 2 2 3 3
If A is a matrix, then
• aMin = min(A) returns a row vector containing the minimum
element from each column.
• [aMin,i] = min(A) also returns a row vector i containing
the index of that element.
What Matlab commands could be used to create this matrix?
The old answer was via meshgrid. Now we use auto-replication:
A = x-xj’ to get
A =
1 2 3 4 5 6
-2 -1 0 1 2 3
-4 -3 -2 -1 0 1
-8 -7 -6 -5 -4 -3
Write a fully vectorised function called nearest.m that implements
nearest neighbour interpolation. Your function must use the inter-
face:
function y = nearest(x,xj,fj)
where x is a row vector of points at which the interpolant is to be
evaluated, xj is a vector containing the data points xj and fj is
a vector containing the data values fj . The output vector y must
be the same size as x and contain the values of the interpolant for
each element of the vector x.
Answer:
function y = nearest(xj,fj,x)
% documentation: name, date, description of function,
% inputs, outputs
[~,j] = min(abs(x-xj’));
y = fj(j);
x − xj+1 x − xj
y(x) = p1 (x) = fj + fj+1 ,
xj − xj+1 xj+1 − xj
y = [ 3 4 5 4 3 4 ]
Scalar implementation
Answer: In a scalar implementation, we loop through each value of x
and use the sum() function to identify the index j of the element in xj
that satisfies x(i) >= xj.
n = length(xj);
for i = 1:length(x)
j = min(max(1,sum(x(i) >= xj)),n-1);
y(i) = ( fj(j+1)*(x(i) - xj(j )) ...
- fj(j )*(x(i) - xj(j+1)) )/(xj(j+1) - xj(j));
end
Answer:
I =
1 1 1 1 1 1
0 0 1 1 1 1
0 0 0 0 1 1
0 0 0 0 0 0
How can you use this matrix to find the index j such that xj(j) <= x < xj(j+1)?
Answer: j = sum(I) returns a row vector j that is the sum of the
elements down the columns of I, which are the required indices.
j =
1 1 2 2 3 3
function y = linearInterp(x,xj,fj)
% Add appropriate documentation
n = length(xj);
j = sum(x >= xj’);
j = min(max(1,j),n-1);
% [~, j] = histc(x,xj);
% j(x < xj(1)) = 1;
% j(x >= xj(n)) = n-1;
y = ( fj(j+1).*(x - xj(j )) ...
- fj(j ).*(x - xj(j+1)) )./(xj(j+1) - xj(j));
3 Numerical integration
Numerical integration The aim of numerical integration is to
obtain approximate values of the definite integral
Z b
I= f (x) dx
a
in areas such as hydraulics, optics, electromagnetism and demogra-
phy (Quarteroni et al. 2014, §4.1). Numerical integration is needed
when:
• it is impossible
R 2 to calculate the integral analytically—for
example, I = 0 exp(sin x) dx; or
• the function is measured from experiments; or
• the function is computed as a table of values.
One way to derive numerical integration formulae is to approximate
the function f (x) using an interpolant, and then integrate the
interpolant.
This is accomplished by sampling the function on a discrete set
of points, and finding the interpolant that passes through those
points. Piecewise polynomials are ideal for this—they are easily
integrated!
3.1 Midpoint rule
Midpoint rule
Rπ
Example 3.1. Consider the integral I = 0 sin x dx = 2. Approxi-
mate f (x) = sin x using piecewise constant interpolation through
three equi-spaced data points:
1
f (x)
0.8
0.6
0.4
0.2
x
1
2
0 ≤ x ≤ π3 ,
0.5 1 1.5 2 2.5 3 f (x) ≈ 1 π 2π
3 ≤x≤ 3 ,
1
2π
2 3 ≤ x ≤ π.
2π
The area under the interpolant is I ≈ 3 = 2.0944.
More generally, divide the interval [a, b] into n equispaced subin-
tervals of width h = (b − a)/n. Let fj = f (xj ), where xj is the
midpoint of the jth interval and use piecewise constant interpolation
(Quarteroni et al. 2014, §4.3.1).
The integral over the jth subinterval is
Z xj + h Z xj + h
2 2
f (x) dx ≈ fj dx = hfj ,
xj − h
2
xj − h
2
Rπ
Example 3.2. Evaluate I = 0 sin x dx = 2 using the midpoint
rule.
n I error
-----------------
3 2.0944 0.0944
6 2.0230 0.0230
12 2.0057 0.0057
24 2.0014 0.0014
48 2.0004 0.0004
-----------------
Error scaling
Definition 3.5. For any quantity q (such as the error) and any
parameter h (such as the size of the subintervals) we write “q =
O(hp ) as h → 0”, said “q is of order hp ” when
q
lim =C
h→0 hp
4 Numerical differentiation
Numerical differentiation The goal of numerical differentiation
is to obtain approximate values of the derivatives f 0 (x), f 00 (x), . . . , f (n) (x)
of a given function f (x).
Numerical derivatives are needed when:
• the function is computed as a table of values; or
• we want to fit trends to experimental data; or
• numerically solving ordinary and partial differential equations.
One way to derive numerical differentiation formulae is to approxi-
mate the function f (x) using an interpolant, and then differentiate
the interpolant.
As with integration, this is facilitated by sampling the function on
a discrete set of points (or grid), and finding the interpolant that
passes through those points.
Taylor series can also be used to derive and analyse numerical
differentiation formulae.
4.1 Differentation using polynomial interpolation
The derivative is
fj+1 − fj
f 0 (x) ≈ p01 (x) = , xj ≤ x ≤ xj+1 .
xj+1 − xj
Error analysis
1 1
|(xj )|, |(xj+1 )| = |f 00 (t)|h ≤ M h = O(h) as h → 0,
2 2
where h = xj+1 − xj and |f 00 (t)| ≤ M .
For example, use this formula to differentiate f (x) = ex sin x at
xj = 0 and xj+1 = h, and loglog-plot the error for various h. The
blue crosses in this plot shows that the error decreases by an order
of magnitude as h decreases by an order of magnitude: hence the
error is O(h1 ); we turn to the red-circles next.
100
one-sided
10−1 centred
10−2
abs-error
10−3
10−4
10−5
10−6 −3
10 10−2 10−1 100
h
Error analysis
Example 4.4. What is the error of the approximate derivative in
Example 4.3? The error is defined as
fj+1 − fj−1
j = fj0 − .
2h
Using Lagrange’s Remainder Theorem 3.4, the error satisfies
1
|j | ≤ M h2 = O(h2 ) as h → 0,
6
where h = xj+1 − xj and |f 000 | ≤ M .
4.3 Finite difference formulae
5 Splines
A spline
Linear splines
2
y(x)
1.5
1 − x
0 ≤ x ≤ 1,
1 y(x) = 0 1 ≤ x ≤ 2,
0.5
2(x − 2) 2 ≤ x ≤ 3.
x
1 2 3
3 1 1
y(x) = − + x + |x − 1| + |x − 2|
2 2 2
y(x) = a + bx + c2 |x − 1| + c3 |x − 2|,
y(0) = a + c2 + 2c3 = 1 ,
y(1) = a + b + c3 = 0 ,
y(2) = a + 2b + c2 = 0 ,
y(3) = a + 3b + 2c2 + c3 = 2 .
N
X −1
y(x) = a + bx + cj |x − xj |,
j=2
N
X −1
y(xi ) = a + bxi + cj |xi − xj | = fi , i = 1, . . . , N.
j=2
value of x.
9
In mathematics, we use a list in parentheses to denote the corresponding
column vector: e.g., (a, b) = [ ab ].
N
X −1
y(x) = a + bx + cj |x − xj |3 .
j=2
2
y(x)
1.5
0.5
x
0.5 1 1.5 2 2.5 3
y(0) = a + c2 + 8c3 = 1 ,
y(1) = a + b + c3 = 0 ,
y(2) = a + 2b + c2 = 0 ,
y(3) = a + 3b + 8c2 + c3 = 2 .
3
The coefficients are a = 16 , b = − 14 , c2 = 5
16 and c3 = 1
16 .
d
|x − xj | = sign(x − xj ), x 6= xj ,
dx
d
sign(x − xj ) = 0, x 6= xj ,
dx (
1 x > 0,
where sign(x) =
−1 x < 0,
and |x − xj | = (x − xj ) sign(x − xj ).
x
0.5 1 1.5 2 2.5 3
x2 + y 2
1
p
r= 1
0
−1 0
−0.5 0
0.5 y
1 −1
x
Replicate such a row vector for all the (x, y) you want.
That is, evaluate the interpolant using
dist = sqrt((x-xj’).^2 + (y-yj’).^2);
u = abc(1) + abc(2)*x + abc(3)*y + dist*abc(4:N+3);
where x and y are equal-length vectors containing the coordinates
of the points at which the interpolant is to be evaluated.
Saved as rbfspline2d.m on MyUni.
u(xi ) = fi , i = 1, . . . , N,
N
X
⇐⇒ a + b · xi + cj kxi − xj k = fi .
j=1
N
X N
X
cj = 0 and cj xj = 0.
j=1 j=1
0 0T
1 1 ··· 1 a 0
0 0 x1 x2 ··· xN b 0
1 xT r1,1 r1,2 ··· r1,N
c1 = f1
1
.. .. .. .. .. .. ..
. . . . . . .
1 xTN rN,1 rN,2 · · · rN,N cN fN
d
X
2 2
ri,j = kxi − xj k = (xi,k − xj,k )2 ,
k=1
using
A = [zeros(d+1,d+1) [ones(N,1) xj]’
ones(N,1) xj sqrt(dist2) ];
Similar to before, we solve with
abc = A\[zeros(d+1,1); fj];
When x has the same structure as xj, evaluate the interpolant using
dist2 = zeros(size(x,1),N);
for k=1:d
dist2 = dist2 + (x(:,k) - xj(:,k)’).^2;
end
u =abc(1)+x*abc(2:d+1)+sqrt(dist2)*abc(d+2:N+d+1);
or more succinctly as
Ax = b.
Solving such systems is one of the central tasks in scientific com-
puting (Quarteroni et al. 2014, Ch. 5).
Examples abound including hydraulic networks, spectrometry, eco-
nomics, and capillary networks (Quarteroni et al. 2014, §5.1).
Factorisation is handy!
Solve the equation 42x = 588 by hand in ten seconds!
y1 = −4 7→ y2 = 0 7→ y3 = −4 .
N
X q
u(x, y) = a + b1 x + b2 y + cj (x − xj )2 + (y − yj )2 ,
j=1
0 0 0 1 1 ··· 1 a 0
0 0 0 x1 x2 ··· xN b1 0
0 0 0 ···
y1 y2 yN b2 0
=
1 x1 y1 r1,1 r1,2 ··· r1,N
c1 f1
. .. .. .. .. .. . .
.. . .. ..
. . . .
1 xN yN rN,1 rN,2 · · · rN,N cN fN
p
where ri,j = (xi − xj )2 + (yi − yj )2 .
http://www.bom.gov.au/sa/observations/adelaidemap.shtml
Need to resolve onto ≈ 4 km grid:
6.2 QR factorisation
Exceptionally tricky problems for LU factorisation Sometimes
LU factorisation does not work usefully.
1 0 0 0 0 0 0 0 0 1
-1 1 0 0 0 0 0 0 0 1
-1 -1 1 0 0 0 0 0 0 1
-1 -1 -1 1 0 0 0 0 0 1
-1 -1 -1 -1 1 0 0 0 0 1
-1 -1 -1 -1 -1 1 0 0 0 1
-1 -1 -1 -1 -1 -1 1 0 0 1
-1 -1 -1 -1 -1 -1 -1 1 0 1
-1 -1 -1 -1 -1 -1 -1 -1 1 1
-1 -1 -1 -1 -1 -1 -1 -1 -1 1
Inverse method:
timeInv =
0.0001
relErrorInv =
1.4560e-16
LU decomposition, same as A\ :
timeLU =
0.0414
relErrorLU =
9.7798e-15
Example 6.10.
>> compareMethods
n =
150
Inverse method:
timeInv =
0.0006
relErrorInv =
5.2904e+03
LU decomposition, same as A\ :
> In compareMethods (line 25)
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 1.401298e-45.
timeLU =
0.0008
relErrorLU =
0.7713
QR decomposition:
timeQR =
0.0012
relErrorQR =
5.8472e-14
Orthogonal matrices
Definition 6.11. An orthogonal matrix Q is a square matrix whose
transpose is its inverse, that is,
QT = Q−1 and QQT = QT Q = I.
QR factorisation
Definition 6.13. A QR factorisation of an m × n matrix A is
A = QR, where Q is an m × m orthogonal matrix and R is an
m × n upper triangular matrix.
3 1
Example 6.14. A QR factorisation of A = is
−4 2
3/5 4/5 5 −1
A = QR =
−4/5 3/5 0 2
u · v = uT v = u1 v1 + u2 v2 + · · · + un vn .
u·v uT v
cos θ = =
kuk kvk kuk kvk
3.5
2.5
2
p
1.5
0.5
0
0 5 10 15 20
t
5a + b − 2.4 ,
10a + b − 1.4 ,
15a + b − 0.7 ,
r = Ax − b = QRx − b
QT r = QT QRx − QT b = Rx − QT b
0 0.6547
0 0
>> j = find(abs(diag(R)) > 1e-8)
j =
1
2
>> x = R(j,:)\(Q(:,j)’*b)
x =
-0.1700
3.2000
Matlab’s \ operator (usually) finds the least squares solution
automatically, without informing you (Quarteroni et al. 2014, §5.8):
>> x = A\b
x =
-0.1700
3.2000
Matrix norm
kAxk
kAk = max = max kAxk.
x6=0 kxk kxk=1
Consequently:
• kAk ≥ 0 ;
• kAmk = 0 if and only if A is all zero;
• *** kAxk ≤ kAk kxk for all x; and
• kAxk = kAk kxk for at least one x 6= 0 .
1 0
Example 6.21. • The matrix A = has norm kAk2 =
1 1
√
(3 + 5)/2.
• Every orthogonal matrix Q has norm kQk = 1.
Condition number
Definition 6.22. (Kreyszig 2011, §20.4) The condition number of
a square matrix A is
Error bounds
kek krk
≤ cond(A)
kxk kbk
1 ≤ good < 102 < poor < 104 < bad < 108 < terrible.
Jacobi iteration
10x1 − x2 + 2x3 = 6 ,
−x1 + 11x2 − x3 + 3x4 = 25 ,
2x1 − x2 + 10x3 − x4 = −11 ,
3x2 − x3 + 8x4 = 15 .
Ax = b
Dx + (A − D)x = b
Dx = (D − A)x + b
0 3 −1 8 15
We write
10 0 0 0 0 −1 2 0
0 11 0 0 −1 0 −1 3
D=
0 0 10 0 and A − D =
2 −1 0 −1
0 0 0 8 0 3 −1 0
Diagonal dominance
Definition 6.28. An n × n matrix A is diagonally dominant if the
diagonal elements in each row of A are larger in magnitude than
the sum of the absolute values of the off-diagonal elements: that is,
if
n
X
|Aii | > |Aij |, i = 1, . . . , n.
j=1
j6=i
0 3 −1 8
is diagonally dominant (check the definition holds).
Convergence
Theorem 6.30. If the matrix A is diagonally dominant, then
Jacobi iteration converges to the solution of Ax = b (Quarteroni
et al. 2014, Prop. 5.3).
Proof overview
(k)
• Define the overall error (k) = maxi |xi − xi |.
• Then (k) ≤ G(k−1) where here
n
1 X
G = max |Aij |.
i |Aii |
j=1, j6=i
7 Nonlinear equations
Nonlinear equations Given some function f (x), we explore the
problem of finding solutions x of
f (x) = 0 .
xk+1 = g(xk ), k = 0, 1, . . .
1 1 x2 − 1
x = (x2 + 1), or x = 3 − , or x = ,
3 x 2x − 3
where mysteriously to get the last, add x2 − 1 to both sides then
divide by 2x − 3. These three yield the three iteration formulae,
respectively,
1 1 x2k − 1
xk+1 = (x2k + 1) or xk+1 = 3 − or xk+1 = .
3 xk 2xk − 3
x2k − 3xk + 1
xk+1 = xk −
2xk − 3
2
x −1
= ··· = k .
2xk − 3
Convergence
Theorem 7.5. If f (x) is twice continuously differentiable, and
f 0 is not zero at a root s of f (x) = 0, then for initial x0 sufficiently
close to s, the rate of convergence of Newton’s method is quadratic,
where ‘quadratic’ means that errork ≤ M error2k−1 (Quarteroni et al.
2014, Prop. 2.2) (Kreyszig 2011, §19.2, Thm. 2).
f (x + h) − f (x)
f 0 (x) ≈ ,
h
f (x, y) = 0 ,
g(x, y) = 0 .
f (x, y) = x2 − xy 2 − xy − 1 = 0 ,
g(x, y) = y 2 + x3 + xy − 3 = 0 .
Try guessing (x, y) = (1, 0). Then f (1, 0) = 0 and g(1, 0) = −2.
This is not a solution, because a solution must satisfy both equa-
tions.
Try to find a better solution by modifying the original guess as
(x, y) = (1 + ∆x, ∆y), where ∆x and ∆y are notionally small.
Ignoring the very much smaller product terms ∆x2 , ∆y 2 , ∆x∆y,
and so on, we obtain
Solving the linear system yields (∆x, ∆y) = (2/5, 4/5). The next
approximation is thus (x1 , y1 ) = (1 + ∆x, ∆y) = (7/5, 4/5).
We could continue this process by searching for a next better
approximation (x2 , y2 ) = (x1 + ∆x0 , y1 + ∆y 0 ), then (x3 , y3 ) =
(x2 + ∆x00 , y2 + ∆y 00 ), and so on. But to do so we need to find the
linear equations systematically.
Now the approximation process leading to f (1 + ∆x, ∆y) ≈ 2∆x −
∆y + 0, g(1 + ∆x, ∆y) ≈ 3∆x + ∆y − 2 gives a tangent plane
xk+1 = xk + ∆x, Jk ∆x = −r k .
xk+1 = xk − Jk−1 r k ,
xk+1 = xk + ∆x, Jk ∆x = −r k .
∂fi
Jij = .
∂xj
f (zk ) zk4 − 1
zk+1 = zk − = z k −
f 0 (zk ) 4zk3
Starting points near 1 converge to the root 1 + 0i, and similarly for
the other roots.
What happens at the boundaries between regions?
Points are coloured by the root which they eventually converge to.
The boundaries dividing basins of attraction for each z0 turn out
to be fascinating! Other fractals are generated by other f (z).
-1
-0.5
0.5
1
-1 -0.5 0 0.5 1
dy
= y 0 = f (t, y),
dt
subject to the initial condition y(t0 ) = y0 (Quarteroni et al. 2014,
§8.2) (Kreyszig 2011, §21.1).
T (0) = T0 .
T (h) − T0
≈ −T0 =⇒ T (h) ≈ T0 − T0 h = (1 − h)T0 .
h
dT Tk+1 − Tk
≈ ≈ −Tk =⇒ Tk+1 = (1 − h)Tk .
dt t=tk h
Tk = (1 − h)k T0 .
h2 h3
−h
T (h) = T0 e = T0 1−h+ − + ··· .
2! 3!
yk+1 = yk + hf (tk , yk ).
This is an explicit formula because all terms on the right hand side
are known from the previous time step.
Theorem 8.2. Euler’s method estimates y(t) to an error of O(h)
as h → 0, provided that |y 00 | ≤ M .
dy
= y 0 = f (t, y), y, f ∈ Rm ,
dt
subject to the initial condition y(t0 ) = y 0 (Quarteroni et al. 2014,
§8.9) (Kreyszig 2011, §21.3).
dx
= −x3 + ax − b,
dt
db
= x − xa ,
dt
where x(t) is the time-dependent muscle fiber length, b(t) is an
electrochemical control that stimulates the muscles, and , a and xa
are constants.
See code euler.m and testeuler.m
d2 θ g
2
+ sin θ = 0,
dt L
where g is the acceleration due to gravity and L is the length of the
pendulum.
For numerical solution we transform this into a system of two
first-order odes by defining y1 (t) = θ(t) and y2 (t) = θ0 (t). Then
0
y1 y2
= .
y20 − Lg sin y1
y k+1 = y k + hf (tk , y k ).
h2 00
y(t + h) = y(t) + hy 0 (t) + y (t) + O(h3 ).
2!
Replace the derivatives y 0 (t) and y 00 (t) using the ode and its first
derivative
Hence
h
f (t + h, y + hf ) + f + O(h3 ).
y(t + h) = y(t) +
2
yk+1 = yk + 21 F1 + F2 ,
F1 = hf (tk , yk ),
F2 = hf (tk + h, yk + F1 ).
F1 = −hT0 ,
F2 = −h(T0 − hT0 ) = (h2 − h)T0 ,
T1 = T0 + 21 − hT0 − h(T0 − hT0 )] = (1 − h + 21 h2 )T0 .
Tk = (1 − h + 12 h2 )k T0 .
h2 h3
−h
T (h) = T0 e = T0 1 − h + − + ··· .
2! 3!
Example 8.8. In the cases where the right-hand side f (t, y) does
not explicitly depend on y, then
Z tk+1
dy
= f (t) =⇒ y(tk+1 ) = y(tk ) + f (t) dt.
dt tk
1
y k+1 = y k + F 1 + 2F 2 + 2F 3 + F 4 ,
6
F 1 = hf (tk , y k ),
F 2 = hf (tk + 12 h, y k + 12 F 1 ),
F 3 = hf (tk + 21 h, y k + 12 F 2 , )
F 4 = hf (tk + h, y k + F 3 ).
dy
= ty 2 subject to y(1) = 2.
dt
One step of the fourth-order Runge–Kutta scheme using h = 0.1
gives (you do the details)
Example 8.11. Consider the ode y 0 = −y, for which the Euler
method is yk+1 = yk − hk yk , but now where hk is an adaptively
chosen step size.
Since the error inn Euler method is ∝ |y 00 |we try choosing hk =
c/|y 00 |, where c is a constant. Then pleasingly the time-step hk is
large when (curvature) |y 00 | is small, and vice versa.
However, in this example we know y 00 = −y 0 = +y and so hk =
c/|yk |. Consequently, Euler method becomes
c
yk+1 = yk − c sign(yk ) and tk+1 = tk + .
|yk |
dx
= −x3 + ax − b,
dt
db
= x − xa ,
dt
with parameters = 0.2, a = 1.6 and xa = 0.7, and initial values
x(0) = 1 and b(0) = 0.5.
See heart.m on MyUni. Notice the nonuniform time steps that are
used.
Stiff systems
y 0 = −λy, y(0) = y0 ,
yk = (1 − λh)k y0 .
u0 = −100u
v 0 = −v
yk+1 − yk
≈ f (tk+1 , yk+1 ),
h
• shooting, and
Shooting
f (x) = y1 (π/4) − 1 = 0,
One nice aspect is that we can make use of the existing computa-
tional infrastructure that we have developed, including Newton and
Runge–Kutta methods.
One disadvantage is that for many guessed initial values the solution
gets enormously large at the other boundary, which tends to ruin
Newton’s method.
Example 8.18. For the ode of Example 8.16 we seek u(t). Con-
struct a grid of points in time t:
π/4
tj = jh, h= , j = 0, . . . , n.
n
Then let uj = u(tj ). Earlier in the course (Section 4.3) we showed
that
uj−1 − 2uj + uj+1
u00j = u00 (tj ) = + O(h2 ).
h2
Substituting this into the differential equation and ignoring the O(h2 )
error term gives
uj−1 + (4h2 − 2)uj + uj+1 = 0, j = 1, . . . , n − 1.
At the boundaries
u0 = u(0) = 0 and un = u(π/4) = 1 .
Applying these to the finite difference equation at j = 1 and j = n−1
yields
(4h2 − 2)u1 + u2 = −u0 = 0 ,
un−2 + (4h2 − 2)un−1 = −un = −1 .
Heat equation
Example 8.19. Consider an insulated rod of unit length. Let
u(x, t) denote the temperature at position x and time t and sup-
pose the temperature at each end is zero (relative to the ambient
temperature). The temperature in the rod is governed by the heat
(diffusion) equation
∂u ∂2u
= α 2, 0 < x < 1, t > 0,
∂t ∂x
subject to
u(x, 0) = f (x), u(0, t) = 0, u(1, t) = 0,
where f (x) is the initial temperature distribution in the rod.
∂u k uk+1
j − ukj
= + O(∆t),
∂t j ∆t
∂2u k ukj−1 − 2ukj + ukj+1
= + O(∆x2 ).
∂x2 j ∆x2
Substituting these into the heat equation and ignoring the O(∆t)
and O(∆x2 ) error terms gives
α∆t
uk+1
j = (1 − 2s)ukj + s(ukj−1 + ukj+1 ), s= . (8.1)
∆x2
From the boundary conditions, uk0 = 0 and ukn = 0. Starting from
the initial condition
u0j = f (xj ),
we can use (8.1) to calculate ukj for j = 1, . . . , n − 1 and k = 1, 2, . . . .
This and other methods for solving partial differential equations
are covered in Level III maths.
Xk = (aXk−1 + c) mod m, k = 1, 2, . . . .
N
b−aX
IN = g(Xi ).
N
i=1
Rπ
Figure 2: error in Monte Carlo estimates of 0 sin x dx for various N .
The reference line is N −1/2 .
10 0
10 -1
Error
10 -2
10 -3
10 -4
10 2 10 3 10 4 10 5
N
Rπ
Example 9.4. Estimate I = 0 sin x dx using Monte Carlo inte-
gration. See mcsin.m (could also use mean()):
N I_mc e_mc e_mid
-----------------------------
100 1.9217 7.83e-02 8.22e-05
200 1.8987 1.01e-01 2.06e-05
400 1.9736 2.64e-02 5.14e-06
800 2.0025 2.54e-03 1.29e-06
1600 1.9915 8.54e-03 3.21e-07
-----------------------------
9.3 Multidimensional integrals
Multidimensional integrals Monte Carlo integration extends to
multi-dimensional integrals. For example, the mean/average of g(x, y)
over a rectangle [a, b] × [c, d] is
RdRb N
c g(x, y) dx dy
a 1 X
≈ g(Xi , Yi )
(d − c)(b − a) N
i=1
Z dZ b N
(d − c)(b − a) X
=⇒ g(x, y) dx dy ≈ g(Xi , Yi )
c a N
i=1
An implementation is
% first 2D MC example in notes, Oct 2018
N = 1e6
x = 2 + 3*rand(N,1);
y = 1 + 3*rand(N,1);
g = x.^2 - y.^2 + x.*y - 3;
I = 9/N*sum(g)
0 ≤ x ≤ 1, 10 ≤ y ≤ 13, y ≥ 12 cos x, y ≥ 10 + x3 .
Comparison
R 1 R 1 with midpoint integration Consider the integral
I = 0 0 f (x, y) dx dy. A midpoint rule approximation of this
integral is
Xn Xn
2
IN = h f (xi , yj ),
i=1 j=1
1
0.8 p(x)
0.6
0.4
0.2 x
−0.5 0.5 1 1.5
The variance of Y is
A Summary
A.1 Introduction
Empty.
A.2 Matlab
Matlab
A.3 Interpolation
Interpolation