0

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.

  • This looks like it will require more than double precision. I cannot even reproduce your plot; all I get is zeros. I know with C you have access to the long double type (128-bit floating point), but I have never looked into whether such a type is available in Matlab. – AnonSubmitter85 Feb 18 '14 at 07:37
  • @AnonSubmitter85: Run this:
    x=-0.00001:0.0000001:0.00001, y=(2./x.^3)./(1./x.^6-1), plot(x,y)
    – Alt Feb 18 '14 at 07:42
  • 1
    $\dfrac{2x^3}{1+x^3+x^6+x^9}$ –  Feb 18 '14 at 07:47
  • Reduce to same denominator, expand and simplify and then arrive to the simple formula given by Rahul to be used everywhere. This is not because of Matlab; this is the way you must write your function. – Claude Leibovici Feb 18 '14 at 07:53
  • @Alt Thanks. I still think that this is ill-defined for 64-bit floats though. For the range of numbers the OP is using, whenever $q>3$, we will have $1 \pm x^q = 1$. – AnonSubmitter85 Feb 18 '14 at 07:54
  • 1
    @beandaddyo. You suddenly change your post and problem ! – Claude Leibovici Feb 18 '14 at 07:56
  • @Rahul Note that what you have written is equal to $(2x^3) / (1+x^3)$ when using double precision. The OP's problem cannot properly be handled with 64-bit floating point numbers. – AnonSubmitter85 Feb 18 '14 at 07:56

2 Answers2

2

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.

gammatester
  • 18,827
1

If you only care about plotting the function, a rather simple and straight-forward method might be to use syms.

x=sym(-0.00001:0.0000001:0.00001);
y=1./(1-(x).^3)-1./(1+(x).^3);
plot(vpa(x),vpa(y))

The result seems okay: enter image description here

Alan Wang
  • 181