MAT005 Lab Exercise 3
MAT005 Lab Exercise 3
Objectives:
1. Demonstrate how round-off and truncation errors are generated using MATLAB
2. Calculate the round-off and truncation errors using MATLAB
Demonstration:
1. The magnitude of round-off errors depends on the magnitude of the numbers that are
involved since the interval between the numbers that can be represented on a computer
depends on their magnitude. Round-off errors are likely to occur when the numbers that
are involved in the calculations differ significantly in their magnitude and when two numbers
that are nearly identical are subtracted from each other. For example, consider the
quadratic equation:
for which the exact solutions are x1 = 100 and x2 = 0.0001. The solutions can be calculated
with the quadratic formula:
RootDis =
99.999899999999997
xl =
100
x2 =
1.000000000033197e-04
The value that is calculated by MATLAB for x2 is not exact due to round-off errors.
Calculating the round-off error (re) for x2 in MATLAB, the result is:
>> re = 0.0001 - x2
re =
-3.319650869297586e-15
The round-off error occurs in the numerator in the expression for x2 Since b is negative, the
numerator involves subtraction of two numbers that are nearly equal. In many cases, the
form of the mathematical expressions that contain subtraction of two quantities that are
nearly equal can be changed to a different form that is less likely to cause round-off errors.
In the expression for x2, this can be done by multiplying the expression by
x2Mod =
1.000000000000000e-04
Calculating the round-off error (re) for x2 in MATLAB, the result is:
re =
2. Truncation errors occur when the numerical methods used for solving a mathematical
problem use an approximate mathematical procedure. A simple example is the numerical
evaluation of sin(x), which can be done by using Taylor's series expansion
The value of sin (π/6) can be determined exactly with the equation above if an infinite
number of terms are used. The value can be approximated by using only a finite number
of terms. The difference between the true (exact) value and an approximate value is the
truncation error, denoted by ETR. For example, if only the first term is used:
If two terms of the Taylor's series are used:
In MATLAB, we can calculate the exact value of sin (π/6), the truncated value (tv) up to
three terms, and the truncation error (te) as follows:
>> sin(pi/6)
ans =
0.500000000000000
>> x = pi/6;
>> tv = x - x^3/(3*2*1)+ x^5/(5*4*3*2*1)
tv =
0.500002132588792
>> te = sin(pi/6) - tv
te =
-2.132588792502776e-06
Exercises:
Use MATLAB to calculate the value of f(x) (use the variable name fx) for x = 1000.
(2 points)
Use this result to calculate an improved value of the function f(x) (use the variable
name fxmod) and calculate the round-off error (use the variable name re) which is
equal to the difference fxmod - fx. (3 points)
c. Use MATLAB to calculate the value of cos (π/4) (use the variable name cosx). Then,
use the Taylor’s series expansion of f(x) = cos x, as shown below, to calculate a
truncated estimate or value for cos (π/4) using up to three terms only of the series (use
the variable name cosxt).
Lastly, calculate the truncation error or the difference cosx – cosxt (use the variable
name te). (5 points.)
------------------------------------------NOTHING FOLLOWS------------------------------------------