ANU The Australian National University



____________________________________________________

[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]

____________________________________________________

COMP2100/2500
Homework 4

Continue filling in a new Time Recording Log and Weekly Time Use Summary each week.

Write the following program, following the initial PSP and filling in the Project Plan Summary and a Defect Recording Log.

Write a program called ‘Roots’ that finds all solutions of a quadratic equation. The program must read three integers a, b and c from the command line. It must then print all roots of the quadratic equation ax² + bx + c = 0.

Here is an example interaction with the finished program:

[comp2100@partch]$ java Roots 1 0 -1
1x^2 + 0x + -1 = 0
1.0, -1.0
[comp2100@partch]$ java Roots 1 -2 1
1x^2 + -2x + 1 = 0
1.0, 1.0
[comp2100@partch]$ java Roots 1 0 1
1x^2 + 0x + 1 = 0
0.0 + 1.0 i, 0.0 - 1.0 i
[comp2100@partch]$ java Roots 1 0 -2
1x^2 + 0x + -2 = 0
x = 1.4142135623730951, x = -1.4142135623730951

The calculated values should be doubles.


Hints

The quadratic formula for the roots of the quadratic equation ax²+bx+c=0 is

x = (-b ± (b² - 4ac)½) / 2a

If the discriminant b² - 4ac is greater than zero, then the roots will be real and different. If it is equal to zero, then the roots will be real and equal. If it is negative then they will be complex and different, and you will have to calculate the real and imaginary parts separately.

If you have really never seen complex numbers before and have no idea what I mean about complex roots, real and imaginary parts etc, then you may modify the specifications. If the discriminant is negative, your program should just print the message: “No real roots”. But if you know what this is about, don't be slack: do the whole thing properly.


Extension work

Using the quadratic formula as suggested is numerically unsatisfactory. If a or c (or both) is small, then one of the roots will involve subtracting b from a very nearly equal quantity (the discriminant), leading to catastrophic roundoff error and thus serious inaccuracy.

The right way to do it is to first calculate

q = - (b + sgn(b) (b² - 4ac)½).

(Here “sgn” is the signum or sign function: if its argument is negative, its value is -1, if its argument is positive, its value is +1.)

Then the roots are

x = q/a and x = c/q.

Rewrite your program to use this method, or better still, do a comparison of the results of the two methods. You might want to allow doubles as the coefficients rather than restricting them to be integers if you do this.

For more on this and related questions, look at Numerical Recipes by Press, Flannery, Teukolsky and Vetterling (Cambridge University Press) or any other good numerical analysis textbook.

____________________________________________________

[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]

____________________________________________________

Copyright © 2005, Ian Barnes, The Australian National University
Version 2005.1, Friday, 11 March 2005, 15:27:29 +1100
Feedback & Queries to comp2100@cs.anu.edu.au