home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / sfit110.zip / DLL / POLYNOM.C < prev    next >
C/C++ Source or Header  |  1994-11-22  |  4KB  |  130 lines

  1. #define INCL_WIN
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <os2.h>
  7. #include <float.h>
  8. #define STDCOL 5
  9. #define XSLOT 0
  10. #define YSLOT 1
  11. #define CALCSLOT -3
  12. #define BESTSLOT -2
  13. #define FLAGSLOT -1
  14.  
  15. /* static data and parameters */
  16. static double *data;                    /* start of complete data array  */
  17.  
  18. /* The data is all in a contigous block starting with datx then daty followed by
  19.  * zero or more additional data columns as read in from the data file. The last
  20.  * three slots are: datt , the current best results and the flag array. 
  21.  * Each point not in an exclusion zone has its corresponding flag TRUE. 
  22.  */
  23.  
  24.  
  25. static double *datx;                    /* x data read in from data file */
  26. static double *daty;                    /* y data read in from data file */
  27. static double *datt;                    /* generated data */
  28. static int Ndat;                        /* number of data points */
  29. static char firsttime;
  30. static double **Ptr;                    /* pointer to parameter pointer array */
  31.  
  32. struct info_line
  33.    {
  34.    char *line;
  35.    };
  36.  
  37. /*****************************************************************************/
  38. /* Do not modify any code not enclosed by this and the end banner            */
  39. /*****************************************************************************/
  40.  
  41. /* the next two values are compared to values passed by the program as check */
  42. /* NADD is the number of additional data columns required for this function  */
  43. /* NPARAM is the number of parameters required by this function              */
  44.  
  45. #define NADD 0              
  46. #define NPARAM  10
  47.  
  48. /* edit the following lines or add and delete new lines */ 
  49. static struct info_line dll_info[] = { 
  50.    "This function calculates a polynomial.",
  51.    "It requires 4 parameters as follows:",
  52.    "P1 is the shift along the x axis.",
  53.    "P2 is the shift along the y value.",
  54.    "P3 is the magnitude of the linear term.", 
  55.    "P4 is the magnitude of the quadratic term",
  56.    "P5 is the magnitude of the cubic term",
  57.    "P6 is the magnitude of the x^4 term",
  58.    "P7 is the magnitude of the x^5 term",
  59.    "P8 is the magnitude of the x^6 term",
  60.    "P9 is the magnitude of the x^7 term",
  61.    "P10 is the magnitude of the x^8 term",
  62.    NULL};
  63.    
  64. VOID EXPENTRY fitfunc(void)
  65.    {
  66.    int i;
  67.    double b, a0, a1, a2, a3, a4, a5, a6, a7, a8;
  68.    double x;
  69.  
  70.    /* The next section is executed the first time through only */
  71.    /* so put any initialization code in here                   */
  72.    if (firsttime) 
  73.       {
  74.       firsttime = FALSE;
  75.       /* Mask off all floating point exceptions */
  76.       _control87(MCW_EM,MCW_EM);
  77.       }
  78.  
  79.    /* give parameters more meaningful names  */
  80.    b = *Ptr[1];
  81.    a0 = *Ptr[2];
  82.    a1 = *Ptr[3];
  83.    a2 = *Ptr[4];
  84.    a3 = *Ptr[5];
  85.    a4 = *Ptr[6];
  86.    a5 = *Ptr[7];
  87.    a6 = *Ptr[8];
  88.    a7 = *Ptr[9];
  89.    a8 = *Ptr[10];
  90.  
  91.    /* This is the main loop that calculates the function datt[] */
  92.    /* for all data points. Put your function in the loop body   */ 
  93.    for (i = 0;i < Ndat;i++)
  94.       {
  95.       x = datx[i] - b;
  96.       datt[i] = a0 + x * (a1 + x * (a2 + x * (a3 + x * (a4 + x * (a5 + x * 
  97.            (a6 + x * (a7 + x * a8)))))));
  98.       }
  99.    }
  100.  
  101. /*****************************************************************************/
  102. /* Do not modify any code not enclosed by this and the start banner          */
  103. /*****************************************************************************/
  104.  
  105.  
  106.  
  107. char EXPENTRY init_dll_globals(int N,int mb,int NP,double *dat,double *p[])
  108.    {
  109.    /* set globals */
  110.    firsttime = TRUE;
  111.    Ndat = N;
  112.    if (NADD != mb - STDCOL) return -1;
  113.    if (NPARAM != NP) return -2;
  114.    /* set array pointers */
  115.    data = dat;
  116.    daty = dat + N;
  117.    datx = dat;
  118.    datt = dat + (mb + CALCSLOT) * N;     /* the calculated data goes into the third last slot */
  119.    /* set parameter pointers */
  120.    Ptr = p;
  121.    return 0;
  122.    }
  123.  
  124. struct info_line * EXPENTRY get_dll_info(void)
  125.    {
  126.    /* return pointer to array of info strings set up above */
  127.    return dll_info; 
  128.    }
  129.  
  130.