allcrossings: locate all intersections of a pair of functions f1 and f2, on a finite domain
usage: Xcross = allcrossings(f1,f2,Xsupport,nsamples)
allcrossings uses fzero as a search engine for the roots of f1(x) - f2(x) == 0,
once it identifies a bracket that contains a root.
If your functions have discontinuities in them, such as tan(x), then an
identified crossing may be spurious, in the sense that f1(x)~=f2(x),
but the two functions should still exhibit a crossing at that location.
arguments: (input)
f1,f2 - a pair of function handles. It is assumed that f1 and f2 are
both vectorized functions, so they can be evaluated at multiple
locations in one call.
if you are trying to solve problems of the form f1(x) == k, then
f2 can be passed in as a scalar constant, or you can use
f2 = @(x) k + zeros(size(x));
as a properly vectorized function.
Xsupport - vector of length 2 that defines the lower and upper limits of
the domain where the search will be done.
nsamples - (optional) integer argument that indicates the number of
points over that domain to test, looking for where there may be
an intersection. At a minimum, nsamples will always be at least
100.
Default value: nsamples = 500;
fzeroOptions - (optional) if supplied, it must abe a valid struct
containing the options used by fzero. The options that fzero will look
for are: {Display, TolX, FunValCheck, OutputFcn, and PlotFcns}. The
values assumed will otherwise be:
Display: 'none'
TolX: 2.2204e-16
FunValCheck: 'off'
OutputFcn: []
PlotFcns: []
arguments: (output)
Xcross - vector of locations where f1(x) == f2(x)
Examples:
Intersection points of sin(x) == cos(x), between -10 and +10.
f1 = @(x) sin(x);
f2 = @(x) cos(x);
xcross = allcrossings(f1,f2,[-10,10],50)
xcross =
-8.6394 -5.4978 -2.3562 0.7854 3.927 7.0686
Example:
Positive solutions to the problem x + tan(x) == 1
f1 = @(x) 2*x + tan(x);
f2 = 1;
xcross = allcrossings(f1,f2,[0,20],100)
xcross =
0.32919 1.5708 1.9113 7.854 7.9213 14.137 14.174
f1(xcross)
ans =
1 1.3748e+15 1 -2.4185e+14 1 -2.0929e+14 1
As you can see, there are crossings found at certain points that are
not technically solutions, but due to the jump in the tan function, they
were still identified by fzero as "crossings". These are spurious solutions.
see also: fzero, fzolve, solve, vpasolve
author: John D'Errico
e-mail: eoodchips@rochester.rr.com
Date: 2/28/2023