Work on this in groups!
Write a Fortran program to compute the roots of a quadratic equation and determine the absolute and relative error in the roots computed. The program should do the following:
So you should be able to do something like this:
$ gfortran quadratic.f90
$ ./a.out
input x1true, x2true:
2.5, 3
Coefficients: a = 0.100000E+01 b = -0.550000E+01 c = 0.750000E+01
Root x1 computed: 0.250000000000000E+01 true: 0.250000000000000E+01
absolute error: 0.000000E+00 relative error: 0.000000E+00
Root x2 computed: 0.300000000000000E+01 true: 0.300000000000000E+01
absolute error: 0.000000E+00 relative error: 0.000000E+00
Don’t worry too much about the formatting but you might want to print out 15 digits in the computed roots.
You might want to assume the values are entered with x1true <= x2true so you know which root from the quadratic equations goes with which original value. (And print out an informative error message otherwise.)
Test a variety of values to see that it’s working.
Once it’s working on reasonable values, try the following:
In each case you should find that one root is computed accurately but the other root has a large relative error (few digits of accuracy).
Figure out why “catastrophic cancellation” is the problem.