Lect 3 Errors
Lect 3 Errors
Lecture 3
• Numerical Errors
Occurs as the numerical methods are usually not exact (that is, they are approximate methods).
Ex: the notation 1/3 ≈ 0.3333. Obviously the more three’s we retain the more accurate the
answer.
Errors can be expressed as two basic types:
Absolute error: This is the difference between the exact answer and the numerical answer.
Relative error: This is the absolute error divided by the size of the answer (either the
numerical one or the exact one), which is often converted to a percentage.
• Let 𝑥
ො be some approximation of 𝑥. So that:
• User Error
• The elimination of user error is critical in achieving accurate results.
• In practice user error is usually more critical than numerical errors, and after some
practice their identification and elimination becomes easier.
• User error is avoidable, whereas many numerical errors are intrinsic to the numerical
techniques or the computational machinery being employed in the calculation.
1
6/3/2022
Example 2.12 Estimate the error associated with taking 1.6 to be a root of the equation
x2 − x − 1 = 0.
• The exact values for the roots are (1±√5)/2 (let us take the positive root). As such the absolute error is:
• and the relative error is the absolute error divided by the value 1.6 (or alternatively the exact root)
which is approximately equal to 0.01127124296868 or 1.127%.
• We could also substitute x = 1.6 into the equation to see how wrong it is: 1.62 − 1.6 − 1 = −0.04.
Although it is difficult to understand how this can be used, it is often the only option (particularly if
the exact answer cannot be found).
Here we have just moved around to try to find the value of x such that f(x) = 40,
but we could have done this in a systematic manner (actually using the size of the
errors).
2
6/3/2022
The MATLAB variable eps is defined as the smallest positive number such that 1+eps is x1 = 1+eps;
different from 1. eps, with no arguments, is the distance from 1.0 to the next larger double y1 = x1-1
precision number, that is eps with no arguments returns 2^(-52) = 2.2204e-16. x2 = 1+eps/2;
Consider the calculations: y2 = (x2-1)*2
• In both cases using simple algebra you would expect to get the same answer, namely eps;
• but in fact y2 = zero. This is because MATLAB cannot distinguish between 1 and 1+eps/2.
• The quantity eps is very useful, especially when it comes to testing routines.
Example 2.14 Calculate the absolute errors associated with the following calculations: sin(15*pi)
To calculate the absolute errors we need to know the exact answers which are (sqrt(2))ˆ2
0, 2, 1 and 1 respectively. We can use the code: 0.001*1000
1e10*1e-10
abs(sin(15*pi)-0) notice in the last case the exponent form of the number
abs((sqrt(2))ˆ2-2) takes precedence in the calculation: we could of course
abs(1000*0.001-1) make sure of this using brackets). The errors are 10−15,
abs(1e10*1e-10-1) 10−16 and zero (in the last two cases).
3
6/3/2022
This code purports to obtain three numbers a, b and c from a user and then produce the results
a + b + c, a/((b + c)(c + a)) and a/(bc).
The corrected code looks like:
Debag the next code clear all
a = input(’ Please entere a ) a = input(’ Please enter a ’);
b = 1nput(’ Please enter b ) b = input(’ Please enter b ’);
a = Input ’ Please enter c ’ c = input(’ Please enter c ’);
v1 = a+ B+d v1 = a+b+c;
v2 = a/((b+c)(c+a) v2 = a/((b+c)*(c+a));
v3 = a/b*c v3 = a/(b*c);
disp(’ a + b + c= ’ int2str(v1)]) disp([’ a + b + c= ’ num2str(v1)])
disp([’v2 = ’ num2str v2 ] disp([’ v2 = ’ num2str(v2)])
disp([’v3 = num2str(v4) ]); disp([’ v3 = ’ num2str(v3)])
• now work through the lines one-by-one detailing where the errors are and how they can be fixed.
( Tasks Home Work)
4
6/3/2022
Any Question?