0% found this document useful (0 votes)
215 views11 pages

P03 Intro To Numerical Methods

This document discusses numerical methods in MATLAB including floating point representation and round-off error. It provides examples of calculating with floating point values and demonstrates how small changes can result in different outputs due to machine epsilon. Methods for estimating errors like truncation error and techniques for evaluating problem conditioning are also presented.

Uploaded by

Sheeraz Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
215 views11 pages

P03 Intro To Numerical Methods

This document discusses numerical methods in MATLAB including floating point representation and round-off error. It provides examples of calculating with floating point values and demonstrates how small changes can result in different outputs due to machine epsilon. Methods for estimating errors like truncation error and techniques for evaluating problem conditioning are also presented.

Uploaded by

Sheeraz Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Introduction to numerical methods

Floating-point representation, round-off error, machine epsilon


Before working on more complex problems, it worth knowing how calculations are done in MATLAB:

x1= 0.3

x1 = 0.3000

x2= 0.1 + 0.1 + 0.1

x2 = 0.3000

The two numbers are obviously equal. Or are they?

x1==x2

ans = logical
0

The last process (==) is a logical operator, that checks whether on both side of the operator are the same
values or not. The result is True(1) or False(0). You would say that these values are the same, therefore the
result is True, but because of the rounding error, it is False.

The computers store numbers in various ways, one of them is the floating-point form (that is used by MATLAB
too) with double precision. This means the following in 64 bit in binary:

where s is the sign bit (1 bit), m is the mantissa (the significant digits stored in 52 bit) and e the exponent (11 bit)
(s+m+e=>1+11+52 = 64 bit).

You can't represent precisely all the real numbers (beacuse there are infinite) as you saw e.g. 0.1 in binary is an
infinite fractional decimal (in this case binary) fraction, from which we only use the first 52 bits, the rest will be
discarded. That will be the round-off error:

– 0.000110011001100110011...

Lets see some similar errors/curiosities:

1
What would be the result of ? The tangent function is undefined at π/2, it converges to infinity. What will

be the result in MATLAB?

tan(pi/2)

ans = 1.6331e+16

The reason why you still got a result is again the round-off error. The size of round-off error ( ) could be
described with the machine epsilon ( ). In numerical calculation 1+ =1, if < . In MATLAB the machine
epsilon is stored in the following variable:

eps

ans = 2.2204e-16

This is the difference between two consecutive numbers that can be represented with the floating-point form.
Lets add 1* to a value:

a=1

a = 1

c=1e-17

c = 1.0000e-17

Let's compare if a is equal to a+c!

a==a+c

ans = logical
1

The result is true, as the number added was less then machine epsilon. Now lets add 1* to the same
value, and compare the result!

d = 1e-14

d = 1.0000e-14

a==a+d

ans = logical
0

The result is false, as we expected. Now lets try with a different number instead of a, use b = 12345!

b = 12345

b = 12345

b == b+d

2
ans = logical
1

According to the previous material, you should see a False result, so b+d is not equal to b, because d was
larger than the machine epsilon. Though the result is different, because the basic number for machine epsilon is
meant to the value of 1, but it changes depending on the numbers you are working with.

eps(12345)

ans = 1.8190e-12

eps(1e20)

ans = 16384

This means the scale determines what kind of the values can be handled. You can't add to a number that has a
scale of another number that is less than 16384/2, because in this case the number will remain the same!

1e20 == 1e20 + 16384/2

ans = logical
1

1e20 == 1e20 + 9000

ans = logical
0

Lets have a look on another typical error, the cancellation error, when we subtract almost equal numbers from
each other:

x1 = 10.000000000000004

x1 = 10.0000

y1 = 10.00000000000004

y1 = 10.0000

(y1-10)/(x1-10) % expected result: 10

ans = 11.5000

You saw already what kind of difference you can see between measurments, but in what range can you even
work with numbers:

realmin

ans = 2.2251e-308

realmax

ans = 1.7977e+308

Truncation error

3
Another important error occures when we use in our calculation a numerical approximation instead of the exact
mathemathical expression e.g. in case of applying Taylor-series, numerical derivation, integration. Lets calculate
the value of the function at x=1 using the built-in MATLAB function, and the Taylor-series, applying

only the first four terms:

x=1

x = 1

f = exp(x)

f = 2.7183

g = 1 + x + x^2/2 + x^3/factorial(3)

g = 2.6667

The difference between the two, is the truncation error. This can be decreased by adding more terms to the
expressin.

Total error = round-off error + truncation error

Absolute and relative error


Till this time we only saw the difference from the exact value i.e. the absolute error. Though there are two

different type of errors: the absolute and the relative error. Let be an approximation of a real number x.

In this case the absolute error is, the difference from the exact value:

The relative error is:

So the ratio of the absolute error and the exact value. If the value of x is around 1, there are no significant
difference, but in other cases the relative error describes far better the scale and impact of the error.

Stability and condition number


We saw that our calculataions are usually containing the numerical errors, but what can we do about them?Can
we rely on our results then? Let us know two new expression, that describes the sensibility/condition of a
specific problem, and the stability of an algorithm:

• A mathemathical problem is good conditioned, if a slight change in the input parameters causes a slight
change in the result too.
• An algorithm is numerically stable, if a slight change in the input parameters causes a slight change in
the result too.

4
If these are not true the problem is more likely to be ill-conditioned or weakly conditioned, and this will tend to
magnify the uncertainties in the computations. Therefore the condition number of a problem has a significant
impact on the solutions precision.

If you apply a stable algorithm on an ill-conditioned problem, or apply an instable algorithm on a good
conditioned system, it can cause inaccuracy in the result.

Condition number
Lets have a look on a system of linear equations:

This looks like the following using matrices:

The conventional form of a linear system of equation is: , where the solution is: .

Lets solve this numerically and symbolically in MATLAB!

The numerical solution using built-in methods:

A = [6,-2; 11.5, -3.85]

A = 2×2
6.0000 -2.0000
11.5000 -3.8500

b= [10; 17]

b = 2×1
10
17

x = inv(A)*b % 45; 130

x = 2×1
45.0000
130.0000

norm(A*x-b)

ans = 0

A = [6,-2; 11.5, -3.84]

A = 2×2
6.0000 -2.0000
11.5000 -3.8400

x = inv(A)*b % 110; 325 instead of 45; 130!

5
x = 2×1
110.0000
325.0000

norm(A*x-b)

ans = 3.2155e-13

This means our results are good, because the norm of the difference vector is zero.

Check the condition number:

cond(A)

ans = 4.6749e+03

This is quite a big number. That means the system is ill-conditioned. The condition number (κ) gives you the
connection between the relative error of the output and the relative error of the input. If this number is large, that
means a slight change in the input variables can cause a far larger difference in the outputs.

an example for an ill-conditioned problem:

Stability
Lets have a look on two different algorithm, to calculate the value of function !

6
Calculate the function value at x = 8.3 ! Lets write a function with two output value, one for each algorithm with a
defined number of terms at a specific location of x! You can use a for loop for this!

[f g] = emx(8.3,10)

f =
1.880344210134851e+02
g =
3.165680237758456e-04

[f g] = emx(8.3,20)

f =
0.283258665911793
g =
2.485550774829941e-04

[f g] = emx(8.3,30)

f =
2.515068435459952e-04
g =
2.485168274212610e-04

You see, that the second algorithm converges to the exact solution far faster, that the other. This means, it
doesn't matter what kind of algorithm do you use to solve a specific problem!

Unwanted numerical effect


• loss of significance (a – b ≈ 0) (Caused by a “bad subtraction,” which means a subtraction of a number
from another one that is almost equal in value); cancellation, when the individual sub-results are far
larger, than the result itself (∑ai << |ai|)
• numerical instability
• ill-conditioned systems

Total error
Let’s look at an example where both truncation and rounding errors play a role. Not only does the Taylor
serial approximation contain a truncation error, but also the numerical integration, or the approximation of the
derivative with a difference quotient. Let's look at an example of the latter:

Let's consider the following function:

Compute the derivative at the location x=2.

Using numerical approximation:

7
The truncation error could be approximated via Taylor-series:

, where . After rearrangment:

On the left side of the above equation we find the truncation error (the difference between the approximation
and the actual value), and on the right side there is an upper bound for this, which will certainly be less than the
error.

Denote the error with H and the upper bound with . In our case we know for sure the first ( ) and
the second derivative ( ), thus, the following is true for the truncation error:

Estimated upper limit of the truncation error as a function of h at maximum ( ), at the location :

It can be seen from the above formula, as expected, that the smaller the intervals h divided into the calculation
of the difference quotients, the smaller the truncation error will be.

Plot the estimated upper bound of the approximation of the previous derivative and also calculate the actual
error as a function of the step change. Take the following steps:

format long
n = 0:-1:-15

n = 1×16
0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12

h = 10.^n

h = 1×16
1.000000000000000 0.100000000000000 0.010000000000000 0.001000000000000

e = @(h) 3*(2+h).*h; % Approximated upper bound of the error


H = @(h) abs(((2+h).^3 - 8)./h - 12); % Actual error
figure(1)
loglog(h,e(h)); hold on;
loglog(h,H(h))
legend(' Approximated upper bound of the error','Actual error')

What do we see in the figure above? As the step size decreases, so does the estimated upper limit of the
truncation error, and for a while, the actual error decreases accordingly, but after a point (somewhere around

8
10-8) the exact error suddenly starts to increase again. What could be the reason for this? This is because the
total error occurs as the sum of the truncation and rounding errors. The truncation error dominates up to about
10-8, after which the rounding error starts to increase strongly. This phenomenon plays a significant role in the
numerical solution of differential equations, for example!

Of course, similarly to what we saw earlier, we can choose an algorithm that is less sensitive to rounding error,
since the main error here is the extinguishing error, because in the difference quotient counter we subtract two
nearly identical numbers from each other, the smaller h, the smaller the two numbers the difference between.
Now we can easily simplify the difference quotient:

Of which truncation error:

H2 = @(h) abs(6*h+h.^2);
fgr1 = loglog(h,H2(h),'g--','LineWidth',2)

fgr1 =
Line (data1) with properties:

Color: [0 1 0]
LineStyle: '--'
LineWidth: 2
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1 0.100000000000000 0.010000000000000 1.000000000000000e-03 1.000000000000000e-04 9.9999999999
YData: [7 0.610000000000000 0.060100000000000 0.006001000000000 6.000100000000001e-04 6.00000999999999

Show all properties

legend(fgr1,'Total error - after correction')

9
Here we have simplified the difference quotient manually; of course, there are also possible simplifications in
Matlab, but these are already in the topic of symbolic calculations, which is part of the Symbolic Math Toolbox.

In symbolic calculations, we do not use specific numbers, but variables. To do this, we first need to tell Matlab
with the syms command what our symbolic variables will be; we need to call the function with these symbolic
variables, then we can simplify the expression with the simplify command. The result of the simplify command
will be a symbolic variable into which you cannot substitute specific numbers unless you convert the expression
back to a traditional function with the matlabFunction command.

syms h
Hsym = simplify(H(h)) % Hsym = abs(h*(h+6))

Hsym =

You can also see in Workspace that the value of Hsym is symbolic. Let’s see what happens when we try to
substitute a specific value!

%Hsym(1e-5)
H2 = matlabFunction(Hsym)

H2 = function_handle with value:


@(h)abs(h.*(h+6.0))

H2(1e-5)

ans =
6.000010000000000e-05

10
function [f g] = emx(x,n)
f = 1; % the first approximation 1 - x + x^2/2 - x^3/6 + ...
p = 1; % the first approximation for the denominator (1 + x + x^2/2 + x^3/6 + ...)
for i=1:n
s = x^i/factorial(i);
f = f +(-1)^i*s;
p = p + s;
g = 1 / p;
end
end

11

You might also like