]> gitweb.michael.orlitzky.com - octave.git/blob - has_root.m
Add even/odd Octave functions.
[octave.git] / has_root.m
1 function has_root = has_root(fa, fb)
2 ## Use the intermediate value theorem to determine whether or not some
3 ## function has an odd number of roots on an interval. If the function
4 ## in question has an even number of roots, the result will be
5 ## incorrect.
6 ##
7 ## Call the function whose roots we're concerned with 'f'. The two
8 ## parameters `fa` and `fb` should correspond to f(a) and f(b).
9 ##
10 ##
11 ## INPUTS:
12 ##
13 ## * ``fa`` - The value of `f` at one end of the interval.
14 ##
15 ## * ``fb`` - The value of `f` at the other end of the interval.
16 ##
17 ## OUTPUTS:
18 ##
19 ## * ``has_root`` - True if we can use the I.V.T. to conclude that
20 ## there is a root on [a,b], false otherwise.
21 ##
22
23 ## If either f(a) or f(b) is zero, the product of their signs will be
24 ## zero and either a or b is a root. If the product of their signs is
25 ## negative, then f(a) and f(b) are non-zero and have opposite sign,
26 ## so there must be a root on (a,b). The only case we don't want is
27 ## when f(a) and f(b) have the same sign; in this case, the product of
28 ## their signs would be one.
29 if (sign(fa) * sign(fb) != 1)
30 has_root = true;
31 else
32 has_root = false;
33 end
34 end