Wednesday, 19 February 2014

R: Fun with surf3D function

There is one package that I've been longing. A package that will give me the power to manipulate and do any 3D stuffs in R. I tried persp and wireframe, but I find them difficult to use especially on complicated mathematical functions, like doing parametric plots. And I am just frustrated about that, since I envy the 3D graphics of Wolfram Mathematica a lot, which I exploited for about a year, before I embrace R. However, that has come to an end after Joseph Rickert introduced the plot3D (authored by Karline Soetaert) package in his post. And for the moment, we will be playing with the surf3D function. Here is the first one, the Mollusc Shell surface plot:
With parametric equations:
x(u,v)&=&\left(1.16^v\right)(1 + \cos(u))\cos(v);\nonumber\\
y(u,v)&=&\left(-1.16^v\right)(1 + \cos(u))\sin(v);\nonumber\\
z(u,v)&=&\left(-2\times 1.16^v\right)(1 + \sin(u));\nonumber
$$where $u\in[0, 2\pi],\,v\in[-15, 6]$. By the way, I am not sure if that's the general equations of the Mollusc shell, I can't find one on the web. In case you know it, please inform me by commenting below. The above equations was based from the Mathematica Documentation, with code

Now with extra animation, here is the R equivalent:

Next is the "figure 8" immersion (Klein bagel) of the Klein bottle:
z(u,v) &=&\sin\left(\displaystyle\frac{1}{2}u\right)\sin(v) + \cos\left(\displaystyle\frac{1}{2}u\right)\sin(2v).\nonumber
$$For $u\in [0,2\pi),\,v\in[0,2\pi),$ and $a>2$.
Which in R,

Now let's investigate the script of this static, transparent Mollusc Shell:

  1. Call the package, that's obviously the first line;
  2. Line 3 to 7 basically assigns the ranges of $u$ and $v$ variables;
  3. Line 10 to 12 are the parametric equations of the shape;
  4. Line 15 adjusts the margin of the plot on all sides in inches; and finally
  5. Line 16 to 20 generates the plot.
I will end this post with a quote from my favourite TV show, Numb3rs:

Math is nature's language, it's method of communicating directly with us. So everything is Numbers. ~ Charlie Eppes from Numb3rs - Sabotage, Season I.


  1. Klein Bottle - Wikipedia. Retrieved February 18, 2014.
  2. Weisstein, Eric W. "Klein Bottle." From MathWorld--A Wolfram Web Resource.

No comments:

Post a Comment