Counting zeros
2 views (last 30 days)
Show older comments
Given a function f(x), Is there a simple way to get MATLAB to determine the number of zeros it has in some arbitrary interval [a,b], without necessarily finding them explicitly? Perhaps counting the number of sign changes or whatnot?
Thanks.
0 Comments
Answers (3)
Andrew Newell
on 10 Jan 2012
In general, no. You can try doing it by counting sign changes, but there is no guarantee you'll catch all the answers. However, we might be able to suggest a better approach if you post the code for f(x).
2 Comments
Andrew Newell
on 10 Jan 2012
Not knowing anything about the function, I'd suggest picking some regularly spaced points in [a,b] and counting sign changes, then add more points and see if anything changes.
Dr. Seis
on 10 Jan 2012
I know you don't need to find the roots explicitly, but this will work:
p = [1 -6 -72 -27]; % polynomial coeffs for f(x) = x^3 - 6*x^2 - 72*x - 27
a = -20; % lower interval bound
b = 0; % upper interval bound
r = roots(p); % roots
n = sum((a <= r).*(r <= b)); % number of roots in interval [a,b]
disp(n);
If you really want to search, then:
f = @(x)x.^3 - 6*x.^2 - 72*x - 27; % Define function
n = 0; % Intialize n to 0
dx = 0.01; % set increment in x
for i = a+dx : dx : b
if sign(f(i)) ~= sign(f(i-dx))
n=n+1;
end
end
disp(n);
Though, I am sure there must be a much more savvy way of doing this.
0 Comments
Walter Roberson
on 10 Jan 2012
For arbitrary functions, there is no way of doing this, not even by counting sign changes. If f(x) is negative and f(x+t) is positive then the best you can say is that f might be discontinuous or might have at least one zero in the interval x to x+t .
For certain classes of functions, such as polynomials, if you are given the symbolic form, it becomes possible to find the roots within the limits of round-off.
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!