accuracy of fzero in finallzeros.m

11 views (last 30 days)
Thijs
Thijs on 19 Apr 2012
Hi,
I am trying to solve a non-linear equation for multiple roots. I use a routine called findallzeros.m which calls fzero to find the roots. The routine works fine, however I can't figure out how to set the required accuracy (normally done by the TolX option of fzero) of the solution.
Any helpfull suggestions would be very much appreciated.
The matlab code:
.m
function x = findallzeros( fhandle, xguess, maxroots)
%
% x = findallzeros( fhandle, xguess, maxroots)
% ==================================
%
% attemps to find multiple roots of a function.
%
% fhandle = function handle, prefix by @
% xguess = a scalar value used as initial guess
% ( = 0 if not supplied )
% maxroots = the number of roots needed
% ( = 1 if not supplied )
% (c) 2005 by Tomas Co
% @ Michigan Technological University
x = [ ];
if nargin==1
xguess =0;
maxroots = 1;
end
if nargin==2
maxroots = 1;
end
nroots = 0;
while nroots<maxroots
if abs(feval(fhandle,xguess,[ ]))<=1e-16,
xroot=xguess;
% xguess results in zero value of objective function fhandle
% --> xroot = xguess
else
% options = optimset('TolX',2*eps(x));
% options = optimset('TolX',1e-12);
[xroot,val,flag,output] = fzero(fhandle,xguess,[ ],x);
% val is the value of the objective function (should converge
% to zero).
% x is the vector of old roots ?
% flag describes the exit condition of fzero;
% when flag = 1, fzero converged to solution xroot;
% when flag < 0, fzero could not find a solution
if flag <= 0
return
end
end
x=[x,xroot];
nroots=nroots+1;
xguess=xguess+0.1;
end
the function:
function f = test( x,oldroots )
% this funcion is called by the findallzeros function to find all roots
% of function f
aspectratio = 5; %aspect ratio of spherocylinder: L/D
denom = pi./6 + (pi./4).*aspectratio;
C1STAR = 2.*aspectratio.^(2)./denom; %coefficient one of excluded volume expression
%--------------------------------------------------------------------------
% calculate Aresn_alpha = f
%--------------------------------------------------------------------------
y = 0.425; %packing fraction
G = (4.*y - 3.*y.^2)./(8.*(1-y).^2); %scaling factor: Areshs/8Nkt
% Calculate modified Bessel functions I2 and I3
I2 = besseli(2,2.*x);
I3 = besseli(3,2.*x);
I2prime = 2.*(I3 + I2./x); %derivative of I2(2*alpha) to alpha
Q1 = G.*C1STAR.*pi;
dummy1 = (1/sinh(x).^2).*( I2prime./2 - coth(x).*I2 );
f = 1./x - atan(sinh(x)).*cosh(x)./sinh(x).^2 + Q1.*dummy1;
if length( oldroots ) > 0 ,
f = f/polyval( poly ( oldroots ), x );
end
end
Note that fhandle = @test.
Thanks in advance!
Thijs

Answers (1)

Walter Roberson
Walter Roberson on 19 Apr 2012
Uncomment the line
% options = optimset('TolX',1e-12);
and change the tolerance to whatever you want.
Then on the next line, change
[xroot,val,flag,output] = fzero(fhandle,xguess,[ ],x);
to
[xroot,val,flag,output] = fzero(@(g) fhandle(g,x),xguess,options);
Warning: the existing code is not correct even for using the default tolerance. Unless perhaps it was supported in a much earlier MATLAB, additional arguments cannot be passed to the target function by including the arguments directly in the fzero() call.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!