Physics 3730/6720 Assignment 5

Reading and references:

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


Exercise 1. Complex roots of a polynomial

Write a program polyroot in the same spirit as findroot in Assignment 4. In this case the program is to find a possibly complex root of an arbitrary polynomial with real coefficients, using the Newton-Raphson method together with the C++ built-in complex class. The polynomial and its derivative are to be evaluated using Horner's algorithm.

For representing complex numbers, you must start from the header file <complex>. For convenience you may define a shorthand double complex type dcmplx using

   typedef complex<double> dcmplx;
The organization of the code should be very similar to the findroot code. That is, create a main program in a file called polyroot.cc that reads the order of the polynomial, the polynomial coefficients, the starting value (complex) and the tolerance. Note that this file is similar to findroot.cc in Assignment 4. The main program should then call a subprogram called polynr in the file polynr.cc that runs the Newton-Raphson iteration. Then it should write the answer as long as everything is OK.

The subprogram polynr belongs in a file called polynr.cc, which is similar to nr.cc in Assignment 4. It runs the Newton-Raphson iteration. It should call the subprogram horner to evaluate the polynomial and its derivative.

The subprogram horner belongs in a file called horner.cc. This file is analogous to f.cc in Assignment 4.

For this exercise you are to hand in four files polyroot.cc, polynr.cc, horner.cc, and Makefile.

Here are more details

The order of standard input must be exactly as follows:

   N                    (order of the polynomial - i.e. highest power) 
   a[0]  a[1] ... a[N]  (coefficients of x^0, x^1, ... x^N in that order)
                        
   tol                  (how close in complex absolute value you should come)
   (Rex, Imx)           (real and imaginary parts of starting point)

To read a dcmplx variable, use cin the same way your would use it with a plain double. It accepts either a single real value or a complex number in the format (Re, Im), that is, with the parentheses and comma exactly as shown.

To write a dcmplx value, use cout in the natural way. The number is printed in the format (Re, Im), same as the input format.

Note that your subprograms require two more arguments than you had for findroot, namely, the order of the polynomial and its coefficients.

Note that the trial solution variable, the value of the polynomial, and the value of its derivative must all be of type dcmplx.

The stopping condition is that the absolute value of the change in the estimated solution is less than the tolerance. The abs function accepts a complex argument.

The Makefile should allow us to create your code with the command

   make polyroot
and run your program with the command
   polyroot

Exercise 2. Running your program

Test your program by finding all the roots of the polynomial x^3 + 7*x^2 + x + 2 to within a tolerance of 1e-5. Put a copy of the console session with your input and output in a file testpoly.txt. You may use Maple (or equivalent) to check your answers.

Submit testpoly.txt.


This page is maintained by:
Carleton DeTar Mail Form
Last modified 13 October 2009