Counting zeros
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (3 件)
Andrew Newell
2012 年 1 月 10 日
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 件のコメント
Andrew Newell
2012 年 1 月 10 日
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
2012 年 1 月 10 日
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 件のコメント
Walter Roberson
2012 年 1 月 10 日
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 件のコメント
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!