Escolar Documentos
Profissional Documentos
Cultura Documentos
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
Interpolation
Alistair Moffat
Algorithmic Goals
Number
Representations
For symbolic processing (for example, sorting strings), desire Root Finding
Interpolation
Algorithmic Goals
Number
Representations
For numeric processing, desire algorithms that are:
Root Finding
Numerical
Integration
I Above all else, correct
Interpolation
I Straightforward to implement
I Effective, in that yield correct answers and have broad
applicability and/or limited restrictions on use
I Efficient in terms of memory and time
I (For approximations) Stable and reliable in terms of the
underlying arithmetic being performed.
Algorithmic Goals
Number
Wish to compute Representations
Root Finding
f (x) = x x +1 x Numerical
Integration
Interpolation
and
x
g (x) = .
x +1+ x
I sqdiff.c
Algorithmic Goals
Number
Representations
Root Finding
Wish to compute
n Numerical
X 1 Integration
h(n) =
i Interpolation
i=1
I logsum.c
Algorithmic Goals
Root Finding
Algorithmic Goals
Number
Representations
Inside the computer, everything is stored as a sequence of Root Finding
binary digits, or bits. Numerical
Integration
Interpolation
Each bit can take one of two values 0, or 1.
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
The preprocessor function sizeof() can be supplied with Interpolation
either a type or a variable, and at compilation time is
replaced by the number of bytes occupied by that type:.
I sizeof.c
COMP20005
Binary numbers
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
In char, short, int, and long variables, the bits are used to Numerical
Integration
create a binary number.
Interpolation
Algorithmic Goals
Number
Representations
Root Finding
Binary counting: 1, 10, 11, 100, 101, 110, 111, 1000, 1001,
Numerical
1010, 1011, 1100, 1101, 1110, 1111, 10000, and so on. Integration
Interpolation
With a little bit of practice, you can count to 1,023 on your
fingers; and with a big bit of practice, to 1,048,575 if you
use your toes as well.
Algorithmic Goals
Number
Representations
Root Finding
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
The second column of Table 13.3 (page 232) shows the Interpolation
Algorithmic Goals
Number
Integer representation Representations
Bit pattern
unsigned sign-magn. twos-comp. Root Finding
0000 0 0 0
Numerical
0001 1 1 1 Integration
0010 2 2 2
Interpolation
0011 3 3 3
0100 4 4 4
0101 5 5 5
0110 6 6 6
0111 7 7 7
1000 8 0 8
1001 9 1 7
1010 10 2 6
1011 11 3 5
1100 12 4 4
1101 13 5 3
1110 14 6 2
1111 15 7 1
COMP20005
Sign-magnitude representation
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
Algorithmic Goals
Number
Representations
Root Finding
So 1101 is expanded as
1 (23 ) + 1 22 + 0 21 + 1 20 , which is minus three.
COMP20005
Twos-complement representation
Alistair Moffat
Algorithmic Goals
Number
Representations
Numerical
I there is only one representation for zero, and Integration
Interpolation
I integer arithmetic is easy to perform.
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
Revisiting an old program, it should now make more sense.
Interpolation
I overflow.c
Algorithmic Goals
Number
Representations
The type char is also an integer type, and using 8 bits can
store values from (27 ) = 128 to 27 1 = 127
COMP20005
Unsigned types
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
Numerical
C offers a set of alternative integer representations, unsigned Integration
char, unsigned short, unsigned int (or just unsigned), Interpolation
But will get printed out if you still use "%d" format
descriptors. Use "%u" instead, or "%lu", or "%llu".
COMP20005
Unsigned types
Alistair Moffat
Algorithmic Goals
Number
Representations
C also provides low-level operations for isolating and setting Root Finding
individual bits in int and unsigned variables. Numerical
Integration
Interpolation
These operations include left-shift (<<), right-shift (>>),
bitwise and (&), bitwise or (|), bitwise exclusive or (^), and
complement (~).
I intbits.c
COMP20005
Unsigned types
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
Table 13.5 (page 236) gives a final precedence table that Interpolation
includes all of the bit operations. If in doubt, over
parenthesize.
Algorithmic Goals
Number
C also supports constants that are declared as octal (base 8) Representations
and hexadecimal (base 16) values. Beware! Any integer Root Finding
Interpolation
int o = 020;
int h = 0x20;
printf("o = %oo, %dd, %xx\n", o, o, o);
printf("h = %oo, %dd, %xx\n", h, h, h);
gives
o = 20o, 16d, 10x
h = 40o, 32d, 20x
COMP20005
Octal and hexadecimal
Alistair Moffat
Algorithmic Goals
Number
Representations
The standard Unix tool bc can be used to do radix conversions: Root Finding
Numerical
mac: bc Integration
ibase=10
Interpolation
obase=2
25
11001
obase=8
25
31
obase=16
25
19
COMP20005
Floating point representations
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
The floating point types float and double are stored as: Interpolation
I a one bit sign, then
I a we -bit integer exponent of 2 or 16, then
I a wm -bit mantissa, normalized so that the leading
binary (or sometimes hexadecimal) digit is non-zero.
COMP20005
Floating point representations
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
When w = 32, a float variable has around wm = 24 bits of Interpolation
precision in the mantissa part. This corresponds to about 7
or 8 digits of decimal precision.
Algorithmic Goals
Number
For example, when w = 16, ws = 1, we = 3, wm = 12, the Representations
Root Finding
exponent is a binary numbers stored using we -bit
Numerical
twos-complement representation, and the mantissa is a Integration
wm -bit binary fraction: Interpolation
Algorithmic Goals
Number
Representations
Floating point representations can also be investigated:
Root Finding
Numerical
Integration
I floatbits.c
Interpolation
Algorithmic Goals
Number
Representations
The (non-ANSI) extended types long long (64-bit integer) Root Finding
and long double (128-bit floating point value) might also Numerical
Integration
come in useful at some stage. Interpolation
Algorithmic Goals
Given a continuous function f (), determine the set of values Number
Representations
x such that f (x) = 0.
Root Finding
Numerical
Why? Function often represents a point of balance between Integration
to opposing constraints (for example, gravity and frictional Interpolation
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
Plot the graph, and look for the roots.
Interpolation
Algorithmic Goals
Number
Representations
A better algorithm uses bisection to rapidly locate an
Root Finding
approximation of the root.
Numerical
Integration
I bisection.c
COMP20005
Bisection method
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
(x1,fx1)
f(x) Interpolation
x2
x1 mid
(x2,fx2)
COMP20005
Bisection method
Alistair Moffat
Algorithmic Goals
Number
Representations
The function judges termination by two different conditions:
Root Finding
I Whether the root has been found to the required Numerical
precision (and not by a test of the form f(x)==0.0), and Integration
Interpolation
I Whether some preset number of iterations has been
exceeded.
Algorithmic Goals
Number
Representations
Root Finding
If f (x) = x 2 10, and x0 = 0 and x1 = 10, the sequence of Numerical
midpoint estimates xm is Integration
Interpolation
The correct value is x = 10 = 3.16227766016.
Algorithmic Goals
Number
Representations
Root Finding
Each iteration adds one bit of accuracy, or a little under one
Numerical
third of a decimal digit. This observation allows the number Integration
Algorithmic Goals
Number
Rather than use midpoint, could also use proportions and Representations
Numerical
Integration
f (x2 )x1 f (x1 )x2
xm = . Interpolation
f (x2 ) f (x1 )
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
In practice convergence speed is not a problem, 50 or 100 Interpolation
loop iterations is nothing.
But both bisection and false position are unreliable (or may
fail completely) when multiple roots fall within preliminary
inspection interval.
COMP20005
Open methods
Alistair Moffat
Algorithmic Goals
Number
A range of other options are also available that do not Representations
Numerical
Integration
In the one-point iterative approach, the defining equation is Interpolation
rewritten to isolate (one instance of) x.
Algorithmic Goals
Number
Representations
Root Finding
The fixed-point iteration method converges when the
Numerical
magnitude of the derivative of the new function g () in the Integration
than one.
Algorithmic Goals
Number
Representations
If the slope of the function at the estimated root is known, it
Root Finding
can be used to compute a tangent to the curve. Numerical
Integration
f (xi )
xi+1 = NR(xi ) = xi .
f 0 (xi )
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
The number of correct digits approximately doubles at each Interpolation
iteration, provided certain conditions are met.
Algorithmic Goals
Number
Representations
Numerical
Interpolation
x2
10 1 10
NR(x) = x = x+ .
2x 2 x
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Combining the ideas of false position and Newton-Raphson, Integration
(x1 , y1 ):
yk xk1 yk1 xk
xk+1 =
yk yk1
The tangent f 0 (x) is approximated by computing the
gradient between two points lying on the curve.
COMP20005
Multiple simultaneous equations
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
Systems of equations also sometimes arise: Numerical
Integration
f1 (x1 , x2 , x3 , . . . , xn ) = 0 Interpolation
f2 (x1 , x2 , x3 , . . . , xn ) = 0
f3 (x1 , x2 , x3 , . . . , xn ) = 0
fn (x1 , x2 , x3 , . . . , xn ) = 0
Algorithmic Goals
Number
Representations
Numerical
Integration
sin x1 cos x2 = 0
Interpolation
x1 + x2 1 = 0
Algorithmic Goals
Number
Representations
Turning points near the root being sought can give divergent
outcomes with NR; and multiple roots can affect both
approaches. So make sure you know the nature of the
function first.
COMP20005
Numerical integration
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Given some continuous function f (x) and two limits, x1 and Integration
x2 , compute Z x2
Interpolation
f (x)dx .
x1
Algorithmic Goals
Number
Representations
Root Finding
As a crude approximation, assume that f (x) is linear Numerical
Integration
between the points (x1 , f (x1 )) and (x2 , f (x2 )), and compute
Interpolation
the area of the trapezoid that is defined:
f (x1 ) + f (x2 )
I = (x2 x1 ) .
2
Algorithmic Goals
Number
Representations
To get a better estimate, break the interval up into n steps
Root Finding
each of size h = (x2 x1 )/n. Then the area of each small
Numerical
trapezoid is worked out, and added to a total. Integration
Interpolation
X
I = trapezoids
n1
X f (x1 + h i) + f (x1 + h (i + 1))
= h
2
i=0
n1
!
h X
= f (x1 ) + 2 f (x1 + h i) + f (x1 + h n) .
2
i=1
COMP20005
Trapezoidal method
Alistair Moffat
Algorithmic Goals
Number
Representations
The greater the value of n, the smaller the value of h, and Root Finding
the better the approximation. Numerical
Integration
Interpolation
In theory, that is.
Algorithmic Goals
Number
Representations
Numerical
points. If n is even (or is doubled to make it even), can fit a Integration
quadratic to consecutive triples of points. Interpolation
Algorithmic Goals
Number
Representations
Hence, Simpsons method for numerical integration:
Root Finding
Numerical
h Integration
I = f (x0 ) + f (x0 + h n) +
3 Interpolation
n1
X n2
X
4 f (x0 + h i) + 2 f (x0 + h i) .
i=1,3,5,... i=2,4,6,...
Algorithmic Goals
Number
Representations
Root Finding
Suppose that instead of being given a function f (x), get Numerical
given some pairs of points that lie on f : (x1 , f (x1 )), Integration
(x2 , f (x2 )), (x3 , f (x3 )), (xn , f (x3 )). Interpolation
Algorithmic Goals
Number
Representations
Root Finding
Numerical
If n = 2, the only plausible estimate for f is as a linear Integration
combination: Interpolation
f (x2 ) f (x1 )
f1 (x) = f (x1 ) + (x x1 ) .
x2 x1
The function is fitted as a straight line that passes through
the two specified points.
COMP20005
Linear interpolation
Alistair Moffat
Algorithmic Goals
Number
Representations
When more that two points, an option is to construct a Root Finding
piecewise linear interpolation for xi x xi+1 , take Numerical
Integration
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
If wish f to be differentiable, fit a polynomial of degree n 1 Interpolation
through the n points.
Algorithmic Goals
Number
Representations
Numerical
f2 (x) = b1 + b2 (x x1 ) + b3 (x x1 )(x x2 )
Integration
Interpolation
since f2 (x2 ) = y2 .
COMP20005
Newton quadratic interpolation
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
Finally, substituting b1 and b2 , and considering x = x3 Interpolation
implies
y3 y2
y2 y1
b3 = x3 x2 x2 x1
x3 x1
since f2 (x3 ) = y3 .
COMP20005
Newton quadratic interpolation
Alistair Moffat
Algorithmic Goals
Number
Representations
To get a quadratic in the usual form f2 (x) = px 2 + qx + r , Root Finding
expand the original: Numerical
Integration
to determine that
p = b3
q = b2 b3 x1 b3 x2
r = b1 b2 x1 + b3 x1 x2
COMP20005
Example 1
Alistair Moffat
Algorithmic Goals
Number
Representations
Root Finding
Interpolation
xi yi
1.00 -2.50
1.50 -1.50
2.50 3.50
Algorithmic Goals
Number
Representations
Root Finding
Numerical
Integration
Consider the function Interpolation
1
g (x) = ln .
1+x
Find a quadratic approximation to g that matches g at the
points x {1, 2, 4}.
COMP20005
Newton polynomial interpolation
Alistair Moffat
Algorithmic Goals
Number
Representations
Numerical
Integration
F [i] = yi .
Interpolation
F [j, j 1, . . . , i + 1, i] =
F [j, j 1, . . . , i + 1] F [j 1, . . . , i + 1, i]
.
xj xi
Algorithmic Goals
Number
Representations
Numerical
Integration
fn (x) = b1 +
Interpolation
b2 (x x1 ) +
b3 (x x2 )(x x1 ) +
b4 (x x3 )(x x2 )(x x1 ) +
bn (x xn1 )(x xn2 ) (x x2 )(x x1 )
Algorithmic Goals
Number
Representations
Root Finding
The Newton polynomial is continuous and differentiable, but
Numerical
the function is likely to diverge rapidly outside the zone for Integration
Algorithmic Goals
Number
Representations
If the degree of the component polynomials is increased Root Finding
(from first degree linear), added freedom is added to the Numerical
Integration
solution space (compared to linear).
Interpolation
Algorithmic Goals
Number
Representations
A cubic spline is continuous at each knot point, and its first Root Finding
and second derivatives are also continuous. The two spare Numerical
Integration
coefficients are usually set by taking f00 (x1 ) = f00 (x2 ) = 0. Interpolation
The maths gets a (little) bit harder, and there are more
coefficients to be stored, plus (slightly) more complex
evaluation.