home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / sfit110.zip / DLL / CUBIC.C < prev    next >
C/C++ Source or Header  |  1994-11-22  |  4KB  |  119 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  5
  47.  
  48. /* edit the following lines or add and delete new lines */ 
  49. static struct info_line dll_info[] = { 
  50.    "This function calculates a cubic.",
  51.    "It requires 5 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.    NULL};
  58.    
  59. VOID EXPENTRY fitfunc(void)
  60.    {
  61.    int i;
  62.    double b, a0, a1, a2, a3;
  63.    double x;
  64.  
  65.    /* The next section is executed the first time through only */
  66.    /* so put any initialization code in here                   */
  67.    if (firsttime) 
  68.       {
  69.       firsttime = FALSE;
  70.       /* Mask off all floating point exceptions */
  71.       _control87(MCW_EM,MCW_EM);
  72.       }
  73.  
  74.    /* give parameters more meaningful names  */
  75.    b = *Ptr[1];
  76.    a0 = *Ptr[2];
  77.    a1 = *Ptr[3];
  78.    a2 = *Ptr[4];
  79.    a3 = *Ptr[5];
  80.  
  81.    /* This is the main loop that calculates the function datt[] */
  82.    /* for all data points. Put your function in the loop body   */ 
  83.    for (i = 0;i < Ndat;i++)
  84.       {
  85.       x = datx[i] - b;
  86.       datt[i] = a0 + x * (a1 + x * (a2 + x * a3));
  87.       }
  88.    }
  89.  
  90. /*****************************************************************************/
  91. /* Do not modify any code not enclosed by this and the start banner          */
  92. /*****************************************************************************/
  93.  
  94.  
  95.  
  96. char EXPENTRY init_dll_globals(int N,int mb,int NP,double *dat,double *p[])
  97.    {
  98.    /* set globals */
  99.    firsttime = TRUE;
  100.    Ndat = N;
  101.    if (NADD != mb - STDCOL) return -1;
  102.    if (NPARAM != NP) return -2;
  103.    /* set array pointers */
  104.    data = dat;
  105.    daty = dat + N;
  106.    datx = dat;
  107.    datt = dat + (mb + CALCSLOT) * N;     /* the calculated data goes into the third last slot */
  108.    /* set parameter pointers */
  109.    Ptr = p;
  110.    return 0;
  111.    }
  112.  
  113. struct info_line * EXPENTRY get_dll_info(void)
  114.    {
  115.    /* return pointer to array of info strings set up above */
  116.    return dll_info; 
  117.    }
  118.  
  119.