reverse-shooting

Matlab scripts for reverse shooting
Log | Files | Refs | README

fzerochad.m (723B)


      1 function x=fzerochad(f,x0,factor,NumTries);
      2 
      3 % Wrapper around fzero: x0=[xlow xhi].  Repeatedly update
      4 %  by factor until find a sign change:  [xlow/factor xhi*factor].
      5 %
      6 %  f is the function to call.  We're looking for x s.t. f(x)=0
      7 
      8 if exist('factor')~=1; factor=2; end;
      9 if exist('NumTries')~=1; NumTries=5; end;
     10 x00=x0;
     11 
     12 sign1=sign(f(x00(1)));
     13 sign2=sign(f(x00(2)));
     14 i=1;
     15 while sign1==sign2 & i<NumTries;
     16   x00(1)=x00(1)/factor;
     17   x00(2)=x00(2)*factor;
     18   sign1=sign(f(x00(1)));
     19   sign2=sign(f(x00(2)));
     20   i=i+1;
     21 end;
     22 if sign1==sign2; disp 'No sign change found in fzerochad. Stopping...'; keyboard; 
     23 %if sign1==sign2; disp 'No sign change found in fzerochad. Assigning a NaN...';
     24   x=NaN;
     25 else; 
     26   x=fzero(f,x00);
     27 end;
     28