R: Fun with surf3D function

[This article was first published on Analysis with Programming, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:
$$
 \begin{eqnarray}
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
\end{eqnarray}
$$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 on 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:
$$
\begin{eqnarray}
x(u,v)&=&\left[a+\cos\left(\displaystyle\frac{1}{2}\right)\sin(v)-\sin\left(\displaystyle\frac{1}{2}\right)\sin(2v)\right]\cos(u);\nonumber\\
y(u,v)&=&\left[a+\cos\left(\displaystyle\frac{1}{2}\right)\sin(v)-\sin\left(\displaystyle\frac{1}{2}\right)\sin(2v)\right]\sin(u);\nonumber\\
z(u,v) &=&\sin\left(\displaystyle\frac{1}{2}u\right)\sin(v) + \cos\left(\displaystyle\frac{1}{2}u\right)\sin(2v).\nonumber
\end{eqnarray}
$$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.
Mathematics is cool. I will leave you with this 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.


References: 

  1. Klein Bottle – Wikipedia. Retrieved February 18, 2014.
  2. Weisstein, Eric W. “Klein Bottle.” From MathWorld–A Wolfram Web Resource. http://mathworld.wolfram.com/KleinBottle.html

To leave a comment for the author, please follow the link and comment on their blog: Analysis with Programming.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)