home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ANUMR5.ZIP / NEWTHOR1.C < prev    next >
C/C++ Source or Header  |  1991-11-25  |  2KB  |  115 lines

  1. /* OK */
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6.  
  7. #include "anum.h"
  8. #include "sysio.h"
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. void affpoly(double *p,int n)
  16.  
  17. {    int i;
  18.  
  19.     for (i=n;i>=0;i--) printf("Poly[%d] = %lf\n",i,*(p+i));
  20. }
  21.  
  22.  
  23.  
  24.  
  25. void results(int initdegree,
  26.          double *initpoly,
  27.          double initguess, double tol,
  28.          int maxiter, int degree, int numroots,
  29.          double *poly, double *root, double *imag,
  30.          double *value, double *deriv,
  31.          int *iter,
  32.          int errcode)
  33.  
  34.  
  35.  
  36. {    int i;
  37.  
  38.     puts("\n\n\nInitial polynome :\n");
  39.     for (i=initdegree; i>=0; i--)
  40.         printf("Poly[%d] : %lf\n",i,*(initpoly+i));
  41.     printf("\nInitial approximation : %lf\n",initguess);
  42.     printf("tolerance : %le\n",tol);
  43.     printf("Maximum number of iterations : %d\n\n",maxiter);
  44.         SYSMSG(errcode,stderr);
  45.     if (errcode==EOVERMAXITER)
  46.         SYSMSG(WCPLXROOTS,stderr);
  47.     printf("\nNumber of calculated roots : %d\n", numroots);
  48.     for (i=1; i<=numroots; i++)
  49.     {    printf("\nRoot : %d\n",i);
  50.         printf("Iterations number : %d\n",*(iter+i));
  51.         printf("Root : %le",*(root+i));
  52.         if (iseq0(*(imag+i)))
  53.             putchar('\n');
  54.         else
  55.             printf(" + %lfi\n",*(imag+i));
  56.         printf("Value of the function at this root : %le\n",
  57.                 *(value+i));
  58.         printf("Value of the derivative at this root : %le\n\n",
  59.                 *(deriv+i));
  60.     }
  61.  
  62.     if (errcode==EOVERMAXITER || errcode==WCPLXROOTS)
  63.     {    puts("\nReduced polynome : ");
  64.         for (i=degree; i>=0; i--) printf("RP[%d]=%lf\n",i,*(poly+i));
  65.     }
  66. }
  67.  
  68.  
  69. void main()
  70.  
  71.  
  72. {    double poly0[20];
  73.     double x,tol,poly[20],racine[20],image[20],valeur[20],deriv[20];
  74.     int maxiter,degre,nbrac,iter[20],errcode,degre0;
  75.  
  76.     FILE *fp;
  77.     char s[80];
  78.     int i;
  79.  
  80.     fp=fopen("toto.dat","rt");
  81.     if (fp==NULL)
  82.     {    errmsg(__FILE__,__DATE__,__TIME__,EFOPENR,
  83.             "Can't read \"toto.dat\".",stderr);
  84.         exit(EFOPENR);
  85.     }
  86.     fgets(s,80,fp);
  87.     sscanf(s,"%d",°re0);
  88.     for (i=degre0; i>=0; i--)
  89.     {    fgets(s,80,fp);
  90.         sscanf(s,"%lf",&poly0[i]);
  91.     }
  92.     fclose(fp);
  93.  
  94.     maxiter=1000;
  95.     x=1;
  96.     tol=1e-8;
  97.  
  98.  
  99.     puts("Polynome to solve using Newton-Horner algorithm :");
  100.     affpoly(poly0,degre0);
  101.     newton_horner(degre0,
  102.               poly0,
  103.               x,tol,
  104.               maxiter,
  105.               °re,&nbrac,
  106.               poly,racine,image,valeur,deriv,
  107.               iter,&errcode);
  108.     puts("\n\nResults :\n-------\n");
  109.  
  110.     results(degre0,poly0,x,tol,maxiter,
  111.         degre,nbrac,poly,racine,image,valeur,deriv,iter,errcode);
  112. }
  113.  
  114.  
  115.