Find a stable way to compute the function

98 Views Asked by At

Say I have this function:

$$f(x)=\frac{\text{e}^x-x}{\sin(x)}$$

And I want to reduce the error when $x\approx0$ and find a stable way to compute the function. More accurate wording, how can the function be written in such a way that the error is reduced?

2

There are 2 best solutions below

0
On BEST ANSWER

Near $0$ you need to expand all terms (into the Taylor series) and cancel $x$: $$ \frac{\text{e}^x-1}{\sin(x)}=\frac{x+\frac{x^2}{2}+\frac{x^3}{6}+\dots}{x-\frac{x^3}{6}+\dots}=\frac{1+\frac{x}{2}+\frac{x^2}{6}+\dots}{1-\frac{x^2}{6}+\dots} $$ This formula can be numerically evaluated without problems. If you need a code evaluating this function for any value, then you need this expansion for $x\in[-\varepsilon,\varepsilon]$, where $\varepsilon$ can be explicitly determined (for the required accuracy), and the original formula for $x$ outside of this interval.

0
On

Similar to @Vítězslav Štembera's answer

Better than Taylor series are Padé approximants : for example $$\frac{e^x-1}{\sin(x)}=\frac {1+\frac{3 }{14}x+\frac{11 }{84}x^2 } {1-\frac{2 }{7}x-\frac{5 }{84}x^2 }$$ gives an asymptotic error of $\frac{53 }{10080}x^5$

$$\frac{e^x-x}{\sin(x)}=\frac 1 x\, \frac{1-\frac{1}{4}x+\frac{41 }{80}x^2 } {1-\frac{1}{4}x-\frac{37 }{240}x^2 }$$