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

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