home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 177.lha / Newton_v1.0 / main.c < prev    next >
C/C++ Source or Header  |  1988-04-28  |  2KB  |  68 lines

  1. /* main.c:    Main "Newton" program.
  2.  *
  3.  * Written by Daniel Barrett.  100% PUBLIC DOMAIN. */
  4.  
  5. #include "decl.h"
  6.     
  7. main(argc, argv)
  8. int argc; char *argv[];
  9. {
  10.     complex polynomial[MAX_DEGREE+1];    /* Our polynomial.   */
  11.     int degree;                /* Its degree.       */
  12.     double epsilon;                /* Desired accuracy. */
  13.  
  14. #ifdef UNIX
  15.     srand48((long)123);
  16. #endif
  17.  
  18.  
  19.     epsilon    = 0.0;
  20.     degree    = 0;
  21.  
  22.     Version();
  23.     InitPoly(polynomial, MAX_DEGREE+1);
  24.     ReadPoly(polynomial, °ree, &epsilon);
  25.     PrintPoly(polynomial, degree);
  26.     NewtonMethod(polynomial, degree, epsilon);
  27. }
  28.  
  29.  
  30. NewtonMethod(poly, degree, epsilon)
  31. /* Find the roots using Newton's Method. */
  32. complex poly[];
  33. int degree;
  34. double epsilon;
  35. {
  36.     int numRoots=0, iterations;
  37.     complex zero, oldGuess, newGuess, polyCopy[MAX_DEGREE+1];
  38.     BOOL ErrorTooBig();
  39.  
  40.     AssignComplex(&zero, 0.0, 0.0);
  41.     while (numRoots < degree-1) {        /* For each root...   */
  42.         CopyPoly(poly, polyCopy, degree);
  43.         Guess(&oldGuess);        /* Guess its value... */
  44.         CopyComplex(oldGuess, &newGuess);
  45.         iterations = MAX_ITERATIONS;
  46.         do {                /* Refine the guess...*/
  47.             CopyPoly(poly, polyCopy, degree);
  48.             CopyComplex(newGuess, &oldGuess);
  49.             SynthDivide(polyCopy, oldGuess, degree-numRoots);
  50.             SynthDivide(polyCopy, oldGuess, (degree-1)-numRoots);
  51.             Iterate(polyCopy, oldGuess, degree-numRoots, &newGuess);
  52.         }while (ErrorTooBig(oldGuess, newGuess, epsilon)
  53.             &&  --iterations);        /* ...until convergence. */
  54.         if (!iterations) {
  55.             printf("Root didn't converge after %d iterations.\n",
  56.                 MAX_ITERATIONS);
  57.             printf("Exiting!\n");
  58.             exit(20);
  59.         }
  60.         SynthDivide(poly, newGuess, degree-numRoots);
  61.         PrintRoot(newGuess, ++numRoots);
  62.     }
  63.  
  64.     Divide(poly[1], poly[0], &poly[1]);    /* Get the last root. */
  65.     Subtract(zero, poly[1], &newGuess);
  66.     PrintRoot(newGuess, degree);
  67. }
  68.