2
$\begingroup$

Suppose we want to define Tanh-function via exponent:

th[x_] := (1 - Exp[-2 x])/(1 + Exp[-2 x]);
FullSimplify[th[x]]

Out[13]= Tanh[x]

Then we define new function:

g[x_] := Sqrt[1 + th[x]]

Simplify[g'[x]]
Out[16]= Sqrt[2] E^(-2 x) (E^(2 x)/(1 + E^(2 x)))^(3/2)

Now try to plot g'[x]:

Plot[g'[x], {x, -60, 60}, PlotRange -> All]

And we got errors:

Power::infy: Infinite expression 1/Sqrt[0.] encountered. >>
Infinity::indet: Indeterminate expression 0. ComplexInfinity encountered. >>
Power::infy: Infinite expression 1/Sqrt[0.] encountered. >>
Infinity::indet: Indeterminate expression 0. ComplexInfinity encountered. >>
Power::infy: Infinite expression 1/Sqrt[0.] encountered. >>
General::stop: Further output of Power::infy will be suppressed during this calculation. >>
Infinity::indet: Indeterminate expression 0. ComplexInfinity encountered. >>
General::stop: Further output of Infinity::indet will be suppressed during this calculation. >>

We have right plot, but why do these errors occur?

It's interesting that when we plot, for example, from -10 to 10, no errors occur.

$\endgroup$
  • $\begingroup$ Simplify before plotting Plot[Evaluate[g'[x] // Simplify], {x, -60, 60}, PlotRange -> All] $\endgroup$ – Bob Hanlon Oct 11 '15 at 17:26
  • $\begingroup$ Yes, it works. But the question is - Why do such errors occur? $\endgroup$ – newt Oct 11 '15 at 17:28
  • 1
    $\begingroup$ It is a numerical precision issue at an intermediate step caused by the large exponents to E. Look at Trace[g'[-60.]] compared with Trace[(g'[x] // Simplify) /. x -> -60.] $\endgroup$ – Bob Hanlon Oct 11 '15 at 18:27
  • $\begingroup$ To help you in answering your own question: what does g'[x] look like before simplifying? $\endgroup$ – J. M. will be back soon Oct 11 '15 at 18:27
5
$\begingroup$
g'[x]

$\frac{\frac{2 e^{-2 x} \left(1-e^{-2 x}\right)}{\left(e^{-2 > x}+1\right)^2}+\frac{2 e^{-2 x}}{e^{-2 x}+1}}{2 \sqrt{\frac{1-e^{-2 > x}}{e^{-2 x}+1}+1}}$

Simplify[g'[x]]

$\sqrt{2} e^{-2 x} \left(\frac{e^{2 x}}{e^{2 x}+1}\right)^{3/2}$

FullSimplify[g'[x]]

$\frac{1}{2} e^{-2 x} (\tanh (x)+1)^{3/2}$

ComplexExpand[g'[x], {x}, TargetFunctions -> Conjugate]

enter image description here

FullSimplify[%]

$-\frac{1}{2} (\tanh (x)-1) \sqrt{\tanh (x)+1}$

Plot[-(1/2) (-1 + Tanh[x]) Sqrt[1 + Tanh[x]], {x, -60, 60}, PlotRange -> All]

enter image description here

Of course, we can also use the built-in functions:

f[x_] := Tanh[x]
Plot[{f[x], f'[x]}, {x, -60, 60}]

enter image description here

f'[x]

$\text{sech}^2(x)$

Plot[Sech[x]^2, {x, -50, 50}, PlotRange -> All]

enter image description here

And we can check:

f'[x] == g'[x]

nope,

$\text{sech}^2(x)=\frac{\frac{2 e^{-2 x} \left(1-e^{-2 x}\right)}{\left(e^{-2 x}+1\right)^2}+\frac{2 e^{-2 x}}{e^{-2 x}+1}}{2 \sqrt{\frac{1-e^{-2 x}}{e^{-2 x}+1}+1}}$

because we'll find roots:

Reduce[f'[x] == a, x]

$c_1\in \mathbb{Z}\land a\neq 0\land \left(x=-\cosh ^{-1}\left(-\frac{1}{\sqrt{a}}\right)+2 i \pi c_1\lor x=\cosh ^{-1}\left(-\frac{1}{\sqrt{a}}\right)+2 i \pi c_1\lor x=-\cosh ^{-1}\left(\frac{1}{\sqrt{a}}\right)+2 i \pi c_1\lor x=\cosh ^{-1}\left(\frac{1}{\sqrt{a}}\right)+2 i \pi c_1\right)$

Reduce[g'[x] == a, x] // Quiet

$c_1\in \mathbb{Z}\land a\neq 0\land \left(\left(0=\frac{1}{2} \left(\sqrt{2} a \text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,1\right]-2 \sqrt{\frac{\text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,1\right]}{\text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,1\right]+1}}+\sqrt{2} a\right)\land x=\frac{1}{2} \left(\log \left(\text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,1\right]\right)+2 i \pi c_1\right)\right)\lor \left(0=\frac{1}{2} \left(\sqrt{2} a \text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,2\right]-2 \sqrt{\frac{\text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,2\right]}{\text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,2\right]+1}}+\sqrt{2} a\right)\land x=\frac{1}{2} \left(\log \left(\text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,2\right]\right)+2 i \pi c_1\right)\right)\lor \left(0=\frac{1}{2} \left(\sqrt{2} a \text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,3\right]-2 \sqrt{\frac{\text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,3\right]}{\text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,3\right]+1}}+\sqrt{2} a\right)\land x=\frac{1}{2} \left(\log \left(\text{Root}\left[\text{$\#$1}^3 a^2+3 \text{$\#$1}^2 a^2+\text{$\#$1} \left(3 a^2-2\right)+a^2\&,3\right]\right)+2 i \pi c_1\right)\right)\right)$

so, built in is quite smart.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.