[roots, possroots] = AllSolve(a,b,fa,fb,L,tol) % Find all roots (and possible roots) of f(x) = 0 for x in [a,b]. % Note: This algorithm's main purpose is to illustrate recursion. if |b-a| < tol if sgn(fb) == sgn(fa) roots = []; possroots = [a,b]; return else roots = [a,b]; possroots = []; return end end % Note: The following test could be improved, but % for this study, do not improve it. if sgn(fa - L |b-a|) == sgn(fa) and sgn(fa + L |b-a|) == sgn(fa) (or if sgn(fb - L |b-a|) == sgn(fb) and sgn(fb + L |b-a|) == sgn(fb)) roots = []; possroots = []; else m = a + (b-a)/2; fm = f(m); [r1,p1] = AllSolve(m,b,fm,fb,L,tol); [r2,p2] = AllSolve(a,m,fa,fm,L,tol); roots = [r1;r2]; possroots = [p1;p2]; end