home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / sfit110.zip / DLL / GAUSS5.C < prev    next >
C/C++ Source or Header  |  1994-11-22  |  5KB  |  159 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 16
  47.  
  48. /* edit the following lines or add and delete new lines */ 
  49. static struct info_line dll_info[] = { 
  50.    "This function calculates five gaussians.",
  51.    "It requires 16 parameters as follows:",
  52.    "P1 is the constant background.",
  53.    "P2 is the center posiion of gaussian 1 along the x axis.",
  54.    "P3 is the amplitude of gaussian 1.",
  55.    "P4 is the width of gaussian 1.", 
  56.    "P5 is the center posiion of gaussian 2 along the x axis.",
  57.    "P6 is the amplitude of gaussian 2.",
  58.    "P7 is the width of gaussian 2.", 
  59.    "P8 is the center posiion of gaussian 3 along the x axis.",
  60.    "P9 is the amplitude of gaussian 3.",
  61.    "P10is the width of gaussian 3.", 
  62.    "P11  is the center posiion of gaussian 4 along the x axis.",
  63.    "P12 is the amplitude of gaussian 4.",
  64.    "P13 is the width of gaussian 4.", 
  65.    "P14 is the center posiion of gaussian 5 along the x axis.",
  66.    "P15 is the amplitude of gaussian 5.",
  67.    "P16 is the width of gaussian 5.", 
  68.    NULL};
  69.    
  70. VOID EXPENTRY fitfunc(void)
  71.    {
  72.    int i;
  73.    double wid1, wid2;
  74.    double wid3, wid4, wid5;
  75.    double amp1, amp2;
  76.    double amp3, amp4, amp5;
  77.    double pos1, pos2;
  78.    double pos3, pos4, pos5;
  79.    double E1, E2;
  80.    double E3, E4, E5;
  81.    double gau1, gau2;
  82.    double gau3, gau4, gau5;
  83.    double offset;
  84.  
  85.    /* The next section is executed the first time through only */
  86.    /* so put any initialization code in here                   */
  87.    if (firsttime) 
  88.       {
  89.       firsttime = FALSE;
  90.       /* Mask off all floating point exceptions */
  91.       _control87(MCW_EM,MCW_EM);
  92.       }
  93.  
  94.    /* give parameters more meaningful names  */
  95.    offset=*Ptr[1];
  96.    pos1 = *Ptr[2];
  97.    amp1 = *Ptr[3];
  98.    wid1 = *Ptr[4];
  99.    pos2 = *Ptr[5];
  100.    amp2 = *Ptr[6];
  101.    wid2 = *Ptr[7];
  102.    pos3 = *Ptr[8];
  103.    amp3 = *Ptr[9];
  104.    wid3 = *Ptr[10];
  105.    pos4 = *Ptr[11];
  106.    amp4 = *Ptr[12];
  107.    wid4 = *Ptr[13];
  108.    pos5 = *Ptr[14];
  109.    amp5 = *Ptr[15];
  110.    wid5 = *Ptr[16];
  111.  
  112.    /* This is the main loop that calculates the function datt[] */
  113.    /* for all data points. Put your function in the loop body   */ 
  114.    for (i = 0;i < Ndat;i++)
  115.       {
  116.       E1 = datx[i] - pos1;
  117.       E2 = datx[i] - pos2;
  118.       E3 = datx[i] - pos3;
  119.       E4 = datx[i] - pos4;
  120.       E5 = datx[i] - pos5;
  121.       gau1 = amp1 * exp(-E1*E1/wid1/wid1);        /*Gaussian */
  122.       gau2 = amp2 * exp(-E2*E2/wid2/wid2); 
  123.       gau3 = amp3 * exp(-E3*E3/wid3/wid3); 
  124.       gau4 = amp4 * exp(-E4*E4/wid4/wid4); 
  125.       gau5 = amp5 * exp(-E5*E5/wid5/wid5); 
  126.       datt[i] = gau1 + gau2 + gau3 + gau4 + gau5 +offset;
  127.       }
  128.    }
  129.  
  130. /*****************************************************************************/
  131. /* Do not modify any code not enclosed by this and the start banner          */
  132. /*****************************************************************************/
  133.  
  134.  
  135.  
  136. char EXPENTRY init_dll_globals(int N,int mb,int NP,double *dat,double *p[])
  137.    {
  138.    /* set globals */
  139.    firsttime = TRUE;
  140.    Ndat = N;
  141.    if (NADD != mb - STDCOL) return -1;
  142.    if (NPARAM != NP) return -2;
  143.    /* set array pointers */
  144.    data = dat;
  145.    daty = dat + N;
  146.    datx = dat;
  147.    datt = dat + (mb + CALCSLOT) * N;     /* the calculated data goes into the third last slot */
  148.    /* set parameter pointers */
  149.    Ptr = p;
  150.    return 0;
  151.    }
  152.  
  153. struct info_line * EXPENTRY get_dll_info(void)
  154.    {
  155.    /* return pointer to array of info strings set up above */
  156.    return dll_info; 
  157.    }
  158.  
  159.