How do I ensure that my function below is well conditioned as $s$ approaches $\infty$? The problem I get is that for large $s$ the function returns an indeterminate form $\frac{0}{0}$. I would otherwise have expected the function to increase monotonically from 0 to 1 as $s$ approaches $\infty$.

\begin{align} f(s) = \frac{x_s^2+\sqrt{1-x_s^2}-1}{\sqrt{x_s^2(x_s(\sqrt{1-x_s^2}-1))^2+(x_s^2+\sqrt{1-x_s^2}-1)^2}} \end{align}

\begin{align} x_s = e^{-s} \end{align}

import numpy as np

def func(s):
    xs = np.exp(-1*s)
    num = xs**2+np.sqrt(1-xs**2)-1
    den = np.sqrt(xs**2*(xs*(np.sqrt(1-xs**2)-1))**2+(xs**2+np.sqrt(1-xs**2)-1)**2) 
    return num/den

s = np.linspace(0,20, 1000)
plot(s, func(s))

enter image description here

  • $\begingroup$ In code you have (xs*(np.sqrt(1-xs**2)-1))**2, but in the definition you instead have $(x\sqrt{1-x^2}-1)^2$. Which of these is supposed to be correct? $\endgroup$ – Kirill Sep 28 '14 at 23:16
  • $\begingroup$ Edited. The code was the correct version. Nice spot! $\endgroup$ – Dipole Sep 29 '14 at 8:08

The function is really smooth as $s \rightarrow \infty$. This allows you to do the following strategy:

  • For $s\le s_0$, do the usual evaluation, i.e., return $f(s)$.

  • For $s>s_0$, define the function $g(h)=f(1/h)$, do a Taylor expansion around $h=0$ (i.e., around $s=\infty$) and return $\bar g(h)|_{h=1/s}$ where $\bar g$ contains the first few Taylor terms of the expansion of $g(h)$. In the evaluation of these Taylor terms, you need to compute the $k$th derivatives $g^{(k)}(0)$ which will involve factors $\frac 00$ for which you will have to apply l'Hopital's rule.

  • $\begingroup$ Thanks, I will try this. How would I pick $s_0$ ? Also I don't look forward to expanding the taylor series for this function, but I guess doing it by hand is the only way? $\endgroup$ – Dipole Sep 28 '14 at 21:43
  • $\begingroup$ @Jack Use a CAS. I get this for the Taylor series at $x=0$: $$\textstyle \frac{1}{2} x+\frac{1}{2} x^2+\frac{5}{16} x^3-\frac{1}{16}x^4-\frac{129}{256}x^5-\frac{197}{256}x^6-\frac{1459}{2048}x^7 -\frac{437}{2048}x^8 +\frac{35451}{65536} x^9+\frac{81235}{65536}x^{10} + O(x^{11})$$ $\endgroup$ – Kirill Sep 28 '14 at 22:58
  • $\begingroup$ @Kirill Would Sympy be able to do that? $\endgroup$ – Dipole Sep 29 '14 at 8:10
  • 1
    $\begingroup$ @Jack: Yes, Sympy can do that. $\endgroup$ – Geoff Oxberry Sep 29 '14 at 9:07
  • $\begingroup$ You'd choose $s_0$ so that for $s<s_0$ no cancellation happens, or not to a degree that is detrimental. In the graph you show, you could choose $s_0=10$. $\endgroup$ – Wolfgang Bangerth Sep 29 '14 at 12:18

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.