# Finding points of intersection of a function and x-axis [duplicate]

I have a function of the form $2e^{-x}\cos(10x)-1$. I want to find all (or atleast some of ) the points where this function meets the $x$-axis. If I plot this function, I can see that it crosses $x$-axis at some points,

Plot[2Exp[-x]*Cos[10*x]-1,{x,0,100}]


All I need is the collection of all the points $x$ where this function crosses $x$-axis.

• NSolve[2 Exp[-x]*Cos[10*x] - 1 == 0 && 0 < x < 100]? (Or use Solve instead of NSolve.) – Michael E2 Jan 9 '18 at 14:19
• But that will give only one point at a time. Is there a way of generating a collection of such points in jus one go? – H. Kenan Jan 9 '18 at 14:46
• NSolve finds three solutions {{x -> 0.0986244}, {x -> 0.582049}, {x -> 0.655564}}... – Ulrich Neumann Jan 9 '18 at 14:48

f[x_] = 2 Exp[-x]*Cos[10*x] - 1;

sol = NSolve[{f[x] == 0, 0 < x < 100}, x, WorkingPrecision -> 15]

(* {{x -> 0.0986244282727579}, {x -> 0.582048501221754},
{x -> 0.655563712011485}} *)


Verifying the solutions

And @@ (f[x] == 0 /. sol)

(* True *)


The function is more readily seen using LogLinearPlot

LogLinearPlot[f[x], {x, 0.01, 100},
Epilog -> {Red, AbsolutePointSize[4], Point[{Log[x], f[x]} /. sol]}]


f[0]

(* 1 *)

f[100] // N[#, 20] &

-1.0000000000000000000

Limit[f[x], x -> Infinity]

-1


I think the following plot finds all the roots of the derivative:

plot = Plot[Min[Max[-2 E^-x Cos[10 x] - 20 E^-x Sin[10 x], -#], #] &[10^-50], {x, 0, 100},
PlotRange -> All, Exclusions -> None, PlotPoints -> 1000]


When the absolute value is above 10^-50 I made the function constant to avoid Plot dealing with unnecessary details.

We can extract the line pieces where the sign change and use FindRoot on each of the found intervals:

intervals = Join @@ Map[With[{which = Position[Differences[Sign[#[[All, 2]]]],
Except[0], {1}, Heads -> False][[All, 1]]},
Transpose[{#[[which, 1]], #[[which + 1, 1]]}]] &,
Cases[plot, Line[a___] -> a, {0, ∞}]];

res = x /. (FindRoot[-2 E^-x Cos[10 x] - 20 E^-x Sin[10 x], {x, (# + #2)/2, #, #2}] & @@@ intervals)

{0.3041924,0.61835167,0.93251093,1.2466702,1.5608295,1.8749887,2.189148,2.5033073,2.8174665,3.1316258,3.4457851,3.7599443,4.0741036,4.3882628,4.7024221,5.0165814,5.3307406,5.6448999,5.9590592,6.2732184,6.5873777,6.901537,7.2156962,7.5298555,7.8440148,8.158174,8.4723333,8.7864926,9.1006518,9.4148111,9.7289704,10.04313,10.357289,10.671448,10.985607,11.299767,11.613926,11.928085,12.242244,12.556404,12.870563,13.184722,13.498882,13.813041,14.1272,14.441359,14.755519,15.069678,15.383837,15.697996,16.012156,16.326315,16.640474,16.954633,17.268793,17.582952,17.897111,18.211271,18.52543,18.839589,19.153748,19.467908,19.782067,20.096226,20.410385,20.724545,21.038704,21.352863,21.667022,21.981182,22.295341,22.6095,22.92366,23.237819,23.551978,23.866137,24.180297,24.494456,24.808615,25.122774,25.436934,25.751093,26.065252,26.379411,26.693571,27.00773,27.321889,27.636048,27.950208,28.264367,28.578526,28.892686,29.206845,29.521004,29.835163,30.149323,30.463482,30.777641,31.0918,31.40596,31.720119,32.034278,32.348437,32.662597,32.976756,33.290915,33.605075,33.919234,34.233393,34.547552,34.861712,35.175871,35.49003,35.804189,36.118349,36.432508,36.746667,37.060826,37.374986,37.689145,38.003304,38.317464,38.631623,38.945782,39.259941,39.574101,39.88826,40.202419,40.516578,40.830738,41.144897,41.459056,41.773215,42.087375,42.401534,42.715693,43.029852,43.344012,43.658171,43.97233,44.28649,44.600649,44.914808,45.228967,45.543127,45.857286,46.171445,46.485604,46.799764,47.113923,47.428082,47.742241,48.056401,48.37056,48.684719,48.998879,49.313038,49.627197,49.941356,50.255516,50.569675,50.883834,51.197993,51.512153,51.826312,52.140471,52.45463,52.76879,53.082949,53.397108,53.711268,54.025427,54.339586,54.653745,54.967905,55.282064,55.596223,55.910382,56.224542,56.538701,56.85286,57.167019,57.481179,57.795338,58.109497,58.423656,58.737816,59.051975,59.366134,59.680294,59.994453,60.308612,60.622771,60.936931,61.25109,61.565249,61.879408,62.193568,62.507727,62.821886,63.136045,63.450205,63.764364,64.078523,64.392683,64.706842,65.021001,65.33516,65.64932,65.963479,66.277638,66.591797,66.905957,67.220116,67.534275,67.848434,68.162594,68.476753,68.790912,69.105072,69.419231,69.73339,70.047549,70.361709,70.675868,70.990027,71.304186,71.618346,71.932505,72.246664,72.560823,72.874983,73.189142,73.503301,73.81746,74.13162,74.445779,74.759938,75.074098,75.388257,75.702416,76.016575,76.330735,76.644894,76.959053,77.273212,77.587372,77.901531,78.21569,78.529849,78.844009,79.158168,79.472327,79.786487,80.100646,80.414805,80.728964,81.043124,81.357283,81.671442,81.985601,82.299761,82.61392,82.928079,83.242238,83.556398,83.870557,84.184716,84.498876,84.813035,85.127194,85.441353,85.755513,86.069672,86.383831,86.69799,87.01215,87.326309,87.640468,87.954627,88.268787,88.582946,88.897105,89.211264,89.525424,89.839583,90.153742,90.467902,90.782061,91.09622,91.410379,91.724539,92.038698,92.352857,92.667016,92.981176,93.295335,93.609494,93.923653,94.237813,94.551972,94.866131,95.180291,95.49445,95.808609,96.122768,96.436928,96.751087,97.065246,97.379405,97.693565,98.007724,98.321883,98.636042,98.950202,99.264361,99.57852,99.89268}


The maxima (i.e. those with negative second derivative) are chosen and plotted:

ListPlot[Transpose[{#, 2 Exp[-#]*Cos[10*#] - 1}]] &[
Pick[res,
Sign[D[-2 E^-x Cos[10 x] - 20 E^-x Sin[10 x], x] /. x -> res],
-1]]


• Thank you very much. Is it possible to find the Maximum values of this function and see how the maximum varies with x? – H. Kenan Jan 9 '18 at 15:10
• @user149973 Just replace the function by its derivative $-20 e^{-x} \sin (10 x)-2 e^{-x} \cos (10 x)$. Then the exact same code returns the x-values of maximums and minimums. – Coolwater Jan 9 '18 at 15:14