Issue with the root-finding fzero function
2 views (last 30 days)
Show older comments
I made this below function to obtain a critical value, which is a root to another function involving an integral. I was supposed to get an answer to crit(5, 2, 0.2104, 60, 0.05) but Matlab gives me this error message(bounded by ~~~~~~~~). Could somebody figure out where this problem comes from, the integration or the root-finding method fzero? fzero looks more suspicious to me. I wonder if replacing fzero with another root-finding method would resolve the issue.
Your suggestions are highly appreciated.
Amy
function c=crit(r,s,q,nu,al)
a=q.*(1+q.^2).^(-1./2);
b=(1+q.^2).^(-1./2);
h=@intd;
function prob=intd(x)
E=@(t) fcdf((r-s)./s.*(sqrt(x).*(r.*t-x).^(1/2)- a.*b.*r.*t).^2./(a.^2.*r.*t-x).^2,s,r-s).*fpdf(t,r,nu) ;
up=x./(b.^2)./r;
down=x./r;
prob=fcdf(x/r,r,nu)+quadv(E,down,up)-1+al;
end
c=fzero(h,r*finv(1-al,r,nu),1e-6);
end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error using betainc Inputs must be real, full, and double or single.
Error in fcdf (line 58) p(kk) = betainc(xx, v1(kk)/2, v2(kk)/2,'lower');
Error in crit>@(t)fcdf((r-s)./s.*(sqrt(x).*(r.*t-x).^(1/2)-a.*b.*r.*t).^2./(a.^2.*r.*t-x).^2,s,r-s).*fpdf(t,r,nu) (line 7) E=@(t) fcdf((r-s)./s.*(sqrt(x).*(r.*t-x).^(1/2)-a.*b.*r.*t).^2./(a.^2.*r.*t-x).^2,s,r-s).*fpdf(t,r,nu) ;
Error in quadv (line 61) y{j} = feval(f, x(j), varargin{:}); %#ok<AGROW>
Error in crit/intd (line 10) prob=fcdf(x/r,r,nu)+quadv(E,down,up)-1+al;
Error in fzero (line 343) a = x - dx; fa = FunFcn(a,varargin{:});
Error in crit (line 13) c=fzero(h,r*finv(1-al,r,nu),1e-6); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 Comments
Answers (1)
Walter Roberson
on 3 Feb 2013
If you "dbstop if error" and look at the class() of the parameters to betainc(), then are they all double? If they are, check to see if one of them has an imaginary component. Is your intd() well defined if x is negative? You might want to put a range for your x0, to prevent x from going negative.
Why are you passing three arguments to fzero? Your third argument is not an options structure.
See Also
Categories
Find more on Software Development Tools 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!