| 2. |
Complex Numbers
- Complex numbers are used to represent roots of quadratic equations that
involve taking the square root of a negative number.
- These numbers can be represented by using the letter i to represent
the square root of -1.
- Numbers that do not need to be expressed using i are known as
real numbers.
- Numbers that can be expressed as the product of a (non-zero) real number and
i are known as imaginary numbers.
- Numbers that can be expressed as the sum of a real number and an imaginary
number are known as complex numbers.
-
For example, the polynomial x2 + x + 1 has the
following roots (obtained by plugging in the coefficients 1, 1, and 1 into the
quadratic formula):
- (-1 + √-3)/2
- (-1 - √-3)/2
These roots can be represented as complex numbers as follows:
- -1/2 + √3 i/2
- -1/2 - √3 i/2
In both cases the -1/2 is called the real part, and the part with the
i (3i/2 for the first root, -3i/2 for the second root) is called
the imaginary part.
- In general, the roots of a quadratic equation are complex if the discriminant
(b2 - 4ac) is less than zero.
- If we break up the quadratic formula into two parts, it can be used the find the
real and imaginary parts of complex roots:
- The real part of both roots is -b/(2a).
- The coefficient of the imaginary part of the larger root is
(√-1 * d)/(2a)
where d is the discriminant.
- The coefficient of the imaginary part of the smaller root is
-(√-1 *d)/(2a)
where d is the discriminant.
|
| 4. |
Quadratic Equation Solver
- Now we are ready to implement a fully functional quatratic equation solver.
- You will need three text boxes for the three coefficients a, b,
and c.
- You will also need two text boxes for the two solutions to the quadratic
equation.
- You will need to write a function called ComputeRoots
to get the input values, parse them using parseFloat,
find the real roots if the discriminant is greater than or equal to zero,
find the complex roots if the discriminant is less than zero,
and write the roots to your output text boxes
- To keep this function as simple as possible, you should implement the
following functions to do your dirty work:
function Discriminant(a, b, c)
// Assumes: a, b, c are coefficients of a quadratic equation
// Returns: the discriminant of the quadratic equation
{
}
function RealRoot1(a, b, c)
// Assumes: a, b, c are coefficients of a quadratic equation with real roots
// Returns: the larger of the two roots of the equation
{
}
function RealRoot2(a, b, c)
// Assumes: a, b, c are coefficients of a quadratic equation with real roots
// Returns: the smaller of the two roots of the equation
{
}
function ComplexRoot1(a, b, c)
// Assumes: a, b, c are coefficients of a quadratic equation with complex roots
// Returns: the larger of the two roots of the equation as a complex object
{
}
function ComplexRoot2(a, b, c)
// Assumes: a, b, c are coefficients of a quadratic equation with complex roots
// Returns: the smaller of the two roots of the equation as a complex object
{
}
- Here are some good test cases:
- x2 - 4x + 13 has complex roots
2 + 3i and 2 - 3i
- x2 - 4x - 5 has real roots 5 and -1
- x2 - 4x + 4 has real roots 2 and 2
|