Page 161 - Compact Numerical Methods For Computers
P. 161

150               Compact numerical methods for computers
                             Algorithm 16. Grid search along a line (cont.)
                               var
                                 j : integer;
                                 h, p, t : real;
                                 notcomp : boolean;
                               begin
                                 writeln(‘alg16.pas-- one-dimensional grid search’);
                                 {STEP 0 via the procedure call}
                                 writeln(‘In gridsrch 1bound=‘,lbound,’ubound=‘,ubound);
                                 notcomp:=false; {function must be called with notcomp false or
                                          root will be displayed -- STEP 1}
                                 t:=fnld(lbound, notcomp); {compute function at lower bound and set
                                          pointer k to lowest function value found so far i.e. the 0’th}
                                 writeln(’lbf(‘,lbound,‘)=‘,t);
                                 if notcomp then halt;
                                 fmin:=t; {to save the function value}
                                 minarg:=0; {so far this is the lowest value found}
                                 changarg:=0; {as a safety setting of this value}
                                 h:=(ubound-lbound)/nint;
                                 for j:=l to nint do {STEP 2}
                                 {Note: we increase the number of steps so that the upper bound ubound is
                                    now a function argument. Warning: because the argument is now
                                    calculated, we may get a value slightly different from ubound in
                                    forming (lbound+nint*h). }
                                 begin
                                    p:=fn1d(lbound+j*h, notcomp); {STEP 3}
                                    write(’f(‘,lbound+j*h,‘)=‘,p);
                                    if notcomp then halt;
                                    if p<fmin then {STEP 4}
                                    begin {STEP 5}
                                       fmin:=p; minarg:=j;
                                    end; {if p<fmin}
                                    if p*t<=0 then
                                    begin
                                       writeln(‘ *** sign change ***‘);
                                       changarg:=j; {to save change point argument}
                                    end
                                    else
                                    begin
                                       writeln; {no action since sign change}
                                    end;
                                    t:=p; {to save latest function value} {STEP 6}
                                 end; {loop on j}
                                 writeln( ‘Minimum so far is f( ‘,lbound+minarg*h,‘)=‘,fmin);
                                 if changarg>0 then
                                 begin
                                    writehr(‘Sign change observed last in interval ‘);
                                    writeln(‘[‘,lbound+(changarg-l)*h,‘,‘,lbound+changarg*h,‘]‘);
                                 end
                                 else
                                    writeln(‘Apparently no sign change in [ ‘,lbound,‘,‘,ubound,‘]‘);
                               end; {alg16.pas == gridsrch}
   156   157   158   159   160   161   162   163   164   165   166