function [root,ea,iter]=modsec(func,xr,delta,es,maxit,varargin)

% [root,ea,iter]=modsec(func,xr,delta, es,maxit,p1,p2,...):
%   uses modified secant method to find the root of func
% input:
%   func = name of function
%   delta = perturbation fraction. Should be small.
%   xr = initial guess
%   es = desired relative error (default = 0.0001%)
%   maxit = maximum allowable iterations (default = 50)
%   p1,p2,... = additional parameters used by function
% output:
%   root = real root
%   ea = approximate relative error (%)
%   iter = number of iterations

if nargin<2,error('at least two input arguments required'),end
if xr==0,error('nonzero initial guess required'),end
if nargin<3,delta=0.1;end
if nargin<4|isempty(es),es=0.0001;end
if nargin<5|isempty(maxit),maxit=50;end
iter = 0;
while (1)
  xrold = xr;
  
  if abs(xr)>1 d=0.1;
  else d=delta*xr; end
  
  
  xr = xr - delta*func(xr)/(func(xr+d)-func(xr));
  iter = iter + 1;
  if xr ~= 0, ea = abs((xr - xrold)/xr) * 100; end
  if ea <= es | iter >= maxit, break, end
end
root = xr;