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