home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / sfit110.zip / DLL / LORENTZ.C < prev    next >
C/C++ Source or Header  |  1994-11-22  |  4KB  |  121 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. // the standard number of data columns
  9. #define STDCOL 5
  10. #define XSLOT 0
  11. #define YSLOT 1
  12. #define CALCSLOT -3
  13. #define BESTSLOT -2
  14. #define FLAGSLOT -1
  15.  
  16. /* static data and parameters */
  17. static double *data;                    /* start of complete data array  */
  18.  
  19. /* The data is all in a contigous block starting with datx then daty followed by
  20.  * zero or more additional data columns as read in from the data file. The last
  21.  * three slots are: datt , the current best results and the flag array. 
  22.  * Each point not in an exclusion zone has its corresponding flag TRUE. 
  23.  */
  24.  
  25.  
  26. static double *datx;                    /* x data read in from data file */
  27. static double *daty;                    /* y data read in from data file */
  28. static double *datt;                    /* generated data */
  29. static int Ndat;                        /* number of data points */
  30. static char firsttime;
  31. static double **Ptr;                    /* pointer to parameter pointer array */
  32.  
  33. struct info_line
  34.    {
  35.    char *line;
  36.    };
  37.  
  38. /*****************************************************************************/
  39. /* Do not modify any code not enclosed by this and the end banner            */
  40. /*****************************************************************************/
  41.  
  42. /* the next two values are compared to values passed by the program as check */
  43. /* NADD is the number of additional data columns required for this function  */
  44. /* NPARAM is the number of parameters required by this function              */
  45.  
  46. #define NADD 0              
  47. #define NPARAM 4
  48.  
  49. /* edit the following lines or add and delete new lines */ 
  50. static struct info_line dll_info[] = { 
  51.    "This function calculates a lorentzian.",
  52.    "It requires 3 parameters as follows:",
  53.    "P1 is the constant background",
  54.    "P2 is the center posiion of lorentzian along the x axis.",
  55.    "P3 is the amplitude of lorentzian.",
  56.    "P4 is the width of lorentzian.", 
  57.    NULL};
  58.    
  59. VOID EXPENTRY fitfunc(void)
  60.    {
  61.    int i;
  62.    double back;
  63.    double width;
  64.    double amplitude;
  65.    double position;
  66.    double E;
  67.  
  68.    /* The next section is executed the first time through only */
  69.    /* so put any initialization code in here                   */
  70.    if (firsttime) 
  71.       {
  72.       firsttime = FALSE;
  73.       /* Mask off all floating point exceptions */
  74.       _control87(MCW_EM,MCW_EM);
  75.       }
  76.  
  77.    /* give parameters more meaningful names  */
  78.    back = *Ptr[1];
  79.    position = *Ptr[2];
  80.    amplitude = *Ptr[3];
  81.    width = *Ptr[4];
  82.  
  83.    /* This is the main loop that calculates the function datt[] */
  84.    /* for all data points. Put your function in the loop body   */ 
  85.    for (i = 0;i < Ndat;i++)
  86.       {
  87.       E = datx[i] - position;
  88.       datt[i] = amplitude / (1 + E*E/width/width) + back;        /*Lorentzian */
  89.       }
  90.    }
  91.  
  92. /*****************************************************************************/
  93. /* Do not modify any code not enclosed by this and the start banner          */
  94. /*****************************************************************************/
  95.  
  96.  
  97.  
  98. char EXPENTRY init_dll_globals(int N,int mb,int NP,double *dat,double *p[])
  99.    {
  100.    /* set globals */
  101.    firsttime = TRUE;
  102.    Ndat = N;
  103.    if (NADD != mb - STDCOL) return -1;
  104.    if (NPARAM != NP) return -2;
  105.    /* set array pointers */
  106.    data = dat;
  107.    daty = dat + N;
  108.    datx = dat;
  109.    datt = dat + (mb + CALCSLOT) * N;     /* the calculated data goes into the third last slot */
  110.    /* set parameter pointers */
  111.    Ptr = p;
  112.    return 0;
  113.    }
  114.  
  115. struct info_line * EXPENTRY get_dll_info(void)
  116.    {
  117.    /* return pointer to array of info strings set up above */
  118.    return dll_info; 
  119.    }
  120.  
  121.