home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / PAS_ENG.ZIP / LEAST3.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-07-19  |  3.8 KB  |  157 lines

  1. program least3;  { --> 209 }
  2. { Pascal program to perform a linear least-squares fit }
  3. { with Gauss-Jordan routine }
  4. { Sperate modules needed:
  5.    GAUSSJ,
  6.    PLOT }
  7.  
  8. const maxr = 20;  { data prints }
  9.  maxc = 4;  { 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.  
  24. procedure get_data(var x: ary;  { independant variable }
  25.      var y: ary;  { dependant variable }
  26.      var nrow: integer); { length of vectors }
  27. { get values for n and arrays x,y }
  28.  
  29. var i : integer;
  30.  
  31. begin
  32.   nrow:=9;
  33.   for i:=1 to nrow do x[i]:=i;
  34.   y[1]:=2.07; y[2]:=8.6;
  35.   y[3]:=14.42; y[4]:=15.8;
  36.   y[5]:=18.92; y[6]:=17.96;
  37.   y[7]:=12.98; y[8]:=6.45;
  38.   y[9]:=0.27;
  39. end;  { proceddure get data }
  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. procedure linfit(x,  { independant variable }
  78.    y: ary; { dependent variable }
  79.    var y_calc: ary; { calculated dep. variable }
  80.    var resid:  ary; { array of residuals }
  81.    var coef:   arys; { coefficients }
  82.    var sig:    arys; { error on coefficients }
  83.    nrow:       integer; { length of array }
  84.    var ncol:   integer); { number of terms }
  85.  
  86. { least squares fit to nrow sets of x and y pairs of points }
  87. { Seperate procedures needed:
  88.  SQUARE -> form square coefficient matrix
  89.  GAUSSJ -> Gauss-Jordan elimination }
  90.  
  91. var xmatr  : ary2;  { data matrix }
  92.  a  : ary2s; { coefficient matrix }
  93.  g  : arys;  { constant vector }
  94.  error  : boolean;
  95.  i,j,nm  : integer;
  96.  xi,yi,yc,srs,see,
  97.  sum_y,sum_y2 : real;
  98.  
  99. begin  { procedure linfit }
  100.   for i:=1 to nrow do
  101.     begin  { setup matrix }
  102.       xi:=x[i];
  103.       xmatr[i,1]:=1.0; { first column }
  104.       for j:=2 to ncol do { other columns}
  105.  xmatr[i,j]:=xmatr[i,j-1]*xi
  106.     end;
  107.   square(xmatr,y,a,g,nrow,ncol);
  108.   gaussj(a,g,coef,ncol,error);
  109.   sum_y:=0.0;
  110.   sum_y2:=0.0;
  111.   srs:=0.0;
  112.   for i:=1 to nrow do
  113.     begin
  114.       yi:=y[i];
  115.       yc:=0.0;
  116.       for j:=1 to ncol do
  117.  yc:=yc+coef[j]*xmatr[i,j];
  118.       y_calc[i]:=yc;
  119.       resid[i]:=yc-yi;
  120.       srs:=srs+sqr(resid[i]);
  121.       sum_y:=sum_y+yi;
  122.       sum_y2:=sum_y2+yi*yi
  123.     end;
  124.   correl_coef:=sqrt(1.0-srs/(sum_y2-sqr(sum_y)/nrow));
  125.   if nrow=ncol then nm:=1
  126.   else nm:=nrow-ncol;
  127.   see:=sqrt(srs/nm);
  128.   for i:=1 to ncol do  { errors on solution }
  129.     sig[i]:=see*sqrt(a[i,i])
  130. end; { linfit }
  131.  
  132. {external procedure plot(x,y,z: ary; nrow: integer);
  133. }
  134. {$I PLOT.LIB }
  135.  
  136. begin  { main program }
  137.   clrscr;
  138.   first:=true;
  139.   done:=false;
  140.   writeln;
  141.   get_data(x,y,nrow);
  142.   repeat
  143.     repeat
  144.       write('Order of polynomial fit? ');
  145.       readln(ncol)
  146.     until ncol<5;
  147.     if ncol<1 then done:=true { quit if ncol<1 }
  148.     else
  149.       begin
  150.  ncol:=ncol+1;  { order is one less }
  151.  linfit(x,y,y_calc,resid,coef,sig,nrow,ncol);
  152.  write_data;
  153.  plot(x,y,y_calc,nrow)
  154.     end { else }
  155.   until done
  156. end.
  157.