home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / turbopas / pas_sci.arc / LEAST4.PAS < prev    next >
Pascal/Delphi Source File  |  1985-08-20  |  4KB  |  159 lines

  1. program least4;        { --> 216 }
  2. { Pascal program to perform a linear least-squares fit }
  3. { with Gauss-Jordan routine to Heat Capacity Equation }
  4. { Separate modules needed:
  5.             GAUSSJ,
  6.             PLOT }
  7.  
  8. const    maxr    = 20;        { data prints }
  9.     maxc    = 5;        { polynomial terms }
  10.  
  11. type    ary    = array[1..maxr] of real;
  12.     arys    = array[1..maxc] of real;
  13.     ary2    = array[1..maxr,1..maxc] of real;
  14.     ary2s    = array[1..maxc,1..maxc] of real;
  15.  
  16. var    x,y,y_calc    : ary;
  17.     resid        : ary;
  18.     coef,sig    : arys;
  19.     nrow,ncol    : integer;
  20.     correl_coef    : real;
  21.     first,done    : boolean;
  22.  
  23. { -> 216 }
  24. procedure get_data(var t    : ary;        { independedt variable }
  25.            var cp    : ary;        { dependent variable }
  26.            var nrow    : integer);    { length of vectors }
  27. var    i    : integer;
  28.  
  29. begin
  30.   nrow:=10;
  31.   for i:=1 to nrow do
  32.     t[i]:=(i+2)*100;
  33.   cp[1]:=7.02;    cp[2]:=7.2;
  34.   cp[3]:=7.43;    cp[4]:=7.67;
  35.   cp[5]:=7.88;    cp[6]:=8.06;
  36.   cp[7]:=8.21;    cp[8]:=8.34;
  37.   cp[9]:=8.44;    cp[10]:=8.53
  38. end;        { procedure get_data }
  39.  
  40.  
  41. procedure write_data;
  42. { print out the answers }
  43. var    i    : integer;
  44. begin
  45.   if first then first:=false else ClrScr;
  46.   writeln;
  47.   writeln;
  48.   writeln('  I      X       Y      YCALC    RESID');
  49.   for i:=1 to nrow do
  50.     writeln(i:3,x[i]:8:1,y[i]:9:2,y_calc[i]:9:2,resid[i]:9:2);
  51.   writeln; writeln(' Coefficients errors ');
  52.   writeln(coef[1],' ',sig[1],' Constant term');
  53.   for i:=2 to ncol do
  54.     writeln(coef[i],' ',sig[i]);        { other terms }
  55.   writeln;
  56.   writeln('Correlation coefficient is ',correl_coef:8:5)
  57. end;        { write_data }
  58.  
  59. {procedure square(x: ary2;
  60.          y: ary;
  61.          var a: ary2s;
  62.          var g: arys;
  63.      nrow,ncol: integer);}
  64. { matrix multiplication routine }
  65. { a= transpose x times x }
  66. { g= y times x }
  67. {$I SQUARE.LIB }
  68.  
  69. {external procedure gaussj(var b:    ary2s;
  70.                   y:    arys;
  71.               var coef:    arys;
  72.               ncol:        integer;
  73.               var error:    boolean);
  74. }
  75. {$I GAUSSJ.LIB }
  76.  
  77. { -> 217 }
  78. procedure linfit(X,    { independent variable }
  79.          y    : ary;    { dependent variable }
  80.     var y_calc    : ary;    { calculated dep. variable }
  81.     var resid    : ary;    { array of residuals }
  82.     var coef    : arys;    { coefficients }
  83.     var sig        : arys;    { error on coefficients }
  84.         nrow    : integer;    { length of ary }
  85.     var ncol    : integer);    { number of terms }
  86.  
  87. { least-squares fit to nrow sets of x and y pairs of points }
  88. { Seperate  procedure needed:
  89.     SQUARE -> form square coefficient matrix
  90.     GAUSSJ -> Gauus-Jordan elimination }
  91.  
  92. var    xmatr        : ary2;        { data matrix }
  93.     a        : ary2s;    { coefficient matrix }
  94.     g        : arys;        { constant vector }
  95.     error        : boolean;
  96.     i,j,nm        : integer;
  97.     xi,yi,yc,srs,see,
  98.     sum_y,sum_y2    : real;
  99.  
  100. begin        { procedure linfit }
  101.   ncol:=3;    { number of terms }
  102.   for i:=1 to nrow do
  103.     begin    { setup x matrix }
  104.     xi:=x[i];
  105.     xmatr[i,1]:=1.0;    { first column }
  106.     xmatr[i,2]:=xi;        { second column }
  107.     xmatr[i,3]:=1.0/sqr(xi)    { third column }
  108.     end;
  109.   square(xmatr,y,a,g,nrow,ncol);
  110.   gaussj(a,g,coef,ncol,error);
  111.   sum_y:=0.0;
  112.   sum_y2:=0.0;
  113.   srs:=0.0;
  114.   for i:=1 to nrow do
  115.     begin
  116.       yi:=y[i];
  117.       yc:=0.0;
  118.       for j:=1 to ncol do
  119.     yc:=yc+coef[j]*xmatr[i,j];
  120.       y_calc[i]:=yc;
  121.       resid[i]:=yc-yi;
  122.       srs:=srs+sqr(resid[i]);
  123.       sum_y:=sum_y+yi;
  124.       sum_y2:=sum_y2+yi*yi
  125.     end;
  126.   correl_coef:=sqrt(1.0-srs/(sum_y2-sqr(sum_y)/nrow));
  127.   if nrow=ncol then nm:=1
  128.   else nm:=nrow-ncol;
  129.   see:=sqrt(srs/nm);
  130.   for i:=1 to ncol do        { errors on solution }
  131.     sig[i]:=see*sqrt(a[i,i])
  132. end;        { LINFIT }
  133.  
  134. {external procedure plot(x,y,z: ary; nrow: integer);
  135. }
  136. {$I C:PLOT.LIB }
  137.  
  138. begin        { main program }
  139.   ClrScr;
  140.   first:=true;
  141.   done:=false;
  142.   writeln;
  143.   get_data(x,y,nrow);
  144.   repeat
  145.     repeat
  146.       write('Order of polynomial fit? ');
  147.       readln(ncol)
  148.     until ncol<5;
  149.     if ncol<1 then done:=true    { quit if ncol<1 }
  150.     else
  151.       begin
  152.     ncol:=ncol+1;        { order is one less }
  153.     linfit(x,y,y_calc,resid,coef,sig,nrow,ncol);
  154.     write_data;
  155.     plot(x,y,y_calc,nrow)
  156.     end    { else }
  157.   until done
  158. end.
  159.