Você está na página 1de 179

Numerical Methods

Module: EG1C2 Engineering Mathematics


Lecturer: James Grimbleby
URL: http://www.elec.rdg.ac.uk/jbg.html
email: j.b.grimbleby reading.ac.uk
Number of Lectures: 10
Recommended text book:
Modern Engineering Mathematics (3rd edition)
Glyn James
Prentice-Hall 2001
ISBN 0-13-018319-9
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 1

Numerical Methods

Modern Engineering
Mathematics (3rd edition)
Glyn James
Prentice-Hall 2001
ISBN 0-13-018319-9
Approx. price: 25

James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 2

Syllabus
Numerical precision, absolute and relative errors, binary
codes, integer and floating-point codes, IEEE floating-point
representations, rounding and truncation errors in arithmetic
operations, underflow and overflow
Real zeros of non-linear equations by repeated bisection,
secant method, iteration method, Newton-Raphson method,
factorisation of polynomials, synthetic division
Curve fitting, linear and quadratic interpolation, Lagrange
polynomials, exact polynomial fit through equally-spaced
points, numerical differentiation, polynomial least-squares fit
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 3

Syllabus
Numerical integration, the trapezium approximation, improper
integrals, midpoint approximation, changes of variable to deal
with infinite integration limits
Function minimisation, single-variable approximation methods,
single-variable search methods, Golden-Section search
Numerical solution of first-order non-linear differential
equations; Euler's method, 2nd and 4th order Runge-Kutta
methods
Numerical solution of higher-order differential equations,
reduction to a set of 1st-order differential equations
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 4

Numerical Methods
Engineering calculations often involve quantities whose
values can vary over a wide range
Frequencies:

1 Hz 10 GHz

Mass:

g many tonnes

Distance:

m thousands of km

Power:

nW MW

Capacitance:

1pF 1F

The range is further increased when such quantities are


multiplied or divided by one another
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 5

Precision
Relative precision: the precision of a number relative to its
magnitude
1002, 10.02 and 0.0010.00002 all have a relative
precision of 0.02 or 2%
Absolute precision: the absolute precisions of these values
are respectively 2, 0.02 and 0.00002.
In most cases it is the relative precision, rather than the
absolute precision, that is of importance

James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 6

Binary Codes
Numbers are represented in a computer by means of
binary codes
Each number is stored and processed in the form of a
group of binary digits (bits) known as a word
A binary word consisting of n bits can represent 2n distinct
values
Natural binary code: n-bit word represents the integer
values 0 to 2n-1
Signed binary code: n-bit word represents the integer
values -2n-1 to 2n-1-1
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 7

Floating-Point Codes
Binary floating-point codes can represent very large and
very small values
Floating-point numbers consist of two fields, the mantissa
M and the exponent E
X = M 2E
The relative precision of a floating-point number is
determined by the number of bits in the mantissa M
The range is determined by the number of bits in the
exponent E
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 8

IEEE Floating-Point Formats


The IEEE standard defines the format of single (32 bit),
double (64 bit) and extended (80 bit) precision floating-point
numbers
Double precision (64 bit):
6362 .52 51.0
1 bit sign

11 bit exponent

52 bit mantissa

Mantissa is 53-bit unsigned fixed-point binary


Exponent is 11 bit excess 1023 binary
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 9

IEEE Floating-Point Formats


Mantissa 52-bit + hidden bit:
1.00000 .. 00000
1.11111 .. 11111
Hidden bit
(always 1)

= 1 (decimal)
2 (decimal)

52 bits of
IEEE format

A change in the least-significant bit is equal to:


2-52 = 2.210-16
so that IEEE 64-bit numbers have a relative precision of 1516 decimal digits
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 10

IEEE Floating-Point Formats


Exponent 11-bit excess 1023:
1 - 1023
=
2046 - 1023 =

-1022
+1023

Floating-point range:
1 2-1022
2 2+1023

=
=

2.3 10-308
1.7 10+308

The special case of 11-bit exponent all 0s is used to


represent the floating-point value 0.0
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 11

Truncation and Rounding Errors


A 32-bit word can represent 4294967296 values
Real quantities cannot be represented exactly and the
nearest discrete value of a binary word is used
For example the decimal value 1.2 has a binary
representation:
1.00110011001100110011001100110011 ...
Since real values must be stored in a binary word the number
of bits must be limited and there are two ways of doing this:
Truncation or Rounding
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 12

Truncation
Truncation is the act of simply discarding unwanted digits
from a number
Decimal value 1.2 has a binary representation:
1.00110011001100110011001100110011 ...
This exact binary representation could be truncated to 8 bits:
1.0011001
This has decimal value 1.1953125, an absolute error of
0.0046875 or relative error of 0.39%
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 13

Rounding
Rounding involves determining the nearest discrete value of
the appropriate word
Decimal value 1.2 has a binary representation:
1.00110011001100110011001100110011 ...
This exact binary representation could be rounded to 8 bits:
1.0011010
This has decimal value 1.203125 , an absolute error of
0.003125 or relative error of 0.26%
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 14

Errors Generated by Addition


When a small number is added to a larger number a rounding
error related to the precision of the larger number occurs
If this procedure is repeated a large number of times then the
effects of the rounding errors can become quite significant
When a value of 1.0 10-15 is added to 1.0 a million times
using IEEE double precision numbers:
1.00000000111022
In spite of the fact that the operations are performed using 15
digit precision, the result is accurate only to 9 digits.
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 15

Errors Generated by Subtraction


When numbers are subtracted the absolute precision of the
result is equal to the absolute precision of the subtracted
quantities
If the result is smaller in magnitude than the subtracted
quantities then it will have a reduced relative precision
When 1000000.0 is subtracted from 1000000.1 using IEEE
double precision numbers:
0.0999999999767169
Six digits of accuracy have been lost.
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 16

Errors Generated by Subtraction


Consider evaluation of the simple formula:
X = ( A + B) B

Obviously the answer should be A


If B is larger in magnitude than A then A will suffer a loss in
relative precision.
Evaluating the formula with A = 0.1 and B =1.0 1015 using
IEEE double precision real numbers:
0.125000000000000
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 17

Errors Generated by Subtraction


Subtractions of nearly equal quantities are implicit in many
engineering calculations
For example if A is small then:
X = e A 1+ A
Adding 1 to A limits the information about A in the result X
If A = 1.010-12 then the value of ln(X) = A evaluated using
IEEE double precision numbers:
1.00008890058184110-12
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 18

Simultaneous Linear Equations

y = a1x + b1

y = a2 x + b2
Solution:

y = a1x + b1

y = a2 x + b2

y = a1x + b1 = a2 x + b2
x(a1 a2 ) = b2 b1
b2 b1
x=
a1 a2

James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 19

Roots of a Quadratic Equation


ax 2 + bx + c = 0
As is well known the roots of this equation are given by the
formula:
b b 2 4ac
x=
2a
Quadratic equation with roots at 100.0 and 0.01:
x 2 100.01x + 1.0 = 0

Using the quadratic formula gives the lower root:

100.01 10002.0001 4.0 100.01 99.99


x=
=
= 0.01
2.0
2.0
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 20

Roots of a Quadratic Equation


Subtraction of nearly-equal quantities only happens when the
roots are real, that is b24ac
For real roots an alternative formula should be used:

q = b + sgn(b). b 2 4ac
q
x1 =
2a
2c
x2 =
q
It is safe to use the normal quadratic formula if the roots are
complex, that is b2<4ac
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 21

Roots of a Quadratic Equation


Quadratic equation:
x 2 100.01x + 1.0 = 0

For this equation b24ac so that the roots are:


q = 100.01 10002.0001 4.0 = 100.01 99.99 = 200.0
200
x1 =
= 100.0
2.0
2.0
x2 =
= 0.01
200.0

At no point were nearly-equal quantities subtracted


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 22

Unstable Algorithms
Often several algorithms are available to perform some
operation
Examples: solving simultaneous linear equations, curve fitting
and solving differential equations
All of the algorithms will be mathematically correct and will
give the same results if executed on a computer that does not
use rounding or truncation
On a real computer they will not necessarily produce results
of the same precision
Some algorithms are notoriously unstable
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 23

Unstable Algorithms
Consider raising to an integer power, where:
5 1
=
2

is related to the Golden Mean or Golden Section k:


1
= k 1 = = 0.618034
k

The n+1 th power of can be obtained from the nth and n-1 th:

n +1

(
=

5 1 n 1 5 2 5 + 1 n 1
5 1 n 1
n 1

4
4
2

= n 1 n
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 24

Unstable Algorithms
n
n (by multiplication)
n (by algorithm)
1
6.180339887498948810-1
6.180339887498948810-1
2
3.819660112501052010-1
3.819660112501051210-1
3
2.360679774997897610-1
2.360679774997898010-1
4
1.458980337503155210-1
1.458980337503153010-1
5
9.016994374947428810-2
9.016994374947451210-2
6
5.572809000084124810-2
5.572809000084077610-2
. . . . . . . . . . . . . . . . . . . . . . . . . . .
32
2.053031023146580010-7
2.051847740602852410-7
33
1.268842952262558610-7
1.270757543636591410-7
34
7.841880708840216010-8
7.810901969662609610-8
35
4.846548813785372010-8
4.896673466703304810-8
36
2.995331895054845210-8
2.914228502959304010-8
37
1.851216918730527610-8
1.982444963744001210-8
38
1.144114976324318010-8
9.317835392153028810-9
39
7.071019424062098410-9
1.050661424528698210-8
40
4.370130339181083210-9 -1.188778853133953810-9
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 25

Overflow and Underflow


IEEE double precision floating point numbers cover the range
of magnitudes from 2.310-308 to 1.710+308
IEEE single precision floating point numbers cover the range
of magnitudes from 1.210-38 to 3.410+38
Both single and double precision numbers also allow a value
of zero
Overflow occurs when a floating point number exceeds in
magnitude the largest value that can be represented
Underflow occurs when a floating point number is smaller in
magnitude than the smallest value that can be represented
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 26

Overflow and Underflow


A complex number x+jy has a modulus m:

m = x2 + y 2
Evaluating this the obvious way limits the values of x and y to
of the available floating-point range
To avoid this the modulus can be evaluated in an alternative
way:
2
y
x y
m = x 1+
x
x
m = y 1+
y
James Grimbleby

x< y

School of Systems Engineering - Electronic Engineering

Slide 27

Equations in a Single Variable


There are many problems in engineering that require solution
of an equation of the form:
f ( x) = 0

In most cases the equation cannot be solved algebraically and


numerical methods must be used
For example consider the simple electronic circuit:
i
1000
2.0V

v1
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 28

Equations in a Single Variable


Ohm's law:

2.0 v1
i=
1000.0

Shockley equation for the diode:


i = 1.0 10

14

1000
2.0V

v1
exp
0.026

v1

Eliminating the current i:


f (v1) = 1.0 10

14

v1
2.0 v1
exp

= 0
0.026 1000.0

This equation cannot be solved algebraically


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 29

Graphical Solution
i
2mA

i = 1.0 10

14

v1
exp
0.026

2.0 v1
i=
1000.0

2V
James Grimbleby

School of Systems Engineering - Electronic Engineering

vi
Slide 30

Repeated Bisection
f(x)

c
a+b
c=
2

bx

If f(a) and f(b)


have opposite
signs then a root
exists between a
and b
Bisect the
interval a to b, to
get a new point c

Also known as Interval Halving or Binary Search


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 31

Repeated Bisection
Repeated Bisection algorithm:
If the initial interval is a0 to b0, with midpoint c0, and the
interval for the next iteration is a1 to b1, then :

c0 = (a0+b0)/2
If f(a0)f(c0) < 0 then:
otherwise:

a1 = a0, b1 = c0
a1 = c0, b1 = b0

A new midpoint c1 is obtained by bisecting the interval a1 to


b1, the function evaluated at this midpoint, and the process
repeated until bn-an < required accuracy
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 32

Repeated Bisection
a
0.000000
0.000000
0.500000
0.500000
0.625000
0.625000
0.656250
0.656250
0.664063
0.664063
0.666016
0.666016
0.666016
. . . . .
0.666031
0.666031
James Grimbleby

c
1.000000
0.500000
0.750000
0.625000
0.687500
0.656250
0.671875
0.664063
0.667969
0.666016
0.666992
0.666504
0.666260
. . . . .
0.666033
0.666032

b
2.000000
1.000000
1.000000
0.750000
0.750000
0.687500
0.687500
0.671875
0.671875
0.667969
0.667969
0.666992
0.666504
. . . . .
0.666035
0.666033

fa
-2.0e-3
-2.0e-3
-1.5e-3
-1.5e-3
-1.1e-3
-1.1e-3
-4.3e-4
-4.3e-4
-9.9e-5
-9.9e-5
-8.3e-7
-8.3e-7
-8.3e-7
. . . .
-3.0e-8
-3.0e-8

fc
fb
5.1e+2 2.6e+9
-1.5e-3 5.1e+2
3.2e-2 5.1e+2
-1.1e-3 3.2e-2
1.7e-3 3.2e-2
-4.3e-4 1.7e-3
3.4e-4 1.7e-3
-9.9e-5 3.4e-4
1.1e-4 3.4e-4
-8.3e-7 1.1e-4
5.1e-5 1.1e-4
2.5e-5 5.1e-5
1.2e-5 2.5e-5
. . . . . . .
7.0e-8 1.7e-7
2.0e-8 7.0e-8

School of Systems Engineering - Electronic Engineering

Slide 33

Repeated Bisection
The repeated bisection method is very robust and its rate of
convergence (a factor of 2 per iteration) is fixed
After n iterations the size of the interval (bn-an) is given by:
b0 a0
bn an =
2n

If the required accuracy is then the number of iterations n


required is given by:
b0 a0
log10
b0 a0

n = log2
=

log10 2
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 34

Repeated Bisection
f(x)

f(x)

x
x
1
f ( x) =
x2
James Grimbleby

Close or coincident roots

School of Systems Engineering - Electronic Engineering

Slide 35

Secant Method
Two points a and b on the function f(x) are used to estimate
the position c of the root by linear interpolation
f(x)
f (b) f (a) 0 f (a)
=
slope =
ba
c a
(b a )
c a = f (a)
f (b) f (a)

f(b)
a

f(a)

(b a)
c = a f (a)
f (b) f (a)

Also known as Method of False Position


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 36

Secant Method
It is not necessary for the root to lie between the points a
and b:

f(x)
f (b) f (a) f (a)
slope =
=
ba
ac

f(b)
c

f(a)
a

James Grimbleby

(b a)
a c = f (a)
f (b) f (a)

(b a)
c = a f (a)
f (b) f (a)

School of Systems Engineering - Electronic Engineering

Slide 37

Secant Method
The process is now repeated with a1=b0 and b1=c0 to get an
even better approximation c1:

f(x)

f(a)
bc
f(b)

James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 38

Secant Method
a
0.700000
1.000000
0.699998
0.699996
0.680959
0.673203
0.667802
0.666258
0.666039

b
1.000000
0.699998
0.699996
0.680959
0.673203
0.667802
0.666258
0.666039
0.666031

f(a)
3.626510-3
5.053910+2
3.626110-3
3.625710-3
1.049510-3
4.308910-4
9.574810-5
1.191410-5
3.913810-7

f(b)
5.053910+2
3.626110-3
3.625710-3
1.049510-3
4.308910-4
9.574810-5
1.191410-5
3.913810-7
1.671810-9

c
0.699998
0.699996
0.680959
0.673203
0.667802
0.666258
0.666039
0.666031
0.666031

Close to root the number of significant figures increases by a


factor of 1.618 per iteration
Secant method may be slow and unreliable when operating
far from a root
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 39

Iteration Method
The Iteration method is based on manipulating the equation
into the form:
x = g (x )
Given an approximation xn to the root, a better approximation
xn+1 is found by using the formula:

xn +1 = g ( xn )
The iteration method can often converge to a root very quickly
Unfortunately the iteration method does not guarantee
convergence
Nevertheless, because of its simplicity it is a valuable method
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 40

Convergence of the Iteration


Method
y

y=x

y=x

y = g (x )

x0

x2

x1

Convergent
James Grimbleby

x
x = g( x)

y = g( x)

x1

x0 x2

Divergent

School of Systems Engineering - Electronic Engineering

Slide 41

Convergence of the Iteration


Method
The condition for the iteration method to converge is that the
magnitude of the slope of g(x) must be less than unity:
g (x ) < 1

Diode circuit:
1.0 10

14

2.0 v1
v1
exp

= 0
0.026 1000.0

which can be written:


v1 = 2.0 1.0 10

11

v1
exp
0.026

or in a form suitable for the iteration method:


x
11
exp
g ( x ) = 2.0 1.0 10
0.026
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 42

Convergence of the Iteration


Method
For a starting value of x0=1.0:
g ( x0 ) = 3.9 10

10

1 .0
exp
= 1.9 107
0.026

Clearly this does not satisfy the condition for convergence


x
1.00000010+00
-5.05396510+05
2.00000010+00

James Grimbleby

g(x)
-5.05396510+05
2.00000010+00
-2.55427610+22

School of Systems Engineering - Electronic Engineering

Slide 43

Convergence of the Iteration


Method
The equation can be rearranged:
2.0 v1

v1
= exp
0.026
1.0 10 11

so that:

v1
2.0 v1
or : ln
=

1.0 10 11 0.026

2.0 v1
v1 = 0.026 ln

11
1.0 10

or in a form suitable for the iteration method:


2.0 x
g ( x ) = 0.026 ln

11

1.0 10

= 0.026 ln(2.0 x ) 0.026 ln 1.0 10 11


James Grimbleby

School of Systems Engineering - Electronic Engineering

)
Slide 44

Iteration Method
From a starting point of x0=1.0:
g ( x0 ) =

0.026
0.026
=
= 0.026
2.0 x
2.0 1

This satisfies the condition for convergence


x
1.000000
0.658539
0.666177
0.666029
0.666032
0.666031
James Grimbleby

g(x)
0.658539
0.666177
0.666029
0.666032
0.666031
0.666031

School of Systems Engineering - Electronic Engineering

Slide 45

Newton-Raphson Method
The Newton-Raphson method can in principle be applied to
any equation of the form:
f ( x) = 0

In practice it is only really suitable for functions whose


derivatives can be obtained analytically
All polynomial equations:
f ( x ) = a0 + a1x + a2 x 2 + a3 x 3 + ... + an x n

have this property and Newton-Raphson method is the


preferred technique for solving such equations
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 46

Newton-Raphson Method
Suppose that x0 is initial estimate of root
Expand function as a Taylor series around this point:
h2
f ( x0 + h) = f ( x0 ) + hf ' ( x0 ) +
f " ( x0 ) + ...
2!

If x0+h is a root of the equation:


f ( x0 + h ) = 0

Provided that x0 is close to the root, then h will be small, and:


f ( x0 ) + hf ' ( x0 ) = 0
James Grimbleby

f ( x0 )
or : h =
f ' ( x0 )

School of Systems Engineering - Electronic Engineering

Slide 47

Newton-Raphson Method
f ( x0 )
h=
f ' ( x0 )

This formula will not give distance h to root exactly because of


approximation involved
However x1 should be a better approximation to root where:
f ( x0 )
x1 = x0
f ' ( x0 )

Newton-Raphson iteration formula:


f ( xn )
xn +1 = xn
f ' ( xn )
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 48

Newton-Raphson Method

f(x)

f(x0)

x2

x1

f ( x0 )
f ' ( x0 ) = slope =
x0 x1
James Grimbleby

x0
or :

f ( x0 )
x1 = x0
f ' ( x0 )

School of Systems Engineering - Electronic Engineering

Slide 49

Newton-Raphson Method
The Newton-Raphson method requires the derivative of the
function to be evaluated
In the case of the diode circuit the derivative can be obtained
in algebraic form:
f ( x ) = 1.0 10

14

2.0 x
x
exp

0.026 1000.0

1.0 10 14
1.0
x
exp
+
f ' ( x) =
0.026
0.026 1000.0

The Newton-Raphson method can now be used with a


starting point of 1.0
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 50

Newton-Raphson Method
xn
1.000000
0.974000
0.948000
0.922001
0.896002
0.870005
0.844014
0.818038
0.792106
0.766295
0.740814
0.716224
0.693917
0.676741
0.667939
0.666099
0.666032
James Grimbleby

f(xn)
5.0539710+2
1.8592510+2
6.8397710+1
2.5161810+1
9.2562210+0
3.4048610+0
1.2522610+0
4.6036110-1
1.6902910-1
6.1848510-2
2.2415310-2
7.9109110-3
2.5927310-3
6.9064110-4
1.0342910-4
3.5114110-6
4.4317210-9

f'(xn)
1.9438410+4
7.1510010+3
2.6307210+3
9.6780510+2
3.5605210+2
1.3100110+2
4.8209510+1
1.7752610+1
6.5485810+0
2.4272410+0
9.1155810-1
3.5464210-1
1.5095410-1
7.8457710-2
5.6211210-2
5.2439010-2
5.2306610-2

xn+1
0.974000
0.948000
0.922001
0.896002
0.870005
0.844014
0.818038
0.792106
0.766295
0.740814
0.716224
0.693917
0.676741
0.667939
0.666099
0.666032
0.666031

School of Systems Engineering - Electronic Engineering

Slide 51

Newton-Raphson Method
Newton-Raphson method may fail where two or more of the
roots are coincident
f(x)

Zero
function
Zero derivative
James Grimbleby

School of Systems Engineering - Electronic Engineering

x
Slide 52

Newton-Raphson Method
Although Newton-Raphson works well in most cases there is
no guarantee that it will converge:
f(x)

x3

x1
x0

James Grimbleby

x2

School of Systems Engineering - Electronic Engineering

Slide 53

Polynomial Equations
The Newton-Raphson method is particularly suitable for
polynomial equations because their derivatives can always be
obtained analytically:
2

i =n

f ( x ) = a0 + a1x + a2 x + ... + an x = ai x i
i =0

f ' ( x ) = a1 + 2a2 x + 3a3 x + ... + nan x

n 1

i =n

= iai x i 1
i =1

In general polynomial equations have complex roots but he


discussion here will be limited to polynomials with real roots
Polynomials up to, and including quartics (n=4) can be solved
algebraically
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 54

Evaluation of Polynomials
The most efficient way of evaluating a polynomial is to use
Horner's rule
This can be derived by rearranging the polynomial into nested
form:
f ( x ) = a0 + a1x + a2 x 2 + ... + an 1x n 1 + an x n
= a0 + x(a1 + x(a2 + ... + x(an 1 + an x )..))

Only one multiplication and one addition is required for each


power of x
This method can be used for the polynomial and its derivative.
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 55

Polynomial Equations
Polynomial with roots: 10, 1, 1 and 0.1:
f ( x ) = 1 12.1x + 22.2 x 2 12.1x 3 + x 4

Estimate of smallest root (or simply use x0=1.0):


a0
1
x0 =
=
= 0.0826446
a1 12.1

Newton-Raphson:
xn
8.2644610-2
9.9339110-2
9.9999010-2
1.0000010-1
James Grimbleby

f(xn)
1.4484610-1
5.3078810-3
8.1191610-6
1.9098010-11

f'(xn)
-8.6762510+0
-8.0436410+0
-8.0190410+0
-8.0190010+0

xn+1
9.9339110-2
9.9999010-2
1.0000010-1
1.0000010-1

School of Systems Engineering - Electronic Engineering

Slide 56

Polynomial Deflation
Roots are removed from a polynomial by a procedure known
as deflation
Any polynomial can be written in the form of the product of n
factors:
f ( x ) = a0 + a1x + a2 x 2 + ... + an 1x n 1 + an x n
= an ( x z1)( x z2 )( x z3 )...( x zn )

Clearly the polynomial becomes zero when x equals any of the


zi so that zi are the roots of the polynomial equation
If zi is a root of a polynomial equation then (x-zi) must be a
factor of the polynomial
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 57

[G James p 88]

Polynomial Deflation
x3-12.0x2+21.0x -10

x-0.1 x4-12.1x3+22.2x2-12.1x+1
x4- 0.1x3

-12.0x3+22.2x2
-12.0x3+ 1.2x2

+21.0x2-12.1x
+21.0x2- 2.1x

-10.0x+1
-10.0x+1

The remainder from this polynomial division should always be


zero
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 58

Polynomial Equations
Deflated polynomial:
f ( x ) = x 3 12x 2 + 21x 10
Estimate of smallest root:

Newton-Raphson:

a0 10
x0 =
=
= 0.476190
a1 21

xn
f(xn)
f'(xn)
xn+1
0.476190
-2.613109
10.251701
0.731086
0.731086
-0.670281
5.057404
0.863621
. . . . . . . . . . . . . . . . . . . . . . . .
0.997837
-0.000042
0.038940
0.998919
0.998919
-0.000011
0.019469
0.999459
0.999459
-0.000003
0.009734
0.999730
0.999730
-0.000001
0.004867
0.999865
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 59

[G James p 88]

Polynomial Deflation
x2-11x +10

x-1 x3-12x2+21x-10
x3 -x2

-11x2+21x
-11x2+11x

10x-10
10x-10

The remaining roots can be found using the quadratic formula:


b b 2 4ac 11 121 40 11 9
=
=
x=
2a
2
2
x = 1 and x = 10
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 60

Cubic Equations
f ( x ) = a0 + a1x + a2 x 2 + a3 x 3

f(x)

f(x)
3 real roots

1 real root
x

Roots of a polynomial equation (with real coefficients) are


either real or occur in complex conjugate pairs
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 61

Cubic Equations
Example:

f ( x ) = x 3 6 x 2 + 13 x 20

Estimate of smallest root:

Newton-Raphson:
xn
1.538462
7.981116
6.042918
4.841269
4.215604
4.019009
4.000165
4.000000
James Grimbleby

a0 20
x0 =
=
= 1.53
a1 13

f(xn)
-10.559854
209.948095
60.125160
15.778298
3.091784
0.249297
0.002144
0.000000

f'(xn)
1.639053
108.321259
50.035556
25.218432
15.726702
13.229197
13.001979
13.000000

xn+1
7.981116
6.042918
4.841269
4.215604
4.019009
4.000165
4.000000
4.000000

School of Systems Engineering - Electronic Engineering

Slide 62

[G James p 88]

Cubic Equations
x2-2x +5

x-4 x3-6x2+13x-20
x3-4x2

-2x2+13x
-2x2 +8x

5x-20
5x-20

The remaining roots can be found using the quadratic formula:


b b 2 4ac 2 4 20 2 16
=
=
x=
2a
2
2
x = 1 2j
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 63

Curve Fitting
Curve fitting consists of obtaining a function f(x) which fits
experimental data points
This may be an exact fit if the number of experimental points
is small
Alternatively if the number of experimental points is large
then the function will normally be an approximate fit
It will be assumed in this course that the function is of
polynomial form and is fitted by adjusting the coefficients ai
f ( x ) = a0 + a1x + a2 x 2 + ... + an 1x n 1 + an x n
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 64

Uses of Curve Fitting


Once a function has been fitted to data it can be used to
predict values away from the data points:
y

yi

y
ye

y3
y2
y1

y3
y2
y1

x1

xi

x2

x3

Interpolation
James Grimbleby

x1

x2

x3

x
xe

Extrapolation

School of Systems Engineering - Electronic Engineering

Slide 65

Interpolation and Extrapolation


y
5th-order
Polynomial
Actual function
Data points
Actual
function
x
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 66

Interpolation and Extrapolation


y
5th-order
Polynomial
Actual
function

Data points

James Grimbleby

Actual
function

School of Systems Engineering - Electronic Engineering

x
Slide 67

Function Differentiation
Inverter Efficiency

Differentiate cubic
and set to zero to
obtain maximum

Cubic
Polynomial

Data points

Frequency
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 68

Function Integration
y

7th-Order
polynomial
fit to data

Data points

Integral = area under curve


a
James Grimbleby

b
School of Systems Engineering - Electronic Engineering

x
Slide 69

[G James p 76]

Straight Line Through 2 Points


The simplest type of curve fit is a straight line:
f ( x ) = ax + b

Suppose that the data points are (x1, y1) and (x2, y2):

( x1, y1)

f ( x ) = ax + b

y
( x2 , y 2 )
x
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 70

Straight Line Through 2 Points


Data points (x1, y1) and (x2, y2) lie on the straight line:
ax1 + b =
ax2 + b =

y1
y2

These simultaneous linear equations in a and b can be


solved algebraically to give:
y 2 y1
a=
x 2 x1
x 2 y1 x1y 2
b=
x 2 x1

Interpolation or extrapolation using a straight-line fit is


termed linear
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 71

Straight Line Through 2 Points


Example: the resistance R of a wire-wound resistor as a
function of temperature T:
T(C)
20
40
Thus:

so that:
James Grimbleby

R()
511.4
529.1

529.1 511.4
= 0.885
a=
40 20
40 511.4 20 529.1
= 493.7
b=
40 20
R = 0.885T + 493.7
School of Systems Engineering - Electronic Engineering

Slide 72

Straight Line Through 2 Points


R = 0.885T + 493.7

The data can be linearly interpolated using this function to


give the resistance at 27C:
R = 0.885 27 + 493.7 = 517.6

and can be linearly extrapolated using this function to give


the resistance at 51C:
R = 0.885 51 + 493.7 = 538.8

The temperature coefficient of the resistor is 0.885 /C


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 73

Quadratic Through 3 Points


A quadratic can exactly fit 3 data points:
f ( x ) = ax 2 + bx + c

Suppose that the data points are (x1, y1), (x2, y2) and (x3, y3):
( x2 , y 2 )
y

f ( x ) = ax 2 + bx + c
( x3 , y 3 )

( x1, y1)
x
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 74

Quadratic Through 3 Points


Data points (x1, y1), (x2, y2) and (x3, y3) lie on the
quadratic:
ax12 + bx1 + c = y1
ax22 + bx2 + c = y 2
ax32 + bx3 + c = y 3

These simultaneous linear equations can be solved to give


a, b and c
The solutions for quadratic and higher-order polynomial fits
are not normally obtained in algebraic form; instead the
coefficients are determined numerically
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 75

Quadratic Through 3 Points


Example: the frequency F of a crystal oscillator as a function
of the ambient temperature T:
T (C)
20
30
40

F (MHz)
1.00040
1.00107
1.00243

These values can be substituted into the quadratic polynomial


to give:
400a + 20b + c = 1.00040
900a + 30b + c = 1.00107
1600a + 40b + c = 1.00243
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 76

Quadratic Through 3 Points


Subtract the 1st equation from the 2nd and 3rd equations:
500a + 10b = 0.00067
1200a + 20b = 0.00203

Now multiply the 1st equation by 2, and subtract from the 2nd:
200a = 0.00069

or

a = 3.450 10 6

Back substitute a:
0.001725 + 10b = 0.00067

or

b = 1.055 10 4

Back substitute a and b:


0.00138 0.00211 + c = 1.00040
James Grimbleby

or

c = 1.00113

School of Systems Engineering - Electronic Engineering

Slide 77

Quadratic Through 3 Points


F = 3.450 10 6T 2 1.055 10 4T + 1.00113

The data can now be interpolated using this function to give


the oscillator frequency at 32 C:
F = 3.450 10 6 1024 1.055 10 4 32 + 1.00113
= 1.00129 MHz

The temperature coefficient at 20 C


dF
= 6.900 10 6T 1.055 10 4
dT
= 6.900 10 6 20 1.055 10 4 MHz/C
= 32.5 Hz/C
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 78

Polynomial Through n Points


A polynomial of order n-1 can be fitted exactly to n data points:
f ( x ) = ax n 1 + bx n 2 + ... + px 2 + qx + r

Such a polynomial has n coefficients and there will be n


simultaneous equations to be solved in order to obtain the
coefficients a, b .. p, q, r:
ax1n 1 + bx1n 2 + ... + px12 + qx1 + r = y1
ax2n 1 + bx2n 2 + ... + px22 + qx2 + r = y 2
........................................
axnn 1 + bxnn 2 + ... + pxn2 + qxn + r = y n
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 79

[G James p 87]

Lagrange Polynomials
The Lagrange polynomial which fits n data points (x1, y1),
(x2, y2) .. (xn, yn) is of n-1th order and has the form:
( x x2 )( x x3 )( x x 4 ) .. ( x xn )
y1
f ( x) =
( x1 x2 )( x1 x3 )( x1 x 4 ) .. ( x1 xn )
( x x1)( x x3 )( x x 4 ) .. ( x xn )
+
y2
( x 2 x1)( x 2 x3 )( x 2 x 4 ) .. ( x2 xn )
+ ...
( x x1)( x x 2 )( x x3 ) .. ( x xn 1)
+
yn
( xn x1)( xn x 2 )( xn x3 ) .. ( xn xn 1)
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 80

Lagrange Polynomials
The Lagrange formula exactly fits each of the data points
Consider the first data point y1=f(x1)
All terms in the formula except the first contain a numerator
factor (x-x1) which is zero at the first data point x=x1
Consequently all terms except the first are zero at the first
data point and the formula simplifies to:
( x1 x2 )( x1 x3 )( x1 x 4 ) .. ( x1 xn )
y1
f ( x1) =
( x1 x2 )( x1 x3 )( x1 x 4 ) .. ( x1 xn )
= y1
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 81

Lagrange Polynomials
Example: the hull drag F of a racing yacht as a function of
the hull speed V:
V (m/s)
0.0
0.5
1.0
1.5
2.0
2.5

F (N)
0.0
20.41
92.75
181.00
421.45
1265.23

A 5th-order Lagrange polynomial can fit this data exactly


Extrapolating to a hull speed of 3 m/s:
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 82

Lagrange Polynomials
(V 0.5)(V 1.0)(V 1.5)(V 2.0)(V 2.5)
0.0
f (V ) =
(0.0 0.5)(0.0 1.0)(0.0 1.5)(0.0 2.0)(0.0 2.5)
(V 0.0)(V 1.0)(V 1.5)(V 2.0)(V 2.5)
+
20.41
(0.5 0.0)(0.5 1.0)(0.5 1.5)(0.5 2.0)(0.5 2.5)
(V 0.0)(V 0.5)(V 1.5)(V 2.0)(V 2.5)
+
92.75
(1.0 0.0)(1.0 0.5)(1.0 1.5)(1.0 2.0)(1.0 2.5)
(V 0.0)(V 0.5)(V 1.0)(V 2.0)(V 2.5)
181.00
+
(1.5 0.0)(1.5 0.5)(1.5 1.0)(1.5 2.0)(1.5 2.5)
(V 0.0)(V 0.5)(V 1.0)(V 1.5)(V 2.5)
+
421.45
(2.0 0.0)(2.0 0.5)(2.0 1.0)(2.0 1.5)(2.0 2.5)
(V 0.0)(V 0.5)(V 1.0)(V 1.5)(V 2.0)
+
1265.23
(2.5 0.0)(2.5 0.5)(2.5 1.0)(2.5 1.5)(2.5 2.0)
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 83

Lagrange Polynomials
Lagrange polynomials can be converted to explicit polynomial
form
T (C)
F (MHz)
20
1.00040
30
1.00107
40
1.00243
Lagrange polynomial:
(T 30)(T 40)
1.00040
f (T ) =
(20 30)(20 40)
(T 20)(T 40)
+
1.00107
(30 20)(30 40)
(T 20)(T 30)
+
1.00243
( 40 20)( 40 30)
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 84

Lagrange Polynomials
T 2 70T + 1200
f (T ) =
1.00040
200

T 2 60T + 800
+
1.00107
100

T 2 50T + 600
+
1.00243
200

f (T ) = (T 2 70T + 1200) 0.00500200


(T 2 60T + 800) 0.01001070
+ (T 2 50T + 600) 0.00501215
f (T ) = (0.00500200 0.01001070 + 0.00501215)T 2
+ ( 0.3501400 + 0.6006420 0.2506075)T
+ 6.002400 8.008560 + 3.007290
= 0.00000345T 2 0.0001055T + 1.00113
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 85

[G James p 78]

Least-Squares Curve Fitting


Any measurements of real physical quantities are subject to
errors
It is unrealistic, therefore, to insist that a curve exactly fits the
measured data points
Suppose that a set of n data points (x1,y1), (x2,y2) .. (xn,yn) is
available, and a function f(x) is to be fitted to this data
The error i between the function f(x) and the data point (xi,yi)
is given by:

i = f ( xi ) y i
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 86

Least-Squares Curve Fitting


Each of the data points has an error and the function is
chosen in such a way as to minimise these errors, so
providing an optimum fit to the data
In order to determine the overall error between the function
and the data the individual errors must be combined in some
way
In the least-squares method the overall error E is taken to be
the sum of the squares of the individual errors:

E=
James Grimbleby

2
i
i =1

= {f ( xi ) y i }2
i =1

School of Systems Engineering - Electronic Engineering

Slide 87

Least-Squares Straight Line Fit


Equation representing a straight line:
f ( x ) = ax + b

and the overall error E is therefore given by:


n

E = {axi + b y i } 2
Differentiating w.r.t. a:

i =1

n
n
n
dE n
2
= 2 xi {axi + b y i } = 2a xi + 2b xi 2 xi y i
da i =1
i =1
i =1
i =1

Differentiating w.r.t. b:
n
n
n
dE n
= 2{axi + b y i } = 2a xi + 2b 1 2 y i
db i =1
i =1
i =1
i =1
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 88

Least-Squares Straight Line Fit


Minimum of the overall error E:
n

a xi2
i =1
n
a xi
i =1

i =1
n

i =1
n

i =1

i =1

+ b xi = xi y i
+ b 1 = yi

Not that the summations evaluate to numbers when the data


is inserted
These simultaneous equation in a and b are solved to obtain
the straight line least-squares fit
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 89

Least-Squares Straight Line Fit


Example: power output P of a semiconductor laser diode,
operating at 35C, as a function of the drive current I:
I (mA)
70
72
74
76
78
80

P (mW)
1.33
2.08
2.88
3.31
3.83
4.67

It is known that, above a certain threshold current, the laser


power increases linearly with drive current so that it is
appropriate to fit a straight line to the data
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 90

Least-Squares Straight Line Fit


The first stage in obtaining the straight line fit is to evaluate
the summations:
i
1
xi
yi
xi2
xiyi
1
1
70
1.33
4900
93.10
2
1
72
2.08
5184
149.76
3
1
74
2.88
5476
213.12
4
1
76
3.31
5776
251.56
5
1
78
3.83
6084
298.74
6
1
80
4.67
6400
373.60

450

18.10

33820

1379.88

These sums are used to set up the simultaneous equations


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 91

Least-Squares Straight Line Fit


n

33820a + 450b = 1379.88


450a + 6b = 18.1

1 = 6

i =1
n

xi = 450

Solve equations to obtain


coefficients:

i =1
n

y i = 18.1

i =1
n

a=0.319 b=-21.0

2
x
i = 33820

i =1
n

xi y i = 1379.88

i =1

James Grimbleby

so least-squares fit:
P = 0.319 I - 21.0

School of Systems Engineering - Electronic Engineering

Slide 92

Least-Squares Straight Line Fit


Temperature T (C)

20
18

Least-squares fit:

16
T = 19 - 0.01h

14
12
10
0

James Grimbleby

200
400
600
Height above sea level h (m)
School of Systems Engineering - Electronic Engineering

Slide 93

Least-Squares Quadratic Fit


A quadratic has three coefficients a, b and c, which must be
chosen to obtain the best fit to the data:
f ( x ) = ax 2 + bx + c

The overall error E is given by:


n

E = {axi2 + bxi + c y i } 2
i =1

Differentiating with respect to a gives:

dE n 2
= 2 xi {axi2 + bxi + c y i }
da i =1
=
James Grimbleby

2a xi4
i =1

+ 2b xi3
i =1

+ 2c xi2
i =1

2 xi2 y i
i =1

School of Systems Engineering - Electronic Engineering

Slide 94

Least-Squares Quadratic Fit


Setting derivatives to zero gives simultaneous linear
equations:
n

n
n
4
3
a xi + b xi + c xi2
i =1
i =1
i =1
n
n
n
3
2
a xi + b xi + c xi
i =1
i =1
i =1
n
n
n
2
a xi + b xi + c 1
i =1
i =1
i =1

= xi2 y i
i =1
n

= xi y i
i =1
n

= yi
i =1

These simultaneous equations can be solved to obtain the


coefficients a, b and c.
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 95

Least-Squares Quadratic Fit


Example: voltage V of a copper/constantan thermocouple
output as a function of temperature T.
T (C)
V (mV)
0
0.000
10
0.391
20
0.789
30
1.196
40
1.611
50
2.035
60
2.467
70
2.908
80
3.357
90
3.813
100
4.277
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 96

Least-Squares Quadratic Fit


1
1
2
3
4
5
6
7
8
9
10
11

xi

1
0
1 10
1 20
1 30
1 40
1 50
1 60
1 70
1 80
1 90
1 100

yi

xi2

0.000
0
0.391
100
0.789
400
1.196
900
1.611 1600
2.035 2500
2.467 3600
2.908 4900
3.357 6400
3.813 8100
4.277 10000

xi3
0.00100
1.00103
8.00103
2.70104
6.40104
1.25105
2.16105
3.43105
5.12105
7.29105
1.00106

xi4
0.000100
1.000104
1.600105
8.100105
2.560106
6.250106
1.296107
2.401107
4.096107
6.561107
1.000108

xiyi

xi2yi

0.00
3.91
15.78
35.88
64.44
101.75
148.02
203.56
268.56
343.17
427.70

0.0
39.1
315.6
1076.4
2577.6
5087.5
8881.2
14249.2
21484.8
30885.3
42770.0

11 550 22.844 38500 3.03106 2.533108 1612.77 127366.7

2.5333 108 a + 3.0250 106 b + 3.8500 10 4 c = 1.2737 105


3.0250 10 6 a + 3.8500 10 4 b + 5.5000 102 c = 1.6128 103
3.8500 10 4 a + 5.5000 10 2 b + 1.1000 101c = 2.2844 101
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 97

Least-Squares Quadratic Fit


Solving these equations gives:

a = 4.145 10 5
b = 3.863 10 2
c = 4.196 10 5
and the least-square quadratic fit is:

V = 4.145 10 5 T 2 + 3.863 10 2 T 4.196 10 5


Alternatively, x values are associated with the voltage V, and
the y values associated with the temperature T:

T = 5.287 10 1 V 2 + 2.561 101 V + 7.193 10 2


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 98

Least-Squares Quadratic Fit


T = 5.287 10 1 V 2 + 2.561 101 V + 7.193 10 2
V
0.000
0.391
0.789
1.196
1.611
2.035
2.467
2.908
3.357
3.813
4.277
James Grimbleby

T
0.000
10.000
20.000
30.000
40.000
50.000
60.000
70.000
80.000
90.000
100.000

quadratic
0.072
10.003
19.946
29.940
39.951
49.990
60.024
70.063
80.073
90.021
99.917

error
0.072
0.003
-0.054
-0.060
-0.049
-0.010
0.024
0.063
0.073
0.021
-0.083

School of Systems Engineering - Electronic Engineering

Slide 99

[G James p 569]

Numerical Integration
Numerical integration is the process of evaluating definite
integrals by numerical methods:
b

Z = f ( x ) dx
a

This is equivalent to solving Z = y(b) for the differential


equation:
dy
= f (x )
dx
with the initial condition y(a) = 0
Another way of looking at the definite integral is that it is the
area below the curve representing f(x)
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 100

f(x)

Numerical Integration

area = f ( x ) dx
a

x
x=a
James Grimbleby

x=b

School of Systems Engineering - Electronic Engineering

Slide 101

Numerical Integration
Analytical integration should be used where possible because
it will be more efficient and more accurate
Many functions cannot be integrated analytically, for example:
a

Z= e

-x

dx

Gaussian
distribution

1
2
Z=
H ( j) d
2 0
Equivalent noise
bandwidth

/2

Z=

d
1 k 2 sin2

Elliptic
integrals

In some cases it is necessary to integrate numerical data: this


cannot be done analytically
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 102

Polar Planimeter

James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 103

[G James p 570]

Trapezium Method

f(x)

Approximate area under


curve by a trapezium

f0

f1

x0
James Grimbleby

x1

x2

x3

x4

x5

x6

School of Systems Engineering - Electronic Engineering

x
Slide 104

Trapezium Method
The area under the curve in each interval is approximated by a
trapezium:

f0

1
area = hf0 + h(f1 f0 )
2
h
= (f0 + f1)
2

f1
h=x1-x0

Normally the n intervals will be of equal width h:

ba
h=
n

so that:

xk = a + kh
James Grimbleby

where

k = 0, 1, 2, .. , n

School of Systems Engineering - Electronic Engineering

Slide 105

Trapezium Method
Total area:

h
Z = {f ( xk 1) + f ( xk )}
k =1 2
h n
= {f ( xk 1) + f ( xk )}
2 k =1

This formula requires the function f(x) to be evaluated twice at


each x value (except at x0 and xn)
n 1

1
1
Z = h f ( x0 ) + f ( x k ) + f ( x n )
2

2
k =1
n 1
1

1
= h f (a) + f (a + kh) + f (b)
2
2

k =1
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 106

Trapezium Method
Example:

Z = x 4 exp x 2 dx
0

Splitting the integration range into 8 equal intervals:


k

xk

0
1
2
3
4
5
6
7
8
*

0.00
0.25
0.50
0.75
1.00
1.25
1.50
1.75
2.00

James Grimbleby

f(xk)
0.000000
0.003670
0.048675
0.180283
0.367879
0.511747
0.533584
0.438657
0.293050
2.231020

Z = 0.25 2.231020
= 0.557755

(Weighted sum)

School of Systems Engineering - Electronic Engineering

Slide 107

Trapezium Method
This result is not very accurate because of the small number
of intervals used (8)
To obtain a higher accuracy the integration range must be split
into much larger number of intervals:

n
100
200
500
1000

Z
0.560805
0.560820
0.560824
0.560824

What value of n should be used to achieve some required


degree of accuracy ?
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 108

Trapezium Method
Use the trapezium rule with successively larger values of n
until the results agree to within the required accuracy:

n=2

n=4

x1

x0

x2

x1

James Grimbleby

x2

x1

x2

x3

x4

n=8

n=4

x0

x0

x3

x4

x0 x1 x2 x3 x4 x5 x6 x7 x8

School of Systems Engineering - Electronic Engineering

Slide 109

Trapezium Method
Example: increase number of intervals to 16:
k
1
3
5
7
9
11
13
15

xk
0.125
0.375
0.625
0.875
1.125
1.375
1.625
1.875

f(xk)
0.000240
0.017181
0.103246
0.272600
0.451810
0.539663
0.497284
0.367442
2.249467

Even values of k
have already been
evaluated for n=8

Sum for n=8

Z = 0.125 (2.231020 + 2.249467)


= 0.560061
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 110

Integration of Tabulated Data


The trapezium approximation can be used to integrate
tabulated data
Example: vertical acceleration a of a rocket measured at 0.5 s
intervals:

James Grimbleby

t(s)

a(m/s2)

0
1
2
3
4
5
6
7
8

0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0

24.1
27.5
31.2
34.4
37.9
41.7
45.1
48.4
51.9

School of Systems Engineering - Electronic Engineering

Slide 111

Integration of Tabulated Data


t1

v1 = v 0 + a dt
t0

1
v 0 + (t1 t 0 )(a0 + a1)
2

James Grimbleby

t(s)

a(m/s2)

0
1
2
3
4
5
6
7
8

0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0

24.1
27.5
31.2
34.4
37.9
41.7
45.1
48.4
51.9

v(m/s)
0.000
12.900
27.575
43.975
62.050
81.950
103.650
127.025
152.100

School of Systems Engineering - Electronic Engineering

Slide 112

Integration of Tabulated Data


t1

h1 = h0 + v dt
t0

1
h0 + (t1 t 0 )(v 0 + v1)
2
k

t(s)

a(m/s2)

0
1
2
3
4
5
6
7
8

0.0
0.5
1.0
1.5
2.0
2.5
3.0
3.5
4.0

24.1
27.5
31.2
34.4
37.9
41.7
45.1
48.4
51.9

James Grimbleby

v(m/s)
0.000
12.900
27.575
43.975
62.050
81.950
103.650
127.025
152.100

h(m)
0.000
3.225
13.344
31.231
57.738
93.738
140.138
197.806
267.588

School of Systems Engineering - Electronic Engineering

Slide 113

[G James p 595]

Improper Integrals
In integral is termed improper if:
1. It has an integrable singularity between its upper and lower
limits, for example:
1

Z = x 1/ 3 dx
1

2. It has an integrable singularity at its upper or lower limits,


for example:
1

Z = x 1/ 3 dx
0

3. Its upper limit is or its lower limit is -


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 114

Improper Integrals
Type 1 improper integrals can always be converted to type 2
integrals by splitting the integration range
Suppose that f(x) has a singularity at c:
b

Z = f ( x ) dx = f ( x ) dx + f ( x ) dx

The resulting integrals have singularities at their integration


limits, and no singularity between their integration limits
The resulting integrals are therefore type 2 improper integrals
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 115

Improper Integrals
The trapezium method evaluates the integrand f(x) at each of
the integration limits
It cannot therefore be used with type 2 improper integrals
It is also unsuitable for integrating functions such as:
1

sin x
Z=
dx
0 x
Here the integrand is perfectly well-behaved at x=0
Nevertheless, any attempt to evaluate the integrand at x=0
on a computer will lead to a divide-by-zero error
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 116

[G James p 570]

Midpoint Method

f(x)

Approximate area under


curve by a rectangle

f1

x1
James Grimbleby

x2

x3

x4

x5

x6

School of Systems Engineering - Electronic Engineering

x
Slide 117

Midpoint Method
Normally the n intervals will be of equal width h:
ba
h=
n

and:

(2k 1)h
xk = a +
2

where

k = 1, 2, .. , n

Total area:
n

Z = hf ( xk )
k =1
n

(2k 1)h

= h f a +

k =1
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 118

Midpoint Method
Example:

sin x
Z=
dx
x
0

Splitting the integration range into 8 equal intervals:


k

xk

f(xk)

1
2
3
4
5
6
7
8

0.392699
1.178097
1.963496
2.748894
3.534292
4.319690
5.105089
5.890487

0.974496
0.784213
0.470528
0.139214
-0.108277
-0.213876
-0.180972
-0.064966

James Grimbleby

Z = 2 / 8 1.800360
= 0.785398 1.800360
= 1.413999

1.800360
School of Systems Engineering - Electronic Engineering

Slide 119

Midpoint Method
This result is not very accurate because of the small number
of intervals used (8)
To obtain a higher accuracy the integration range must be split
into much larger number of intervals:
n
100
200
500
1000

Z
1.418125
1.418145
1.418150
1.418151

What value of n should be used to achieve some required


degree of accuracy ?
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 120

Midpoint Method
Use the midpoint rule with successively larger values of n until
the results agree to within the required accuracy:
n=1

n=3

x1

x1

James Grimbleby

x2

x3

n=9

n=3

x1

x2

x3

x1 x2 x3 x4 x5 x6 x7 x8 x9

School of Systems Engineering - Electronic Engineering

Slide 121

Infinite Integration Limits


Both trapezium and midpoint methods will fail if either of the
limits are infinite:

Z = f ( x ) dx
0

It may be possible to replace infinite limits by large but finite


values, but this is often impractical
Fortunately the problem of infinite integration limits can easily
be overcome by using a variable transformation
The transformation used must convert infinite limits to finite
values, thus allowing the definite integral to be evaluated
numerically
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 122

Infinite Integration Limits

Z = f ( x ) dx
0

Let x=tan y; the limits x=0 and x= become y=0 and y=/2

dx
d sin y cos y sin2 y
=
=
+
= 1 + tan2 y
dy dy cos y cos y cos 2 y
so:

dx = {1 + tan2 y } dy
and the integral becomes:

/2

Z = f ( x ) dx = f ( tan y ) {1 + tan2 y} dy
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 123

Infinite Integration Limits


Example: equivalent noise bandwidth B:

1
2
B=
H ( j) d
2 0

where:

H ( j ) =

1 + ( j)2 / 42

1 + ( j) / 2 + 3( j)2 / 28 + ( j)3 / 84 + ( j) 4 / 1680


to evaluate:
nr = 1 x 2 / 42
ni = 0
d r = 1 3 x 2 / 28 + x 4 / 1680

d i = x / 2 x 3 / 84

2
2
n
n
+
2
r
i
f ( x ) = H ( jx ) =
d r2 + d i2
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 124

Infinite Integration Limits


Splitting the integration range of /2 into 8 intervals of /16:
k
1
2
3
4
5
6
7
8

yk

0.098175
0.294524
0.490874
0.687224
0.883573
1.079923
1.276272
1.472622

xk=tan(yk)

0.098491
0.303347
0.534511
0.820679
1.218504
1.870869
3.296560
10.153170

f(xk)

0.999192
0.992355
0.976417
0.945119
0.882234
0.740300
0.365908
0.030949

(1+xk2)f(xk)
1.008885
1.083671
1.255382
1.581670
2.192133
3.331460
4.342343
3.221357
18.016891

Z = 0.196349 18.016891 = 3.537608


Z
= 0.563028
B=
2
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 125

Optimisation
Optimisation involves adjusting the parameters of a system to
obtain the best performance
Most applications of computers in engineering are concerned
with analysis
Optimisation differs from these techniques in that it is a true
design method
Essentially optimisation closes the loop around analysis; if a
system can be analysed then it can also be optimised
Optimisation is usually formulated as a function minimisation
problem
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 126

Function Minimisation
Many designs can be optimised by minimising the difference
between their behaviour and some target behaviour
If some performance item is to be maximised (for example
efficiency) then function minimisation can still be used:

maximum(f ) = minimum( f )
The function to be minimised is known as the objective function
It is possible to optimise functions of several variables, but the
discussion here will be limited to single-variable optimisation
Two methods will be considered: approximation methods and
golden-section search
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 127

Thermistor Thermometer
Rth
Vref
R0

Rth = Ae

B /T

Vout

R0
R0
Vout = Vref
= Vref
R0 + Rth
R0 + Ae B / T

Typical values: A = 4.310-3, B = 4000.0


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 128

Thermistor Thermometer
V(50)
Linear
approximation

Vout

Temperature
error

Actual voltage

V(0)
0C
James Grimbleby

Temperature
School of Systems Engineering - Electronic Engineering

50C
Slide 129

Thermistor Thermometer

Temperature Error

10C

0C
100
James Grimbleby

R0
1k

10 k

School of Systems Engineering - Electronic Engineering

Slide 130

Approximation Methods
Fit a cubic polynomial to the objective function at 4 points:
Objective
function f(x)
Cubic
polynomial

Cubic exactly fits


f(x) at 4 x values

x
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 131

Approximation Methods
Coefficients a, b, c, d of cubic polynomial are chosen so that it
fits f(x) at four x values:
p( x ) = ax 3 + bx 2 + cx + d
Differentiating:

dp
= 3ax 2 + 2bx + c = 0
dx

which has a solution:

b b 2 3ac
xmin =
3a
Select minimum using second derivative:

d2 p
dx
James Grimbleby

= 6ax + 2b > 0

School of Systems Engineering - Electronic Engineering

Slide 132

Approximation Methods
The minimum xmin of the polynomial is only an approximation
to the minimum of the objective function
The process must be repeated, using xmin in place of the worst
of the original points, until a satisfactory accuracy has been
obtained
Approximation methods are useful in certain applications but
they require the objective function to be well-behaved
The objective function for the thermistor thermometer has a
discontinuous gradient at the minimum, and cannot be
accurately approximated around this point
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 133

Search Methods
Given an initial interval containing a minimum split the interval
into two or more sections and determine which of these
contains the minimum
Repeat the process until the minimum has been located to a
sufficient degree of accuracy
Search methods do not require the objective function to be
well-behaved and will find the minimum even if the objective
function is discontinuous.
Since the interval is reduced by a fixed ratio at each iteration,
search methods have an entirely predictable convergence
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 134

Objective function f(x)

Binary Search

a
James Grimbleby

c-h c c+h
School of Systems Engineering - Electronic Engineering

Slide 135

Golden-Section Search
The binary search method has the disadvantage that it is
difficult to choose a suitable value for h
If h is large then the convergence rate is reduced
If h is small then errors can occur because of rounding
These problems are overcome in the Golden-Section
method, where h is large but only one objective evaluation is
required at each iteration
The name Golden-Section derives from the convergence rate
which is 1.618
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 136

Objective function f(x)

Golden-Section Search

James Grimbleby

p
a

q
p
a

q
p q

School of Systems Engineering - Electronic Engineering

b
b
b

Slide 137

Golden Section Search


Range is reduced by k at each iteration:
ba
qa = b p =
k
Second iteration:
a/ = p

b/ = b

p/ = q

Applying same reduction factor:


q / a/

b/ p/

or:
bq
James Grimbleby

b/ a/
k

bp
k

School of Systems Engineering - Electronic Engineering

Slide 138

Golden Section Search


Combining equations and using the fact that:
bq

gives:

bp
k

or:

ba

k2
1

k2

thus:
2

k k 1= 0
James Grimbleby

(b a) (q a)

ba
(b a )
k

1
1
k
1+ 5
k=
= 1.618
2

School of Systems Engineering - Electronic Engineering

Slide 139

Golden-Section Search Algorithm


Search interval a to b contains at least one minimum
While (b-a) is greater than required absolute precision {
Let c = (b-a)/k; then p = b-c, q = a+c
Evaluate the objective function at p and q
If f(p) < f(q) then minimum lies between a and q: let a'=a, b'=q
else minimum lies between p and b: let a'=p, b'=b
} end while
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 140

Golden-Section Search
The search is continued until the search interval b-a is less
than the required precision
Convergence rate is k = 1.618
If required precision is and the initial search interval is a to
b, then number of iterations n will be:
log(b a) log( )
n=
log(k )

Golden-Section search will only work if the initial search


interval brackets a minimum
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 141

Golden-Section Search
Thermistor thermometer: choose initial range a=1k, b=4k
b a 3 k
qa = b p =
=
= 1.854 k
1.618
k

so that:

p = b 1.854 k = 4 k 1.854 k = 2.146 k


q = a + 1.854 k = 1 k + 1.854 k = 2.854 k

1.854k
a=1k

James Grimbleby

1.854k

p=2.146k

q=2.854k

f(p)=1.171

f(q)=2.061

School of Systems Engineering - Electronic Engineering

b=4k

Slide 142

Golden-Section Search
Next iteration: a'=1k, b'=2.854k
so that:

b'a' 1.854 k
q ' a' = b ' p ' =
=
= 1.146 k
k
1.618

p' = b'1.146 k = 2.854 k 1.146 k = 1.708 k


q ' = a'+1.146 k = 1 k + 1.146 k = 2.146 k

1.146k
a'=1k

James Grimbleby

1.146k

p'=1.708k

q'=2.146k

f(p')=2.179

f(q')=1.171

School of Systems Engineering - Electronic Engineering

b'=2.854k

Slide 143

Golden-Section Search
a

1.00010+3
1.00010+3
1.70810+3
1.70810+3
1.97910+3
2.14610+3
2.14610+3
2.21010+3
2.21010+3
2.23410+3
2.24910+3
2.24910+3
2.25510+3
2.25510+3
2.25710+3
2.25710+3

2.14610+3
1.70810+3
2.14610+3
1.97910+3
2.14610+3
2.24910+3
2.21010+3
2.24910+3
2.23410+3
2.24910+3
2.25910+3
2.25510+3
2.25910+3
2.25710+3
2.25910+3
2.25810+3

q
2.85410+3
2.14610+3
2.41610+3
2.14610+3
2.24910+3
2.31310+3
2.24910+3
2.27410+3
2.24910+3
2.25910+3
2.26410+3
2.25910+3
2.26110+3
2.25910+3
2.25910+3
2.25910+3

b
4.00010+3
2.85410+3
2.85410+3
2.41610+3
2.41610+3
2.41610+3
2.31310+3
2.31310+3
2.27410+3
2.27410+3
2.27410+3
2.26410+3
2.26410+3
2.26110+3
2.26110+3
2.25910+3

f(p)
1.17110+0
2.17910+0
1.17110+0
1.51310+0
1.17110+0
9.81010-1
1.05210+0
9.81010-1
1.00810+0
9.81010-1
9.65710-1
9.71510-1
9.65710-1
9.67910-1
9.65710-1
9.66510-1

f(q)
2.06110+0
1.17110+0
1.25710+0
1.17110+0
9.81010-1
1.06310+0
9.81010-1
9.92110-1
9.81010-1
9.65710-1
9.75310-1
9.65710-1
9.68910-1
9.65710-1
9.66410-1
9.65710-1

Minimum at R0 = 2.259 k where peak error is 0.97 C.


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 144

Objective function f(x)

Bracketing a Minimum

James Grimbleby

b
a

c
b
a

x
c
b

School of Systems Engineering - Electronic Engineering

c
Slide 145

Differential Equations
The time-domain response of electronic systems and the
dynamical behaviour of mechanical systems can be described
by differential equations
Many differential equations can be solved analytically,
particularly those describing idealised systems
The more realistic the mathematical model of a physical
system, the less likely it is that an analytical solution can be
found
In cases where an analytical solution is not available the
differential equations must be solved by numerical methods
and as a consequence the solution will not be exact
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 146

Differential Equations
We shall first consider the problem of solving first-order nonlinear differential equations of the form:
dx
= f ( x,t )
dt

Most physical systems cannot be described by a single firstorder differential equation


Fortunately any nth-order differential equation can be reduced
to n coupled first-order differential equations
The solution of nth-order differential equations is then simply a
generalisation of the method used for solving a single equation.
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 147

Differential Equations
R
100k
i(t)
vi
4Vdc

C
10nF

vC(t)

Suppose that prior to t=0 the switch is open and the capacitor
discharged: vC=0
Then at t=0 the switch is closed and the capacitor starts to
charge towards the input voltage vi
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 148

Differential Equations
R
100k
i(t)
vi
4Vdc

C
10nF

vC(t)

dv c (t )
i (t ) = C
dt
Ri (t ) + v c (t ) = v i

dv c (t )
RC
+ v c (t ) = v i
dt

This differential equation has an analytical solution:


t

v c (t ) = v i {1 e RC }
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 149

Euler Integration
A first-order differential equation can be written in the form:
dx
= f ( x,t )
dt
Provided that x and t are sufficiently small:
x dx

t dt

or

dx
x t
= t f ( x,t )
dt

If x is known at time t, then its value at time t+t is:


x(t + t ) = x(t ) + x
dx
x(t ) + t
dt
x(t ) + t f ( x,t )

This is the Euler integration formula


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 150

Euler Integration
Equation for the capacitor voltage vc can be rewritten:
dv c (t )
1
=
{v i v c (t )}
dt
RC

Using Euler integration formula:


t
{v i v c (t )}
v c (t + t ) = v c (t ) +
RC

Time step t should be between 1/10 and 1/1000 of shortest


time constant T0:
T0 = RC = 105 10 8 = 10 3 s

Choose time step: t = 10 4 s


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 151

Euler Integration
t
0.0000
0.0001
0.0002
0.0003
0.0004
0.0005
0.0006
0.0007
0.0008
0.0009
0.0010

x(t)
0.000000
0.400000
0.760000
1.084000
1.375600
1.638040
1.874236
2.086812
2.278131
2.450318
2.605286

f(x,t)
4000.00
3600.00
3240.00
2916.00
2624.40
2361.96
2125.76
1913.19
1721.87
1549.68
1394.71

x=t.f(x,t) x(exact)
0.400000
0.360000
0.324000
0.291600
0.262440
0.236196
0.212576
0.191319
0.172187
0.154968
0.139471

0.000000
0.380650
0.725077
1.036727
1.318720
1.573877
1.804753
2.013659
2.202684
2.373721
2.528482

At a time t=1 ms the error in the numerical result is 0.077 V


corresponding to a relative precision of about 3%
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 152

Euler Integration
Euler integration using smaller time steps:
t
10-4
10-5
10-6

Iterations
10
100
1000

vc
2.605286
2.535871
2.529218

error
0.076804
0.007389
0.000736

A 10-fold reduction in t reduces the error by a factor of 10


Repeat Euler integration with successively smaller time steps
until two results agree to within the required precision
In practice, because of its poor error performance, Euler
integration is rarely used
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 153

Euler Integration

x(t+t)
dx
= f ( x,t )
slope =
dt

x(t)

t
James Grimbleby

x = t f ( x,t )

t+t

School of Systems Engineering - Electronic Engineering

t
Slide 154

Heun Integration
The Heun method obtains a better estimate of the slope by
averaging the derivative at the start and end of the interval
k1 = f ( x,t )
x1 = x(t ) + t .k1

An approximation to the slope k2 at the end of the interval


k 2 = f (x1,t + t )

The average of k1 and k2 is now used to extrapolate over the


full interval from the initial point:
t
x(t + t ) = x(t ) + {k1 + k 2 }
2
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 155

Heun Integration
x
x(t+t)
x1

slope = k 2 = f ( x1,t + t )

t
{k1 + k 2 }
x =
2

slope = k1 = f ( x,t )

x(t)

t
k1 + k 2
slope =
2

t
James Grimbleby

t+t

School of Systems Engineering - Electronic Engineering

t
Slide 156

Heun Integration
t
0.0000
0.0001
0.0002
0.0003
0.0004
0.0005
0.0006
0.0007
0.0008
0.0009
0.0010

x(t)
0.000000
0.380000
0.723900
1.035130
1.316792
1.571697
1.802386
2.011159
2.200099
2.371090
2.525836

k1=f(x,t)
4000.00
3620.00
3276.10
2964.87
2683.21
2428.30
2197.61
1988.84
1799.90
1628.91
1474.16

x1(t)
0.400000
0.742000
1.051510
1.331617
1.585113
1.814527
2.022147
2.210043
2.380089
2.533981
2.673252

k2
3600.00
3258.00
2948.49
2668.38
2414.89
2185.47
1977.85
1789.96
1619.91
1466.02
1326.75

x
0.380000
0.343900
0.311230
0.281663
0.254905
0.230689
0.208773
0.188940
0.170991
0.154746
0.140046

exact
0.000000
0.380650
0.725077
1.036727
1.318720
1.573877
1.804753
2.013659
2.202684
2.373721
2.528482

The error at time t=1 ms of 0.002646 compares favourably with


that obtained using the Euler method; however the Heun method
involved twice as many evaluations of f(x,t).
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 157

Heun Integration
Heun integration using smaller time steps:
t
10-4
10-5
10-6

Iterations
10
100
1000

vc
2.525836
2.528458
2.528482

error
0.002646
0.000025
0.000000

A 10-fold reduction in t reduces the error by a factor of 100


The Heun method is one of a family of predictor-corrector
integration algorithms
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 158

2nd-Order Runge-Kutta Integration


The Euler method can be improved by extrapolating to the
midpoint of the interval t+t/2:
k1 = f ( x,t )

t
x1 = x(t ) + k1
2

and evaluating the slope k2 at the midpoint:


k 2 = f (x1,t + t / 2)

This slope, which is a good approximation to the average


slope over the interval, is used to extrapolate over the full
interval from the initial point:
x(t + t ) = x(t ) + t k 2
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 159

2nd-Order Runge-Kutta Integration


x

slope = k 2 = f ( x1,t + t / 2)

x(t+t)
x1
slope = k1 = f ( x,t )
slope = k 2

x = t k 2

x(t)
t / 2

t
James Grimbleby

t+t/2

t / 2

t+t

School of Systems Engineering - Electronic Engineering

t
Slide 160

2nd-Order Runge-Kutta Integration


t
0.0000
0.0001
0.0002
0.0003
0.0004
0.0005
0.0006
0.0007
0.0008
0.0009
0.0010

x(t)

k1=f(x,t) t/2.f(x,t)

0.000000
0.380000
0.723900
1.035129
1.316792
1.571697
1.802386
2.011159
2.200099
2.371090
2.525836

4000.00
3620.00
3276.10
2964.87
2683.21
2428.30
2197.61
1988.84
1799.90
1628.91
1474.16

0.200000
0.181000
0.163805
0.148244
0.134160
0.121415
0.109881
0.099442
0.089995
0.081446
0.073708

x1

0.200000
0.561000
0.887705
1.183373
1.450953
1.693112
1.912266
2.110601
2.290094
2.452535
2.599544

k2
3800.00
3439.00
3112.30
2816.63
2549.05
2306.89
2087.73
1889.40
1709.91
1547.47
1400.46

t.k2

x(exact)

0.380000
0.343900
0.311230
0.281663
0.254905
0.230689
0.208773
0.188940
0.170991
0.154747
0.140046

0.000000
0.380650
0.725077
1.036727
1.318720
1.573877
1.804753
2.013659
2.202684
2.373721
2.528482

The error at time t=1 ms of 0.002646 compares favourably with


that obtained using the Euler method; however the Runge-Kutta
method involved twice as many evaluations of f(x,t).
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 161

2nd-Order Runge-Kutta Integration


2nd-order Runge-Kutta integration using smaller time steps:
t
10-4
10-5
10-6

Iterations
10
100
1000

vc
2.525836
2.528458
2.528482

error
0.002646
0.000024
0.000000

A 10-fold reduction in t reduces the error by a factor of 100


The 2nd-order Runge-Kutta method is one of a family of
integration algorithms, of which the 4th-order Runge-Kutta, or
RK-4, algorithm is the most generally useful.
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 162

4th-Order Runge-Kutta Integration


The slopes k1, k2, k3, k4 are evaluate at 4 points:
k1 = f ( x,t )
t
t

k 2 = f x + k1 ,t +
2
2

t
t

k 3 = f x + k 2 ,t +
2
2

k 4 = f (x + k 3 t ,t + t )

The the weighted sum of these slopes is used to extrapolate


over the complete interval:
t
x(t + t ) = x(t ) + (k1 + 2k 2 + 2k 3 + k 4 )
6
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 163

4th-Order Runge-Kutta Integration


x
k2

x(t+t)
k1

k3

k2

x(t)

k4

k3

1
k = (k1 + 2k 2 + 2k 3 + k 4 )
6

t
James Grimbleby

t+t/2

t+t

School of Systems Engineering - Electronic Engineering

t
Slide 164

4th-Order Runge-Kutta Integration


Because of the complexity of the fourth-order Runge-Kutta
algorithm it is normally performed on a computer
t
10-4
10-5
10-6

Iterations
10
100
1000

vc
2.528481
2.528482
2.528482

error
0.000001
0.000000
0.000000

10 iterations of the Runge-Kutta algorithm provide much


better accuracy than 1000 iterations of the Euler algorithm
1000000 Euler iterations are required to achieve the same
accuracy as 10 4th-order Runge-Kutta iterations!
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 165

nth-Order Differential Equations


The Euler and Runge-Kutta methods developed so far can be
used to solve first-order differential equations:
dx
= f ( x,t )
dt

Most real systems are described by higher-order differential


equations
A general nth-order differential equation can be written in the
form:
n 1

dx d2 x
dn x
d
x
, ...
,t
= f x, ,
n 1
dt dt 2
d
t
dt n

James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 166

nth-Order Differential Equations


To reduce this nth-order equation to first-order equations a set
of n-1 new variables are introduced:
dx
x1 =
so that:
dt
d2 x

dx1
x2 =
=
dt
dt 2
d3 x

dx 2
x3 =
=
dt
dt 3
. . . . . . . . . . . . .
dn 1x

dxn 2
xn 1 =
=
dt
dt n 1
James Grimbleby

dxn 1 dn x
=
dt
dt n
n 1
dx d2 x

d
x
= f x, ,
,t
, ...
n 1
dt dt 2
d
t

= f (x, x1, x2 , ... xn 1, t )

School of Systems Engineering - Electronic Engineering

Slide 167

nth-Order Differential Equations


The n first-order differential equations:
dx
= x1
dt
dx1
= x2
dt
dx2
= x3
dt
. . . . . . . . . . . . .
dxn 2
= xn 1
dt
dxn 1
= f (x, x1, x 2 , ... , xn 1, t )
dt
James Grimbleby

The set of n first-order


differential equations can
then be solved by the Euler
or Runge-Kutta method
Note that the equations are
not independent

School of Systems Engineering - Electronic Engineering

Slide 168

nth-Order Differential Equations


Example:

d3 x

d2 x

dx
+2
+ 2 + 8 xt = 5
3
2
dt
dt
dt

Create 2 new variables:


dx
x1 =
dt
d2 x

dx1
=
x2 =
2
dt
dt

James Grimbleby

dx
= x1
dt
dx1
= x2
dt
dx2 d3 x
=
= 2 x2 2 x1 8 xt + 5
3
dt
dt

School of Systems Engineering - Electronic Engineering

Slide 169

n-Variable Euler Method


An iteration consists of updating each of the n variables using
the Euler formula
dx
= f0 = x1
dt
dx1
= f1 = x 2
dt
dx2
= f2 = 2 x2 2 x1 8 xt + 5
dt

It is important that all the n derivatives f0 .. fn are calculated


before any of the n variables are updated.
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 170

n-Variable Euler Method


Solution at t=2 s using a time step t=0.01 s and starting from
an initial condition of x=0, x1=0 and x2=0 at t=0:
t

x1

x2

0.00
0.01
0.02
0.03
0.04
0.05

0.00000
0.00000
0.00000
0.00000
0.00002
0.00005

0.00000
0.00000
0.00050
0.00149
0.00296
0.00490

0.00000
0.00000
0.00000
0.00001
0.00003
0.00004

0.00000
0.00050
0.00099
0.00147
0.00194
0.00240

0.05000
0.04900
0.04801
0.04703
0.04606
0.04510

1.99
2.00

1.28996 -0.52439 -4.87962 -0.00524


1.28471 -0.57319 -4.92690 -0.00573

-0.04879
-0.04926

-0.04728
-0.04555

0.00000
0.05000
0.09900
0.14701
0.19404
0.24010

x=t.f0

x1=t.f1 x2=t.f2

The Runge-Kutta method can also be generalised to deal with


sets of n coupled first-order differential equations
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 171

Simulation of Electronic Systems


Linear electronic systems can be characterised by a frequency
response function H(j):
Y ( j)
H ( j) =
=
X ( j)

a0 + a1( j) + a2 ( j)2 + .. + an 1( j)n 1 + an ( j)n


b0 + b1( j) + b2 ( j)2 + .. + bn 1( j)n 1 + bn ( j)n

where X(j) is the Fourier transform of the input x(t), and Y(j)
is the Fourier transform of the output y(t)
Introduce an auxiliary variable z(t) with Fourier transform Z(j):
Y ( j) = {a0 + a1( j) + a2 ( j)2 + .. + an 1( j)n 1 + an ( j)n } Z ( j)

so that:
{b0 + b1( j) + b2 ( j)2 + .. + bn 1( j)n 1 + bn ( j)n } Z ( j) = X ( j)
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 172

Simulation of Electronic Systems


The equivalent of j in the frequency domain is d/dt in the time
domain:
2
n
d
( j)
dt

( j)

dt

( j)

dt n

The differential equation involving z(t) and x(t) is therefore:

d
d2
dn 1
dn
b0 + b1 + b2 2 + .. + bn 1 n 1 + bn n z(t ) = x(t )
dt

dt
dt
dt

This differential equation in z can now be solved numerically in


the usual way
First the nth-order differential equation is converted to n firstorder differential equations
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 173

Simulation of Electronic Systems


A set of n-1 new variables are introduced to replace the first n1 derivatives of z:
dz
= z1
dt
dz
z1 =
dz1
dt
= z2
dt
2
d z dz1
. . . . . . . . . . . . .
=
z2 =
dt
dt 2
dzn 2
= zn 1
. . . . . . . . . . . . .
dt
dzn 1 1
dn 1z dzn 2
=
{ b0 z b1z1 ..
=
zn 1 =
dt
bn
dt
dt n 1
bn 1zn 1 + x(t )}
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 174

Simulation of Electronic Systems


Converting the defining equation for Z(j) into the time
domain:

d
dn 1
dn
+ an
y (t ) = a0 + a1 + .. + an 1
z(t )

dt

dt n 1
dt n
dzn 1
= a0 z + a1z1 + .. + an 1zn 1 + an
dt
an
= a0 z + a1z1 + .. + an 1zn 1 +
{ b0 z b1z1 .. bn 1zn 1 + x(t )}
bn
an b0

an b1
an bn 1
an
= a0
x(t )
z + a1
z1 + .. + an 1
zn 1 +
bn
bn
bn
bn

The output y can now be obtained by substituting z, z1, ..zn-1


James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 175

Simulation of Electronic Systems


Example:

0.01F
10k
Input

1.67H
0.05F

Output

This circuit has a frequency-response function:


H ( j) =

1 + 1.67 10 8 ( j)2
1 + 5.00 10 4 ( j) + 1.00 10 7 ( j)2 + 8.33 10 12 ( j)2

James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 176

Simulation of Electronic Systems


This response is 3rd-order so that their are 3 first-order
differential equations in the coupled set:
dz
= f0 = z1
dt
dz1
= f1 = z2
dt
dz2
z 5.00 10 4 z1 1.00 10 7 z2 + x(t )
= f2 =
dt
8.33 10 12

and the output y(t) is given by:


y (t ) = z + 1.67 10 8 z2
James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 177

Simulation of Electronic Systems


x(t ) = exp

1.0

(t 2.0 10 3 )2
2(0.5 10 3 )2

Output
y(t)

0.0
0.0ms
James Grimbleby

t
5.0ms

School of Systems Engineering - Electronic Engineering

Slide 178

Numerical Methods

J. B. Grimbleby, October 07

James Grimbleby

School of Systems Engineering - Electronic Engineering

Slide 179

Você também pode gostar