0% found this document useful (0 votes)
24 views

Lect 3 Errors

This document provides examples and discussion of numerical errors and user errors that can occur when performing calculations. It discusses different types of numerical errors such as absolute error and relative error. It also discusses common sources of user errors such as typographical mistakes, incorrect use of operators or variables, syntactical errors, mathematical errors in algorithms, and logical errors. The document emphasizes that eliminating user errors is critical for accurate results, and that in practice user errors are usually more significant than numerical errors. It provides examples calculating errors and debugging code to address user errors.

Uploaded by

Alaa Fj
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)
24 views

Lect 3 Errors

This document provides examples and discussion of numerical errors and user errors that can occur when performing calculations. It discusses different types of numerical errors such as absolute error and relative error. It also discusses common sources of user errors such as typographical mistakes, incorrect use of operators or variables, syntactical errors, mathematical errors in algorithms, and logical errors. The document emphasizes that eliminating user errors is critical for accurate results, and that in practice user errors are usually more significant than numerical errors. It provides examples calculating errors and debugging code to address user errors.

Uploaded by

Alaa Fj
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/ 5

6/3/2022

Lecture 3

By: Zahra Elashaal

• 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.10 Suppose an error of £1 is made in a financial calculation of interest on £5 and on


£1,000,000. In each case the absolute error is £1 (|𝑥 -𝑥|
ො = |5 − 4 = 1 and |1,000,000 – 999,999|=1),
whereas the relative errors are 20% and 0.0001% respectively. (|𝑥 -𝑥|/|𝑥|ො = 1/5*100=20% and
1/1000000 * 100 = 0.0001%
Example 2.11 Suppose a relative error of 20% is made in the above interest calculations on £5 and
£1,000,000. The corresponding absolute errors are £1 and £200,000.

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).

Example 2.13 Determine a value of x such that f(x) = x2 + 4x = 40.

We start by guessing that x = 6 is the root we require:


• x = 6, f(6) = 60 > 40 which is too big, try x = 5.
• x = 5, f(5) = 45 > 40 which is still too big, try x = 4.
• x = 4, f(4) = 32 < 40 now this is too small, so we shall try x = 4.5.
• x = 4.5, f(4.5) = 38.25 < 40 a bit too small, try x = 4.75
• x = 4.75, f(4.75) = 41.5625 > 40 a bit too big, back down again to x = 4.625
• x = 4.625, f(4.625) = 39.890625 < 40 a bit too small, back up again to x = )4.625 + 4.75(/2
• x = 4.6875, f(4.6875) = 40.72265625 > 40 and we can continue this process.

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).

• 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.
Once all the syntax errors have been eliminated within a code, the next level of errors are harder to
find. These are usually due to:
1. An incorrect use of variable names. 4. Mathematical errors incorporated into the
This may be due to a typographical error which numerical scheme the code seeks to implement.
has resulted in changes during the coding. These usually occur where the requested calculation
2. An incorrect use of operators. is viable but incorrect.
The most common instance of this error occurs 5. Logical errors in the algorithm.
with dot arithmetic.
This is where an error has occurred during the
3. Syntactical errors which still produce feasible coding and we find we are simply working out what
MATLAB code. is a wrong answer.
For instance in order to evaluate the expression
cos x, we should use cos(x): • Practice is needed to Avoiding all of these errors.
and usually after quite a lot of frustration.

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?

You might also like