home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / ANUMR5.ZIP / LSQTEST.C < prev    next >
C/C++ Source or Header  |  1991-03-31  |  3KB  |  154 lines

  1.  
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <alloc.h>
  7.  
  8. #include "anum.h"
  9. #include "sysio.h"
  10.  
  11.  
  12. void allocate(double **ptr, int n)
  13.  
  14. {    *ptr=farcalloc(n, sizeof(double));
  15.     if (*ptr==NULL)
  16.     {    fputs("Not enough core", stderr);
  17.         exit(-1);
  18.     }
  19. }
  20.  
  21.  
  22. void getdata(int *n, int *m, double **x, double **y, lsqfit *lsq)
  23.  
  24. {       int i;
  25.  
  26.     do
  27.     {    printf("Number of points ( >=2 ) ?");
  28.         while (scanf("%d", n)!=1) ;
  29.     }    while ( *n<2 || *n>20);
  30.  
  31.     allocate(x, *n);
  32.     allocate(y, *n);
  33.  
  34.     for (i=0; i<*n; i++)
  35.     {    do
  36.             printf("XPt[%d] ?\n",i+1);
  37.         while (scanf("%lf",*x+i)!=1);
  38.         do
  39.             printf("YPt[%d] ?\n",i+1);
  40.         while (scanf("%lf",*y+i)!=1);
  41.     }
  42.  
  43.  
  44.     printf("Number of terms ? (2-%d)\n",*n);
  45.     do
  46.     {    while(scanf("%d",m)!=1) ;
  47.     } while (*m<2 || *m>*n);
  48.  
  49.  
  50.     puts("Choose method");
  51.     puts("_____________");
  52.     puts("1) exponantial least square fit");
  53.     puts("2) fourier least square fit");
  54.     puts("3) logarithm least square fit");
  55.     puts("4) polynomial least square fit");
  56.     puts("5) power least square fit");
  57.     puts("6) powers of x least square fit");
  58.     do
  59.     {    while(scanf("%d",&i)!=1) ;
  60.     }     while(i<1 || i>6);
  61.  
  62.     switch(i)
  63.     {    case 1    : *lsq=expolsq;
  64.                   break;
  65.  
  66.         case 2    : *lsq=fourierlsq;
  67.                   break;
  68.  
  69.         case 3    : *lsq=loglsq;
  70.                   break;
  71.  
  72.         case 4    : *lsq=polylsq;
  73.                   break;
  74.  
  75.         case 5  : *lsq=powerlsq;
  76.                   break;
  77.  
  78.         case 6    : *lsq=xpowerlsq;
  79.                   break;
  80.     }
  81. }
  82.  
  83.  
  84.  
  85. void disp_results(int nbpoints, int nbtermes,
  86.           double *xdata, double *ydata,
  87.           double *vectsol, double *yfit, double *residus,
  88.           double ecrtyp, double variance,
  89.           lsqfit fit, int errcode)
  90.  
  91. {    int i;
  92.  
  93.     clrscr();
  94.     SYSMSG(errcode, stderr);
  95.     puts("Results");
  96.     puts("-------");
  97.     puts("The data points:");
  98.     puts("  N             X            Y");
  99.     for (i=0; i<nbpoints; i++)
  100.         printf("%3d        % 10le   % 10le\n", i+1, *(xdata+i),
  101.                     *(ydata+i));
  102.     putchar('\n');
  103.     puts(lsqname(fit));
  104.     putchar('\n');
  105.  
  106.     puts("Coefficients in least squares fit");
  107.     for (i=0; i<nbtermes; i++)
  108.         printf("Coeff[%3d] = % 10le\n",i, *(vectsol+i));
  109.     putchar('\n');
  110.  
  111.     puts("     X          LSQ Fit       Residual");
  112.     for (i=0; i<nbpoints; i++)
  113.         printf("% 10le  % 10le  % 10le\n", *(xdata+i), *(yfit+i),
  114.                             *(residus+i));
  115.     putchar('\n');
  116.     printf("Standard deviation : % 10le\n",ecrtyp);
  117.     printf("Variance           : % 10le\n",variance);
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
  124. void main(void)
  125.  
  126. {    int nbpoints, nbtermes;
  127.     double *xdata, *ydata, *vectsol, *yfit, *residus,
  128.            ecrtyp,variance;
  129.     int errcode;
  130.     lsqfit fit;
  131.  
  132.     ena_m_beep();
  133.     clrscr();
  134.     getdata(&nbpoints, &nbtermes, &xdata, &ydata, &fit);
  135.  
  136.     allocate(&vectsol, nbpoints);
  137.     allocate(&yfit,    nbpoints);
  138.     allocate(&residus, nbpoints);
  139.  
  140.     lsq(nbpoints,
  141.         xdata, ydata,
  142.         &nbtermes,
  143.         vectsol, yfit, residus,
  144.         &ecrtyp, &variance,
  145.         &errcode,
  146.         fit);
  147.  
  148.  
  149.     disp_results(nbpoints, nbtermes, xdata, ydata,
  150.         vectsol, yfit, residus,
  151.         ecrtyp, variance,
  152.         fit, errcode);
  153.  
  154. }