# Physics 3730/6720 Assignment 2

For each of the following exercises create the specified files with your answer(s). Submit your homework using the course submit utility.

In this exercise you are to improve a program for computing the roots of a quadratic equation ax2 + bx + c = 0 and in the next exercise, use it to explore round-off errors. I have a version in ~p6720/examples/python/quadratic.py. Copy this program, but modify it as follows:

1. Insert a test to cover the special cases a = 0 and a = b = 0. (Here it would be nice, but not required, to have your code write the solution to a linear equation.)
2. Insert a test to quit if the roots are not real.
3. Compute the roots using the standard formula
```     x = [-b +/- sqrt(b^2 - 4*a*c)]/(2*a)
```
and using the alternative formula
```     x = 2*c/[-b -/+ sqrt(b^2 - 4*a*c)]
```
Note: You might want to make the code exit before it reaches the end of the program. For example, if the roots are complex, you might exit with an error message. To do that you use the Python sys.exit(1) command. Also add import sys to the top of the code to give you access to this exit procedure. You should compare the second formula (by hand algebra) with the standard one and notice that the root with the plus sign before the square root in one formula gives the same root as with the minus sign in the other. (Nothing to hand in.) By choosing the better formula carefully for each root you can avoid any round-off errors. Write all four answers, so you can compare them. Have your program label the two more reliable roots "robust" and the two less reliable roots "risky".
4. In the second formula you might be dividing by zero. There are still two valid roots. So handle this special case as well.
Hand in your revision of the file quadratic.py . We will test the program with our own inputs.

#### Exercise 2. Roundoff error

(a) In the conventional formula, when b is positive, which sign in front of the square root leads to a possible loss of significant digits? When it is negative, which?

(b) A large loss of significance occurs when you subtract two numbers that are almost equal. In each case the two numbers are b and sqrt(b^2 - 4*a*c). What simple mathematical condition can you put on a, b, and c that would cause the magnitudes of these two numbers to be very close?

(c) Then run your program for the cases

```     a = 0.02, b = 2, c = 4
a = 2e-8, b = 3, c = 5
a = 2e-8, b = 8, c = 1e-7
```
Put your results in a text file roundoff.txt. Identify which of the pair of results is the correct answer. Then explain the differences in the two sets of results in terms of round-off error. Explain also the size of the round-off error in the poorly evaluated roots.

NOTE: It is not sufficient to give only numerical results here. You must also give a clear EXPLANATION of those results. Your explanation should say why the risky formula gives such a bad result in the worst case above, and why it gives such a good result in the best case above. Say how you could predict which case would give the worst result.

Submit the file roundoff.txt.