I'm solving questions from an exam I failed, and I would love some help with the following question:
Question
We want to calculate the following function in Matlab: $$ f(x) = \frac{e^{x^2} - (1 + x^2)}{x^4} $$ We know that as $x$ approaches $0$, $f(x)$ approaches $0.5$, but in Matlab we get: $f(0.0001) = 0$.
- Why don't we get a result close to 0.5 for small $x$?
- Propose an algorithm to calculate $f(x)$ for $x = 0.001$ that will be exact for at least 10 decimal points.
My thoughts
I played with the function in Matlab and I saw that when I calculate $1 + x^2$ and $e^{x^2}$ I get 1, which explains the result: x^2 is smaller than the machine's epsilon, so $1+x^2$ cannot be represented in floating point using the number of bits we have, and since both expressions are rounded to 1 we get 0.
I am not sure how to approach 2. I tried to change the form of the expression analytically to something that won't cause similar problems but didn't find anything promising.
Thanks! :)