2. Systems of Linear Equations and Floating-Point Errors

§2.1 Floating Point Limitations

Exercise 2.1
Include full-sentence response. (a)  Before entering the following commands into Matlab, take a minute and try to describe what this short program is doing.  Do you expect it to stop at some point?  Why or why not?
>> x = 1; i = 0;
>> while x > 0
x = x/2
i = i + 1;
end
Include input and output. Include full-sentence response. (b)  Enter the commands above into MATLAB.  If you run into trouble while entering the commands, Ctrl-C will bring you back to the prompt.  You will get a long output; a line for each time the program executesx = x/2.  What happened?  Did the program terminate? 

    Of course, in theory, dividing a number indefinitely by 2 will never result in a value at or below zero.  In practice, however, there appears to be a limit to how small numbers can get in MATLAB.

Include input and output. Exercise 2.2  From the results of the program above (i.e. the final value of i), can you give an estimate of the smallest positive numerical value MATLAB can store (maybe a range it falls into)? (Note: by just typingi and hitting enter, you'll get the last value ofi.)

    Let's look at another example of the limitations to which floating point numbers are subject.  Let's look at the sum (also called a 'series'):

       

What if we wanted to know the difference between the value of this series, when x = 1, after 38 terms and its value after 41 terms?  Let's look at the sum of the first 38 terms of the expansion at x = 1.  Enter this:

>> x = 1; Sum_38 = 1;
>> for n = 1 : 38
Sum_38 = Sum_38 + (x^n)/factorial(n);
end

Exercise 2.3
Include input and output. (a)  After entering the program above, run a similar program to find the value of the sum of the first 41 terms (call itSum_41).  (Hint: In the four places the program above has a '38', change it to '41'.)  Now find the difference, >> Sum_41 - Sum_38.  What do you find?

Of course, the value of this series after 38 terms is not the same as the value of the series after 41 terms, even though Matlab told us the difference between them was zero.  This time, MATLAB's error was not due to a number being too small (as in the first example), but rather has to do with where the significant figures are in the numbers being added.  Now let's switch our attention to errors that can come up in linear algebra.
 

§2.2 Systems of Linear Equations

    By now we have seen how a system of linear equations can be transformed into a matrix equation, making the system easier to solve.  For example, the system

can be written the following way

Now, by augmenting the matrix with the vector on the right and using row operations, this equation can easily be solved by hand.  However, if our system did not have nice integer entries, solving the system by hand using row reduction could become very difficult.  MATLAB provides us with an easier way to get an answer.

    A system of this type has the form Ax = b, so we can enter these numbers into MATLAB using the following commands:

>> A = [2 -1 1; 1 2 3; 3 0 -1];

>> b = [8; 9; 3]

(Notice that for the column vector b, we include semicolons after each entry to ensure that the entries are on different rows; the command

 >> b = [8 9 3]

would produce a row vector, which is not the same thing.)

    The command

>> x = A\b

will find the solution (if it exists) to our equation Ax = b.  In this case, MATLAB tells us

x =

    2.0000
   -1.0000
    3.0000

Exercise 2.4
Do work by hand. (a)  Consider the system of equations:

       
On paper, convert this system of equations into a matrix equation of the formCx = d.

Do work by hand. (b)  Again on paper, form the augmented matrix [C d] and use row operations to find its reduced row echelon form, and find the solution x to the system of equations.
Include input and output. (c)  Enter the matrix C and the column vector d into MATLAB, and use the command

>> x = C\d 

to check your solution.

Include input and output. (d)  We would expect to get the column vector d in MATLAB if we ran the command C*x, right?  In other words, C*x - d should be zero.  Enter this equation into MATLAB:

>> C*x-d 

What do you get?

    The discrepancy in part (d) of the above exercise is simply due to rounding error.  You'll notice that the error is a vector multiplied by a very small number, one on the order of 10-15.  But why is there any error at all?  After all, solving by row reduction gave very nice numbers, right?  The reason is that MATLAB doesn't solve this problem the way you did.  Instead, MATLAB uses complicated calculations to find the inverse of C,  C-1, then finds the answer by the formula x = C-1*d.  Some error is introduced when calculating C-1.

§2.3 Effects of Errors

    So far, the error that we encounter appears to be extremely small.  Will it really make a difference in a real-world application?  Often the answer is no, but it is important to be aware of the difference even a small error can make.  For one thing, if a problem requires many calculations and each calculation introduces a small, seemingly insignificant error, the errors can compound through the calculations until the small errors have become very large.  But even a tiny error in a single calculation has the potential to cause big problems given the right (or wrong) circumstances. 

    For example, consider a linear system given by

    ax + y = 3.5
    bx + y = 3.0

and suppose through MATLAB calculations we have determined that a and b are both within 0.001 of 0.27; i.e. a = 0.27 ± 0.001 and b = 0.27 ± 0.001.  How much difference can an error of ±0.001 make?  We know that

0.269 ≤ a ≤ 0.271 and
0.269 ≤ b ≤ 0.271

so let's just test the extremes.

Exercise 2.5
Include input and output. (a)  Use MATLAB to solve for x and y when a = 0.269 and b = 0.271.  (i.e., set up a matrix and column in Matlab as in Exercise 2.4 for the system of equations above after substituting these values for a and b.)  Then do the same for when a = 0.271 and b = 0.269.  Include your input as well as your answers in your write-up. 
Include full-sentence response. (b)  How big a difference did the small error make; i.e. how far apart are these two points on the xy plane?

§2.4 Conclusion

    Floating-point errors are an inherent part of computer calculations and knowing how to handle them is necessary in any field that uses computers to model real-world scenarios.  Error analysis now is an important and active area of mathematical study because, in practice, problems can arise very quickly if even small errors are not kept in check. 


Last Modified:

09/26/04