Help with Matlab rewriting subtraction as division

119 Views Asked by At

I have the function 1./(1-(x).^3)-1./(1+(x).^3) I need to rewrite this function so that it works well when I plot it on Matlab on the interval x=-0.00001:0.0000001:0.00001. So far, the best thing I have is $$\frac{(2/x^3)}{(1/x^6)-1},$$ but this has a discontinuity near $x=0$. I believe the problem lies in subtracting, but I can't figure out how to get rid of that $-1$. I've replaced all other subtraction with division, and this equation works a lot better than the original. enter image description here

edited second function for clarity.

1

There are 1 best solutions below

0
On

Multiply your function $f(x) = \frac{1}{1-x^3} - \frac{1}{1+x^3}$ with $1-x^3$ and get $$f(x)(1-x^3)=\left(\frac{1}{1-x^3} - \frac{1}{1+x^3}\right)(1-x^3) =1 - \frac{1-x^3}{1+x^3} = \frac{1+x^3-1+x^3}{1+x^3}= \frac{2x^3}{1+x^3} $$ Now divide the numerator and get a computational stable form for your range $$f(x) = \frac{(2x^3)/(1-x^3)}{1+x^3} $$ Note that this is an exact reformulation. A much easier formula would use the Taylor expansion $$f(x) = 2x^3\left(1+x^6+x^{12}+O(x^{18})\right)$$ If $|x| < 0.002$ (and this includes your given range) you can safely use the approximation $f(x)=2x^3$ accurate to double precision.