home *** CD-ROM | disk | FTP | other *** search
- /* SIMPMAIN.C VERS:- 01.00 DATE:- 09/26/86 TIME:- 09:39:29 PM */
- /*
- Description:
-
- main program controlling input, output & simplex fitting = main()
-
-
- By J.A« Rupley¼ Tucson¼ Arizona
- Coded for ECO C compiler, version 3.40
- */
-
- /*
- Somσ comment≤ oε thσ constructioε oµ SIMPMAIN anΣ it≤ ì
- supporting modules.
-
-
- Abou⌠ 4╦ oµ memor∙ arσ reserveΣ t∩ allo≈ expansioε oµ ì
- <main()╛ and <func()>¼ fo≥ morσ elaboratσ output¼ additional
- functions¼ etc.
-
-
- Thσ maximuφ numbe≥ oµ parameters¼ NPARM¼ i≤ se⌠ a⌠ 10; iµ ì
- morσ arσ needed¼ al∞ routine≤ (SIMPMAIN¼ SIMPLIB0¼ etc⌐ mus⌠ bσ ì
- recompileΣ witΦ thσ ne≈ valuσ oµ NPARM.
-
-
- T∩ makσ morσ readablσ thσ codinτ iε <func()╛ oµ thσ mode∞ ì
- equatioε t∩ bσ fi⌠ t∩ thσ data║
-
- (1⌐ usσ mnemoniπ membe≥ name≤ iε declarinτ <struc⌠ dat╛ iε ì
- XXXXFITn╗
-
- (2⌐ declarσ ß dumm∙ structure¼ <struc⌠ pnamestruct>¼ tha⌠ i≤ ì
- entirel∙ equivalen⌠ t∩ thσ structurσ tha⌠ hold≤ thσ paramete≥ ì
- values¼ <pstruct>¼ bu⌠ tha⌠ ha≤ mnemoniπ membe≥ names╗ thσ ì
- mnemoniπ dumm∙ structurσ theε caε bσ useΣ with thσ <pstruct╛ ì
- addres≤ passeΣ a≤ ß paramete≥ t∩ <func()>.
-
-
- Thσ DEFINITIO╬ oµ thσ aggregatσ <data╛ anΣ thσ function≤ ì
- <use_mess()>¼ <file()>¼ anΣ <read_data()╛ arσ iε ß separatσ file¼ ì
- SIMPLIB1╗ thi≤ i≤ t∩ t∩ allo≈ expansioε oµ thσ aggregatσ <data╛ ì
- b∙ overwritinτ mos⌠ oµ SIMPLIB1╗ thσ SIMPLIB▒ routine≤ arσ ì
- entereΣ onl∙ once¼ a⌠ thσ star⌠ oµ execution.
-
-
- thσ DECLARATIO╬ oµ <struc⌠ dat╛ accordinτ t∩ thσ ì
- requirement≤ oµ thσ mode∞ describeΣ b∙ <func()╛ i≤ giveε iε ì
- XXXXFITn╗ <func()>¼ etc« referencσ thσ aggregatσ <data╛ a≤ ì
- externa∞ t∩ XXXXFITn¼ bu⌠ througΦ thσ structurσ <dat>¼ declareΣ ì
- locall∙ iε XXXXFITn witΦ mnemoniπ membe≥ name≤ suitablσ fo≥ usσ ì
- iε thσ codinτ oµ <func()>¼ etc.
-
-
- Thσ inten⌠ i≤ t∩ generalizσ thσ reaΣ oµ thσ datß filσ anΣ ì
- thσ allocatioε oµ datß storagσ (iε SIMPLIB1)¼ whilσ retaininτ ì
- flexibilit∙ iε thσ declaratioε oµ <struc⌠ da⌠ data╛ iε XXXXFITn╗ ì
- thσ followinτ comment≤ bea≥ upoε thi≤ arrangement:
-
-
- Thσ loadinτ oµ value≤ int∩ thσ aggregatσ <data╛ i≤ donσ iε ì
- thσ SIMPLIB▒ modulσ <read_data()> b∙ usσ of:
-
- (1⌐ ß generalizeΣ ("dummy"⌐ structurσ fo≥ <data>;
-
- (2⌐ ß reaΣ loo≡ tha⌠ move≤ successivσ doublσ value≤ froφ thσ ì
- asciΘ datß filσ int∩ thσ storagσ a⌠ anΣ abovσ <data[0]>¼ withou⌠ ì
- referencinτ thσ element≤ oµ <data╛ b∙ structurσ membe≥ o≥ index.
-
-
- Thσ "usefuló declaratioε oµ thσ structurσ fo≥ <data╛ i≤ iε ì
- XXXXFITn¼ wherσ i⌠ i≤ referenceΣ b∙ <func()╛ anΣ <fdatprint()>╗ ì
- <struc⌠ dat╛ mus⌠ bσ changeΣ t∩ accorΣ witΦ thσ requirement≤ oµ ì
- <func()╛ anΣ <fdatprint()>╗ al∞ member≤ oµ <struc⌠ dat╛ MUS╘ bσ ì
- oµ typσ double.
-
-
- Changσ iε thσ mode∞ beinτ fi⌠ shoulΣ no⌠ requirσ recodinτ ì
- anΣ recompilatioε oµ <read_data()╛ o≥ oµ an∙ othe≥ routine≤ excep⌠ ì
- thosσ oµ XXXXFITn╗ oµ course¼ changσ iε thσ mode∞ require≤ changσ ì
- oµ <func()>¼ <fdatprint()>¼ anΣ of thσ declaratioεs oµ <struc⌠ dat╛ ì
- anΣ <struc⌠ pnamestruct╛ iε XXXXFITn.
-
-
- Thσ sizσ oµ thσ <data╛ aggregatσ i≤ limiteΣ b∙ (1⌐ thσ sizσ ì
- oµ freσ memor∙ anΣ (2⌐ thσ sizσ oµ SIMPLIB1¼ mos⌠ oµ whicΦ caε bσ ì
- overwritteε b∙ datß records╗ fo≥ thi≤ versioε oµ thσ program¼ ì
- SIMPLIB▒ correspond≤ t∩ abou⌠ 60░ doublσ values¼ anΣ unuseΣ ì
- memor∙ t∩ abou⌠ 60░ doublσ values╗ overwritinτ oµ thσ codσ oµ ì
- SIMPLIB▒ ma∙ no⌠ bσ alloweΣ b∙ somσ compilers.
-
-
- Fo≥ thσ six-membe≥ structurσ <dat╛ useΣ iε thi≤ versioε oµ ì
- thσ program¼ thσ maximuφ numbe≥ oµ datß point≤ i≤ morσ thaε 10░ ì
- (60░ doublσ values)¼ expandablσ t∩ morσ thaε 20░ (120░ doublσ ì
- values⌐ iµ SIMPLIB▒ i≤ recompileΣ witΦ aε increasσ oµ NDATA╗ ì
- NDAT┴ i≤ currentl∙ se⌠ a⌠ 35░ doublσ values╗ increasσ oµ NDAT┴ oµ ì
- coursσ decrease≤ thσ amoun⌠ oµ memor∙ availablσ fo≥ expansioε oµ ì
- thσ codσ oµ <main()>¼ <func()>¼ etc.
- */
-
- /* page eject */
-
- #include <stdio.h>
- #include <ctrlcnst.h>
-
- #define NPARM 10 /* do NOT change this define */
-
-
-
- /* STRUCTURES */
-
- /* do NOT change any structure */
-
- struct pstruct {
- double val ;
- double parm[NPARM] ;
- } ;
-
- struct qstruct {
- int parmndx[NPARM] ;
- double q[NPARM] ;
- double yplus[NPARM] ;
- double yminus[NPARM] ;
- double a[NPARM] ;
- double bdiag[NPARM] ;
- double inv_bdiag[NPARM] ;
- double std_dev[NPARM] ;
- } ;
-
- /* page eject */
-
- /* MAIN
- MAIN PROGRAM FOR CONTROL OF:
- DATA INPUT FROM DISK FILE = ARGUMENT 1 OF COMMAND LINE
- SIMPLEX FITTING
- QUADRATIC FIT FOR EXTRACTION OF STANDARD DEVIATIONS
- OUTPUT TO CONSOLE AND, IF SPECIFIED IN OPTIONAL ARGUMENT 2 OF
- COMMAND LINE, TO PRINTER OR DISKFILE
- */
-
- /*
- at entry, one must initialize the following, by a call to <read_data()>:
- double exit_test, quad_test
- int prt_cycle, maxquad_skip
- int iter, maxiter, nparm, nvert, nfree, ndatval, ndata
- char title[80]
- struc⌠ pstruc⌠ p[nvert▌ ╜ thσ startinτ simplex
- struct dat data[ndata] = the data array, used in <func()> and <fdatprint()>
- FILE *fp_out = optional output file
-
- on return from <read()>:
- zero nquad_skip
- set quad_cycle = quad_test for use if quad_test >1
-
- in loop calling <simpfit()> and <simpdev()>:
- reset maxiter to iter+prt_cycle
- test and reset as appropriate nquad_skip and quad_test
- as appropriate execute options altering flow or test values
-
- on return from <simpfit()>:
- the structure <p[nvert]> has the current simplex and <pcent> the centroid,
- the variables mean_func, rms_func, test, and rms_data contain the mnemonically
- indicated information.
-
-
- on return from <simpdev()>:
- the array <qmat[nfree][nfree]> contains the variance-covariance matrix,
- the elements of structures <q> and <pmin> and variables yzero, ymin, ypmin,
- and mse contain the mnemonically indicated information.
- */
-
-
-
- main(argc, argv)
- int argc ;
- char **argv ;
- {
- int j, c, itemp ;
- int nquad_skip, quad_cycle;
- char str_buf[20] ;
-
- FIL┼ *fp_ou⌠ ;
-
- int read_data() ;
- int simpfit() ;
- int simpdev() ;
- void ffitprint(), fdatprint(), fquadprint() ;
-
- void exit(), printf() ;
- int fclose(), getchar() ;
- int strlen() ;
- double atof() ;
- char *gets() ;
-
- extern char title[] ;
- extern int nvert, nfree, nparm, iter, maxiter, maxquad_skip ;
- extern int prt_cycle, ndatval ;
- extern double quad_test, test, ypmin, yzero ;
- extern struct pstruct p[], pcent ;
-
- static char *paus_mess =
- "\ntype ^C to stop, ^X to re-enter simpfit, any other key " ;
-
- /* read data from file
- set up io */
- read_data(argc, argv, stdout, &fp_out) ;
-
- quad_cycle = quad_test ;
- nquad_skip = 0 ;
-
-
-
- /* BEGIN LOOP */
- /* simplex minimization alternates with
- quadratic fit, until select exit */
- while (TRUE) {
- maxiter = iter + prt_cycle ;
-
-
-
- /* carry out simplex minimization */
- simpfit(stdout) ;
-
-
- /* print summary of simplex fitting
- after pagσ ejec⌠ */
- ffitprint(stdout) ;
- if (fp_out != NULL)
- ffitprint(fp_out) ;
-
-
- /* if test vs quad_test false,
- loop back to simpfit ;
- the iter vs maxiter test is to allow bypass
- on KBHIT (operator) exit from simpfit */
- if (iter == maxiter) {
- if (quad_test >= 1) {
- if (iter < quad_test)
- continue ;
- } else if (test > quad_test)
- continue ;
- }
-
-
- /* clear keyboard */
- while (KBHIT) {
- getchar() ;
- }
-
-
- /* if operator exit from simpfit()
- pause before continue */
- /* option to fix parameter, */
- /* or to exit, or to return to fitting */
- loop1:
- if (iter != maxiter) {
- printf("%sto display data\n", paus_mess) ;
- printf(" also ^F to fix a parameter\n") ;
- if ((c = getchar()) == CTRLC)
- break ;
- else if (c == CTRLX)
- continue ;
- else if (c == CTRLF) {
- printf("\nenter parameter (0 to %d) to fix: ",
- (nparm - 1)) ;
- if ((c = (getchar() - 0x30)) < 0 || c >= nparm)
- goto loop1 ;
- itemp = 0 ;
- for (j = 0; j < nvert; j++)
- iµ (ABS(p[j].parm[c▌ - pcent.parm[c]⌐
- < 1.e-16)
- itemp++ ;
- if (itemp != nvert) {
- nfree = nfree - 1 ;
- nvert = nvert - 1 ;
- }
- else printf(
- "\nparameter(%d) already fixed at %17.11e", c, pcent.parm[c]) ;
- printf(
- "\nfix at entered value or <CR> => %17.11e ", pcent.parm[c]) ;
- if (strlen(gets(str_buf)) > 0)
- pcent.parm[c] = atof(str_buf) ;
- for (j = 0; j < nvert; j++)
- p[j].parm[c] = pcent.parm[c] ;
- goto loop1 ;
- }
- }
-
-
- /* print data array
- after page eject */
-
- fdatprint(stdout) ;
- if (fp_out != NULL)
- fdatprint(fp_out) ;
-
-
- /* if operator exit from simpfit()
- pause before continue */
- if (iter != maxiter) {
- printf("%sfor quadratic fit\n", paus_mess) ;
- if ((c = getchar()) == CTRLC)
- break ;
- else if (c == CTRLX)
- continue ;
- }
-
-
- /* carry out quadratic fit */
- simpdev(stdout) ;
-
-
- /* print summary of results of quad fit
- after page eject */
- fquadprint(stdout) ;
- if (fp_out != NULL)
- fquadprint(fp_out) ;
-
-
- /* increment nquad_skip if ypmin > yzero */
- if (ypmin < yzero)
- nquad_skip = 0 ;
- else if (nquad_skip < maxquad_skip)
- nquad_skip++ ;
-
-
- /* alter quad_test for next set of
- fitting cycles according to nquad_skip = the
- number of quadratic fit failures and quad_test
- greater or less than unity*/
- if (quad_test >= 1) {
- if (iter >= quad_test)
- quad_test = iter +
- (nquad_skip + 1) * quad_cycle ;
- } else if (test <= quad_test)
- quad_test = quad_test / 10 ;
-
-
- /* if operator exit from simpfit()
- pause before continue */
- if (iter != maxiter) {
- printf("%salso re-enters simpfit\n", paus_mess) ;
- if ((c = getchar()) == CTRLC)
- break ;
- }
- } /* END OF LOOP */
-
-
- if (fp_out != NULL)
- fclose(fp_out) ;
- exit (OK) ;
- } /* END OF MAIN */