ProjectMOSAIC / mosaicCalc

Calculus in R
12 stars 4 forks source link

Zeros() doesn't identify points of tangency as with 1 + sin(x) #19

Open dtkaplan opened 1 year ago

dtkaplan commented 1 year ago

The source of the difficulty is that 1+sin(x) does not cross zero; it is tangent to zero. 0.99999 + sin(x) does indeed cross zero. The different behaviors of findZeros() and Zeros() stem from a simple source: The number of sub-divisions used to hunt for zero crossings. findZeros() uses 1000 segments, but Zeros() uses fewer. You can tell Zeros() to look more carefully by giving an additional argument, say nsegs=1001. The documentation for Zeros() should be corrected to say that it looks for "zero crossings." That's easy enough to do. If you would like to have a "tangent at Zero" function, we can add that in. It requires some attention to the numerical tolerance. I think the easiest thing would be to modify Zeros() to have a tangents= arguments. If true, then the function would be differentiated and a zero-search done on that. This will robustly find argmaxes and argmins, which can be checked to see how close the corresponding function values are to zero. Another approach would be to search for zero-crossings on three functions: f(x), f(x)+eps, f(x)-eps. If any of these makes a hit, a tangency can be checked for.