Are these two functions numerically stable?

318 Views Asked by At

Given are two (identical) functions $$f(x)=\sqrt{x^2+1}-1 \\ g(x)= \frac{x^2}{\sqrt{x^2+1}+1}$$

Are the calculation specifications for $f$ and $g$ numerically stable?

I have absolutely no idea how this could be solved and I might need to know that in the exam : /

I have read that something is stable if there aren't round-off errors that sum up and therefor give a bad solution. In other words, if something (in this case the functions) is insensitive towards a little disorder / change. That's how I understood it.

But how can this be checked for these two functions? Shall I manually choose some very small values, insert them into the function. Then do little changes with the inserted values, then compare both solutions? If big changes $\rightarrow$ numerically unstable, else numerically stable.

Isn't there are better way to do this? If not, would my cheap idea work?

1

There are 1 best solutions below

0
On BEST ANSWER

Both (identical) functions have the Taylor series at 0

$$f(x) = \frac{1}{2}x^2-\frac{1}{8}x^4+O(x^6)$$

Imagine what happens, if you want to compute $f(x)$ for $x^2 < \frac{1}{2}\epsilon$, where $\epsilon$ is the machine epsilon for your floating point arithmetic (i.e. $1+\epsilon \approx 1$) and $\approx$ denotes the result of floating point operations:

You get $$f(x) = \sqrt{x^2+1}-1 \approx \sqrt{1}-1 \approx 0$$ and for $$g(x) = \frac{x^2}{\sqrt{x^2+1}+1} \approx \frac{x^2}{\sqrt{1}+1} \approx \frac{x^2}{2}$$ i.e. we recover the leading term of the Taylor series.

To summarize: $f(x)$ suffers from rounding and cancellation (aka loss of significance) and is useless for small arguments, $g(x)$ is rounded as well but there is no cancellation and it will give a faithful result near zero.

Update: Here are some actual numbers. First for $f(x)$ with $t=\sqrt{x^2+1}, y=t-1$

x= 0.001000000000000  t= 1.000000499999880  y=    4.99999875058777E-007
x= 0.000100000000000  t= 1.000000005000000  y=    4.99999996961265E-009
x= 0.000010000000000  t= 1.000000000050000  y=    5.00000041370186E-011
x= 0.000001000000000  t= 1.000000000000500  y=    5.00044450291171E-013
x= 0.000000100000000  t= 1.000000000000000  y=    4.88498130835069E-015
x= 0.000000010000000  t= 1.000000000000000  y=    0.00000000000000E+000
x= 0.000000001000000  t= 1.000000000000000  y=    0.00000000000000E+000
x= 0.000000000100000  t= 1.000000000000000  y=    0.00000000000000E+000

you can see that if $x$ becomes small, the $t$ values approach 1 and for $x \le 0.00000001$ the floating point difference $t-1$ is zero (all significant digits are lost).

For $g(x)$ with $d=\sqrt{x^2+1}+1, y=x^2/d$ it looks like

x= 0.000100000000000  d= 2.000000005000000  y=    4.99999998750000E-009
x= 0.000010000000000  d= 2.000000000050000  y=    4.99999999987500E-011
x= 0.000001000000000  d= 2.000000000000500  y=    4.99999999999875E-013
x= 0.000000100000000  d= 2.000000000000000  y=    4.99999999999999E-015
x= 0.000000010000000  d= 2.000000000000000  y=    5.00000000000000E-017
x= 0.000000001000000  d= 2.000000000000000  y=    5.00000000000000E-019
x= 0.000000000100000  d= 2.000000000000000  y=    5.00000000000000E-021

Here the $d$ is rounded to 2 for $x \le 0.00000001$, but in this case no significant digits are lost ($d\approx 2$ with very good precision).