# Finding Roots of a Function [closed]

I'm very new to Mathematica.

I am trying to find the roots of a function in terms of t, here is my try and the output:

p[y_, t_, k_] := ((1 - E^(-((-1 + y)^2/t))) t)/(-1 + y)^2 -
((1 - E^(-((1 + y)^2/t))) t)/(1 + y)^2 - k

FindRoot[p[y, t, 0.5], {y, 1}]

During evaluation of In[3]:= Power::infy: Infinite expression 1/0.^2 encountered. >>

During evaluation of In[3]:= Infinity::indet: Indeterminate expression 0. t ComplexInfinity encountered. >>

During evaluation of In[3]:= FindRoot::nlnum: The function value {Indeterminate} is not a list of numbers with dimensions {1} at {y} = {1.}. >>

(* FindRoot[p[y, t, 0.5], {y, 1}] *)


I also tried NSolve:

p[y_, t_, k_]:= ((1 - E^(-((-1 + y)^2/t))) t)/(-1 + y)^2 -
((1 - E^(-((1 + y)^2/t))) t)/(1 + y)^2 - k

t = Table[i, {i, 0, 5}]
NSolve[p[y, t, 0.5], y]


but got error messages about division by zero. However, I am suspecting the starting value for y = 1, which will then evolve with time t. I will appreciate any insight into solving for Roots of this type of equation.

## closed as off-topic by Michael E2, Wjx, ilian, gwr, MarcoBOct 2 '16 at 0:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Michael E2, Wjx, ilian, gwr, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.

• You pass 1 as the initial value for y in FindRoot, when your function has -1+y in the denominator -- shouldn't you expect a divide by zero error? – Michael E2 Sep 29 '16 at 1:36
• Indeed one would expect zero, however, the function is such that the starting value for 'y=1' at 't=0', which then evolves with time. Beyond 't~3.75', solution no longer exist for the function. – D. Andrew Sep 29 '16 at 3:17

You can use NSolve by restricting the domain to Reals and specifying a WorkingPrecision rather than using MachinePrecision

p[y_, t_, k_] :=
((1 - E^(-((-1 + y)^2/t))) t)/(-1 + y)^2 -
((1 - E^(-((1 + y)^2/t))) t)/(1 + y)^2 - k;


Including verification of the solution

Module[{soln},
{"t=" <> ToString[#, TraditionalForm],
soln = NSolve[p[y, #, 1/2] == 0, y, Reals,
WorkingPrecision -> 15],
If[Length[soln] > 0, And @@ (p[y, #, 1/2] == 0 /. soln),
$Failed]} & /@ Range[1/2, 4, 1/2]] // Grid  EDIT: Plotting contour for p[y, t, 1/2] == 0 Finding the approximate {t, y} point for the largest value of t pt1 = FindArgMax[{t, p[y, t, 1/2] == 0}, {{t, 3.5}, {y, 1.8}}] (* {3.76655, 1.87905} *)  Finding the approximate {t, y} point for the smallest value of t pt2 = FindArgMin[{t, p[y, t, 1/2] == 0, t >= 0}, {{t, 1/100}, {y, 99/100}}, WorkingPrecision -> 20] // N (* {9.58573*10^-9, 0.999876} *)  ContourPlot with extreme points ContourPlot[p[y, t, 1/2] == 0, {t, 0, 3.8}, {y, 0.4, 2.35}, Exclusions -> {t == 0}, FrameLabel -> (Style[#, 14, Bold] & /@ {t, y}), Epilog -> {Red, AbsolutePointSize[4], Point[{pt1, pt2}]}]  • Thank you, Bob for the clarification, Indeed the function has two roots at every given time except at t = 0, where y1=y2=1 and at t~3.75 with y1=y2~1.91. These were values given by an author for the problem. I have used your code to generate all the needed values.I will eventually have to generate some kind of plots with the solution set. Thanks. – D. Andrew Sep 29 '16 at 3:19 Your starting point is more at 0.5 than 1 (at least for$t\in(0,1)\$:

ContourPlot[p[y, t, 0.5], {y, 0, 1}, {t, 0, 1}, PlotLegends -> Automatic]


Table[y /. FindRoot[p[y, i, 0.5], {y, 0.5}], {i, 0.1, 1, 0.1}]


{0.625639, 0.514341, 0.460898, 0.438802, 0.434756, 0.44139, 0.454654, \ 0.472282, 0.492957, 0.515882}

But:

so

Table[y /. FindRoot[p[y, i, 0.5], {y, 0.5 i}], {i, {1, 2.1, 3}}]


{0.515882, 0.846635, 1.21487}

There appears to be some problem (probably with the convergence of the method) at t = 2, so I took 2.1 instead.

The 1/0 warning is related to the initial point; see the docs for a description.

• Thanks @ Corey979. I wanted to find the values of y as a function of t as demonstrated by Bob Halon. For every given time, there are two values for y except at times t =0 and t~3.75. Some of the roots you generated are also listed in Bob's answer. – D. Andrew Sep 29 '16 at 3:21