File Exchange

## rootSolve(func, xLow, xUpp, tol)

version 1.21.0.0 (2.41 KB) by
This function uses Ridder's Method to do non-linear root finding

Updated 24 Apr 2017

This function uses Ridder's Method to do non-linear root finding. This method works well for smooth scalar functions, and requires a bounded search space.
% XZERO = ROOTSOLVE(FUNC, XLOW, XUPP, TOL)
%
% FUNCTION: This function uses Ridder's Method to return a root, xZero,
% of func on the interval [xLow,xUpp]
%
% INPUTS:
% func = a function for a SISO function: y = f(x)
% xLow = the lower search bound
% xUpp = the upper search bound
% tol = return xZero if abs(func(xZero)) < tol
%
% OUTPUTS:
% xZero = the root of the function on the domain [xLow, xUpp]
%
% NOTES:
% 1) The function must be smooth
% 2) sign(f(xLow)) ~= sign(f(xUpp))
% 3) This function will return a root if one exists, and the function is
% not crazy. If there are multiple roots, it will return the first one
% that it finds.

### Cite As

Matthew Kelly (2021). rootSolve(func, xLow, xUpp, tol) (https://www.mathworks.com/matlabcentral/fileexchange/54458-rootsolve-func-xlow-xupp-tol), MATLAB Central File Exchange. Retrieved .

Matthew Kelly

You can test the function by running it without arguments. The example code is included at the bottom of the rootSolve.m file. The minimal syntax to compute the root of `exp(-x) - x = 0` is: `xRoot = rootSolve(@(x)(exp(-x)-x), -2,2)`, which searches for roots of the function on the domain [-2,2]. The solution is: xRoot = 0.567143290409784, which visually matches the plot of the function.

tbaracu

how i can run this code in Matlab ? I cannot make it solve exp(-x)-x = 0

Matthew Kelly

Great point. I do use fzero for the most root-finding applications. I wrote this function for two reasons:
1) I use Matlab Coder to compile much of my code to MEX. The fzero command is not compatible with Matlab Coder (at least at the time of writing), while this implementation is (with slight modifications).
2) This code was used as a test implementation (checking results against fzero) for a root-solving function that I wrote in Java for a contact solver in a physics engine.

Duane Hanselman

This method is one of several used within the standard MATLAB function fzero. fzero changes methods based on the results of each iteration. Doing so make the function quite robust. So, I don't know what value this function has.

##### MATLAB Release Compatibility
Created with R2012a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux