home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / sfit110.zip / DLL / LORENTZ9.C < prev    next >
C/C++ Source or Header  |  1994-11-22  |  7KB  |  192 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  28
  48.  
  49. /* edit the following lines or add and delete new lines */ 
  50. static struct info_line dll_info[] = { 
  51.    "This function calculates nine lorentzians.",
  52.    "It requires 28 parameters as follows:",
  53.    "P1 is the constant background",
  54.    "P1 is the center posiion of lorentzian 1 along the x axis.",
  55.    "P2 is the amplitude of lorentzian 1.",
  56.    "P3 is the width of lorentzian 1.", 
  57.    "P4 is the center posiion of lorentzian 2 along the x axis.",
  58.    "P5 is the amplitude of lorentzian 2.",
  59.    "P6 is the width of lorentzian 2.", 
  60.    "P7 is the center posiion of lorentzian 3 along the x axis.",
  61.    "P8 is the amplitude of lorentzian 3.",
  62.    "P9 is the width of lorentzian 3.", 
  63.    "P10  is the center posiion of lorentzian 4 along the x axis.",
  64.    "P11 is the amplitude of lorentzian 4.",
  65.    "P12 is the width of lorentzian 4.", 
  66.    "P13 is the center posiion of lorentzian 5 along the x axis.",
  67.    "P14 is the amplitude of lorentzian 5.",
  68.    "P15 is the width of lorentzian 5.", 
  69.    "P16 is the center posiion of lorentzian 6 along the x axis.",
  70.    "P17 is the amplitude of lorentzian 6.",
  71.    "P18 is the width of lorentzian 6.", 
  72.    "P19 is the center posiion of lorentzian 7 along the x axis.",
  73.    "P20 is the amplitude of lorentzian 7.",
  74.    "P21 is the width of lorentzian 7.", 
  75.    "P22 is the center posiion of lorentzian 8 along the x axis.",
  76.    "P23 is the amplitude of lorentzian 8.",
  77.    "P24 is the width of lorentzian 8.", 
  78.    "P25 is the center posiion of lorentzian 9 along the x axis.",
  79.    "P26 is the amplitude of lorentzian 9.",
  80.    "P27 is the width of lorentzian 9.", 
  81.    NULL};
  82.    
  83. VOID EXPENTRY fitfunc(void)
  84.    {
  85.    int i;
  86.    double wid1, wid2, wid6, wid7; 
  87.    double wid3, wid4, wid5, wid8, wid9;
  88.    double amp1, amp2 , amp6, amp7; 
  89.    double amp3, amp4, amp5, amp8, amp9;
  90.    double pos1, pos2, pos6, pos7;
  91.    double pos3, pos4, pos5, pos8, pos9;
  92.    double E1, E2, E6, E7;
  93.    double E3, E4, E5, E8, E9;
  94.    double lor1, lor2, lor6, lor7;
  95.    double lor3, lor4, lor5, lor8, lor9;
  96.    double offset;
  97.  
  98.    /* The next section is executed the first time through only */
  99.    /* so put any initialization code in here                   */
  100.    if (firsttime) 
  101.       {
  102.       firsttime = FALSE;
  103.       /* Mask off all floating point exceptions */
  104.       _control87(MCW_EM,MCW_EM);
  105.       }
  106.  
  107.    /* give parameters more meaningful names  */
  108.    offset=*Ptr[1];
  109.    pos1 = *Ptr[2];
  110.    amp1 = *Ptr[3];
  111.    wid1 = *Ptr[4];
  112.    pos2 = *Ptr[5];
  113.    amp2 = *Ptr[6];
  114.    wid2 = *Ptr[7];
  115.    pos3 = *Ptr[8];
  116.    amp3 = *Ptr[9];
  117.    wid3 = *Ptr[10];
  118.    pos4 = *Ptr[11];
  119.    amp4 = *Ptr[12];
  120.    wid4 = *Ptr[13];
  121.    pos5 = *Ptr[14];
  122.    amp5 = *Ptr[15];
  123.    wid5 = *Ptr[16];
  124.    pos6 = *Ptr[17];
  125.    amp6 = *Ptr[18];
  126.    wid6 = *Ptr[19];
  127.    pos7 = *Ptr[20];
  128.    amp7 = *Ptr[21];
  129.    wid7 = *Ptr[22];
  130.    pos8 = *Ptr[23];
  131.    amp8 = *Ptr[24];
  132.    wid8 = *Ptr[25];
  133.    pos9 = *Ptr[26];
  134.    amp9 = *Ptr[27];
  135.    wid9 = *Ptr[28];
  136.  
  137.    /* This is the main loop that calculates the function datt[] */
  138.    /* for all data points. Put your function in the loop body   */ 
  139.    for (i = 0;i < Ndat;i++)
  140.       {
  141.       E1 = datx[i] - pos1;
  142.       E2 = datx[i] - pos2;
  143.       E3 = datx[i] - pos3;
  144.       E4 = datx[i] - pos4;
  145.       E5 = datx[i] - pos5;
  146.       E6 = datx[i] - pos6;
  147.       E7 = datx[i] - pos7;
  148.       E8 = datx[i] - pos8;
  149.       E9 = datx[i] - pos9;
  150.       lor1 = amp1 / (1 + E1*E1/wid1/wid1) ;   /*Lorentzian */
  151.       lor2 = amp2 / (1 + E2*E2/wid2/wid2) ;  
  152.       lor3 = amp3 / (1 + E3*E3/wid3/wid3) ;  
  153.       lor4 = amp4 / (1 + E4*E4/wid4/wid4) ;  
  154.       lor5 = amp5 / (1 + E5*E5/wid5/wid5) ;  
  155.       lor6 = amp6 / (1 + E6*E6/wid6/wid6) ;  
  156.       lor7 = amp7 / (1 + E7*E7/wid7/wid7) ;  
  157.       lor8 = amp8 / (1 + E8*E8/wid8/wid8) ;  
  158.       lor9 = amp9 / (1 + E9*E9/wid9/wid9) ;  
  159.       datt[i] = lor1 + lor2 + lor3 + lor4 + lor5 + lor6 + lor7 + lor8 + lor9 +offset;
  160.       }
  161.    }
  162.  
  163. /*****************************************************************************/
  164. /* Do not modify any code not enclosed by this and the start banner          */
  165. /*****************************************************************************/
  166.  
  167.  
  168.  
  169. char EXPENTRY init_dll_globals(int N,int mb,int NP,double *dat,double *p[])
  170.    {
  171.    /* set globals */
  172.    firsttime = TRUE;
  173.    Ndat = N;
  174.    if (NADD != mb - STDCOL) return -1;
  175.    if (NPARAM != NP) return -2;
  176.    /* set array pointers */
  177.    data = dat;
  178.    daty = dat + N;
  179.    datx = dat;
  180.    datt = dat + (mb + CALCSLOT) * N;     /* the calculated data goes into the third last slot */
  181.    /* set parameter pointers */
  182.    Ptr = p;
  183.    return 0;
  184.    }
  185.  
  186. struct info_line * EXPENTRY get_dll_info(void)
  187.    {
  188.    /* return pointer to array of info strings set up above */
  189.    return dll_info; 
  190.    }
  191.  
  192.