TransWikia.com

Approximating step function with Tanh to compute integral

Mathematica Asked on July 4, 2021

It is easy to compute by hand the following integral.

$$ Q(k) = lim limits_{varepsilon to 0} int_2^{infty} frac{e^{-varepsilon r}}{k}sin(kr) mathrm{d}r = frac{cos(2k)}{k^2}$$

I wanted to use smoothened version of step function to compute this integral using Mathematica. My idea was to rewrite this integral in the following way.

$$ Q(k) = lim limits_{varepsilon to 0} int_0^{infty} Theta(r-2) frac{e^{-varepsilon r}}{k}sin(kr) mathrm{d}r$$

Here, I use step function $Theta(r-2)$ in the following smooth form.

$$ Theta(r-2) = frac{1}{2} + frac{1}{2} tanh(1000(r-2)) $$

I would like to compute this integral in this way and then compare it to the result $cos(2k)/k^2$. To do the computations, I use the following code.

Regularized[k_, epsilon_] := Assuming[epsilon >= 0 && k >= 0, Integrate[Sin[x*k]/k*Exp[-epsilon*x]*(0.5 + 0.5*Tanh[1000*(x - 2)]), {x, 0, Infinity}]]; 
ft[k_] := Limit[Regularized[k, P], P -> 0, Direction -> "FromAbove"];

To compare, I now do two plots using the following commands.

Plot[ft[k], {k, 0, 10}]
Plot[Cos[2 k]/(k*k), {k, 0, 10}]

My problem is that this computation takes more than 2 hours on my PC (which is not that old), and I have not yet seen the final result, so I wonder if I do something wrong. Maybe this approximation for step function is bad and instead of $tanh$ I should use something else? Or is there a problem with my Mathematica code? Any help is very much appreaciated

2 Answers

Side-stepping the main question some, Rubi is capable of integrating this much more directly. In my own experience, adding a smoothed function to simplify a symbolic problem requires ensuring that the function in question has convenient cancellation behavior. I can't say that the choice of $tanh$ is good or bad in this case, but it certainly does take longer than I'd expect on Mathematica.

On the other hand, using Rubi:

<< Rubi`
expr = Int[Exp[-ε r]/k Sin[k r], {r, 2, Infinity}]

ConditionalExpression[(E^(-2 ε) Cos[2 k])/(k^2 + ε^2) + (E^(-2 ε) ε Sin[2 k])/(k (k^2 + ε^2)), k > 0 && ε > 0]

If you plug this expression into an appropriate Limit:

Limit[expr, ε -> 0, Direction -> -1, Assumptions -> {k > 0}]

ConditionalExpression[Cos[2 k]/k^2, k > 0]

It also turns out that the original integrand also works if k is explicitly assumed to be positive:

 expr2 = Integrate[Exp[-ε r]/k Sin[k r], {r, 2, Infinity}, Assumptions -> {ε > 0, k > 0}]

Using HeavisideTheta also works as a step function under the same assumptions:

 Integrate[HeavisideTheta[r - 2] Exp[-ε r]/k Sin[k r], {r, 0, Infinity}, Assumptions -> {ε > 0, k > 0}]

Both of these expressions can be subjected to the same limit as above to give the same result. Again, I would expect that introducing inexact numbers or additional complicated functions would tend to make the result take longer, unless the form is peculiarly chosen to simplify the problem tremendously. I also generally trust that if a closed form solution to an integral exists and is at least somewhat well known, Rubi can provide a good head start on figuring out what it is.

Answered by eyorble on July 4, 2021

There is another approximation for the step function which is commonly used in AI, namely the logistic function which takes the generic form $1/(1+e^{-x})$. In particular, it has the property $$vphantom{``}%The editor complained at me unless I put this invisible code here... Theta(x)=lim_{zrightarrowinfty}frac{1}{1+e^{-zx}}. $$ For the particular case of $Theta(x-2)$ that you're interested in, we can compare the approximation you had using the $tanh$ to the logistic function with $z=50$ (chosen such that the approximation isn't so good that we can't tell the functions apart):

Plot[{(0.5+0.5*Tanh[1000*(x-2)]),1/(1+Exp[-50*(x-2)])},{x,1,3}]

enter image description here

Now, the advantage of the logistic map in AI is typically that it's very fast to evaluate, so there are numerical advantages. It seem like here you're looking for a symbolic result (as you're using Integrate rather than NIntegrate, which admittedly fails to converge for the $tanh$ approximation), so numerical efficiency isn't so helpful, but because this approximation seems to give a nicer result. In fact, the integral $$ f(k;epsilon,z)=int_0^inftyfrac{sin(kx)}{k}e^{-epsilon x}frac{1}{1+e^{-z(x-2)}} $$ turns out to be doable in closed form for all $epsilon,k,z>0$. Adding the $z>0$ condition to the assumptions in your original integration, Mathematica evaluates this integral to $$ f(k;epsilon,z)=frac{1}{2k(epsilon^2+k^2)}left[(k+iepsilon)_2F_1left(1,frac{epsilon+ik}{z};frac{epsilon+ik+z}{z};-e^{2z}right)+(k-iepsilon)_2F_1left(1,frac{epsilon-ik}{z};frac{epsilon-ik+z}{z};-e^{2z}right)right], $$ where $_2F_1$ is one of the hypergeometric functions. Plotting this function for $epsilon=0.05$ and $z=100$, we can see

Plot[{f[k,epsilon,z] /. {epsilon -> 0.05, z -> 100}, Cos[2*k]/k^2}, {k, 0, 10},PlotRange -> {{0, 10}, {-0.2, 0.2}}, PlotLegends -> Automatic]

enter image description here

where the yellow curve is the exact result and the blue curve is our approximation.

Answered by Richard Myers on July 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP