CS 1 - Lab 10

Outcomes:

Upon completion of the lab, the student will know how to:

1.

For this lab you will implement complex number objects, and use them to build a more complete quadratic equation solver.

  • Create an HTML file for this lab assignment called lab10.html (See the instructions for Lab 2 if you are unsure how to do this.)

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.

3.

Javascript Objects

  • Javascript has no built in support for complex numbers, but we can use objects to represent them.
  • Using the techniques described in class, (see this tutorial for a summary) create a template function for a complex number object (call it complex)
  • Our complex object should have two properties:
    • rp is the real part
    • cip is the coefficient of the imaginary part
  • Using your constructor you should be able to create a new complex number by calling the template function:
    complexRoot = new complex(3, -4);
                      
  • We will also need a method to convert the complex number to a string (call this method toString).
  • This method should take no arguments and return the string equivalent of the rational number.
  • For example, using complexRoot as defined above the following statement:
    complexRootString = complexRoot.toString( );
                      
    should set the value of complexRootString to the string "3 - 4i".
  • You might want to test this out using text boxes before you continue to the next part of the lab.

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

5.

When you are done, upload the lab10.html file to the CS server using FileZilla. (See the instructions for Lab 1 if you are unsure how to do this)