# Coordinate systems

## Introduction

This notebooks provides a tutorial about (curvilinear) coordinate systems. We use Mayavi to do the visualization of some of the surface for constant coordinate values.



### Use of Mayavi in this notebook
Mayavi can display either images or X3D elements on the notebook. The images are static and one cannot interact with them. The X3D output produces a fully interactive 3D scene. For information on how to interact with the scene, see here: http://www.x3dom.org/documentation/interaction/

Mayavi ships with some javascript files that can be installed as:

    $ jupyter nbextension install --py mayavi --user

This will install the x3dom Javascript and CSS files locally. Note that you do not need to “enable” the extension or anything after you run the above. For more instructions and options see the Installation of Jupyter Extensions. Doing this allows one to view X3D files without a network connection.

To view Mayavi visualizations on the notebook one should first do:

    from mayavi import mlab
    mlab.init_notebook()

    Subequently, one may simply do:

    s = mlab.test_plot3d()
    s

When the init_notebook method is called it configures the Mayavi objects so they can be rendered on the Jupyter notebook.

**More information:** http://docs.enthought.com/mayavi/mayavi/tips.html


```
from mayavi import mlab
import numpy as np
```


```
mlab.init_notebook()
```

    Notebook initialized with ipy backend.



```
red = (0.9, 0.1, 0.1)
blue = (0.2, 0.5, 0.7)
green = (0.3, 0.7, 0.3)
```

## Cylindrical coordinates

The [ISO standard 80000-2](https://en.wikipedia.org/wiki/ISO_80000-2) recommends the use of $(ρ, φ, z)$, where $ρ$ is the radial coordinate, $\varphi$ the azimuth, and $z$ the height.

For the conversion between cylindrical and Cartesian coordinates, it is convenient to assume that the reference plane of the former is the Cartesian $xy$-plane (with equation $z=0$, and the cylindrical axis is the Cartesian $z$-axis. Then the $z$-coordinate is the same in both systems, and the correspondence between cylindrical $(\rho, \varphi)$ and Cartesian $(x, y)$ are the same as for polar coordinates, namely

\begin{align}
x &= \rho \cos \varphi \\
y &= \rho \sin \varphi
\end{align}

in one direction, and

\begin{align}
    \rho &= \sqrt{x^2+y^2} \\
    \varphi &= \begin{cases}
        0 & \mbox{if } x = 0 \mbox{ and } y = 0\\
        \arcsin\left(\frac{y}{\rho}\right) & \mbox{if } x \geq 0 \\	
        \arctan\left(\frac{y}{x}\right) & \mbox{if } x > 0 \\	
        -\arcsin\left(\frac{y}{\rho}\right) + \pi & \mbox{if } x < 0
    \end{cases}
\end{align}

in the other. It is also common to use [$\varphi = atan2(y, x)$](https://en.wikipedia.org/wiki/Atan2).



```
cyl = mlab.figure(bgcolor=(1.0, 1.0, 1.0))
mlab.clf()
```


```
# Cylinder
phi, z = np.mgrid[0:2*np.pi:31j, -2:2*np.pi:31j]
x = 2*np.cos(phi)
y = 2*np.sin(phi)
z = z
cylinder = mlab.mesh(x, y, z, color=red)
```


```
# Vertical plane
rho, z = np.mgrid[0:3:31j, -2:2*np.pi:31j]
x = rho*np.cos(np.pi/4)
y = rho*np.sin(np.pi/4)
z = z
plane = mlab.mesh(x, y, z, color=blue, opacity=0.6)
```


```
# Horizontal plane
rho, phi = np.mgrid[0:3:31j, 0:2*np.pi:31j]
x = rho*np.cos(phi)
y = rho*np.sin(phi)
z = np.ones_like(x)
plane = mlab.mesh(x, y, z, color=green, opacity=0.6)
```


```
cyl
```


    Image(value=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01\x90\x00\x00\x01^\x08\x02\x00\x00\x00$?\xde_\x00\…


## Spherical coordinates


The use of $(r, θ, φ)$ to denote radial distance, inclination (or elevation), and azimuth, respectively, is common practice in physics, and is specified by [ISO standard 80000-2](https://en.wikipedia.org/wiki/ISO_80000-2).


The spherical coordinates of a point can be obtained from its [Cartesian coordinate system](https://en.wikipedia.org/wiki/ISO_80000-2) $(x, y, z)$

\begin{align}
r&=\sqrt{x^2 + y^2 + z^2} \\
\theta &= \arccos\frac{z}{\sqrt{x^2 + y^2 + z^2}} = \arccos\frac{z}{r} \\
\varphi &= \arctan \frac{y}{x}
\end{align}

The inverse tangent denoted in $\varphi = \arctan\left(\frac{y}{x}\right)$ must be suitably defined , taking into account the correct quadrant of $(x, y)$ (using the function ``atan2``).

Conversely, the Cartesian coordinates may be retrieved from the spherical coordinates (_radius_ $r$, _inclination_ $\theta$, _azimuth_ $\varphi$), where $r \in [0, \infty)$, $\theta \in [0, \pi]$ and $\varphi \in [0, 2\pi)$, by:

\begin{align}
x&=r \, \sin\theta \, \cos\varphi \\
y&=r \, \sin\theta \, \sin\varphi \\
z&=r \, \cos\theta
\end{align}


```
sph = mlab.figure(bgcolor=(1.0, 1.0, 1.0))
mlab.clf()
theta, phi = np.mgrid[0:np.pi:21j, 0:np.pi:21j]
```


```
# Sphere
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)
sphere = mlab.mesh(x, y, z, color=red)
spher2e = mlab.mesh(-x, -y, z,representation='wireframe', color=red)
```


```
# Cone
x = theta/3 * np.cos(phi)
y = theta/3 * np.sin(phi)
z = theta/3
cone = mlab.mesh(x, y, z, color=blue, opacity=0.6)
cone2 = mlab.mesh(-x, -y, z, representation='wireframe', color=blue)
```


```
# Plane
x = theta/np.pi
y = theta/np.pi
z = phi - np.pi/2
plane = mlab.mesh(x, y, z, color=green)
```


```
sph
```


    Image(value=b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01\x90\x00\x00\x01^\x08\x02\x00\x00\x00$?\xde_\x00\…


## Ellipsoidal coordinates


```
ellip = mlab.figure(bgcolor=(1.0, 1.0, 1.0))
mlab.clf()
v, theta = np.mgrid[0:np.pi/2:21j, 0:np.pi:21j]
a = 3
b = 2
c = 1
```


```
# Ellipsoid
lam = 3
x =  np.sqrt(a**2  + lam) * np.cos(v) * np.cos(theta)
y = np.sqrt(b**2  + lam)* np.cos(v) * np.sin(theta)
z = np.sqrt(c**2 + lam) * np.sin(v)
ellipsoid = mlab.mesh(x, y, z, color=red)
ellipsoid2 = mlab.mesh(x, y, -z, color=red)
ellipsoid3 = mlab.mesh(x, -y, z, representation='wireframe', color=red)
ellipsoid4 = mlab.mesh(x, -y, -z, representation='wireframe', color=red)
```


```
# Hyperboloid of one sheet
mu = 2
x = np.sqrt(a**2 + mu) * np.cosh(v) * np.cos(theta)
y = np.sqrt(b**2 + mu) * np.cosh(v) * np.sin(theta)
z = np.sqrt(c**2 + mu) * np.sinh(v)
z = np.sqrt(c**2 + mu) * np.sinh(v)
hyper = mlab.mesh(x, y, z, color=blue)
hyper2 = mlab.mesh(x, y, -z, color=blue)
hyper3 = mlab.mesh(x, -y, z, representation='wireframe', color=blue)
hyper4 = mlab.mesh(x, -y, -z, representation='wireframe', color=blue)
```


```
# Hyperboloid of two sheets
nu = 1
x = np.sqrt(a**2 + nu) * np.cosh(v)
y = np.sqrt(c**2 + nu) * np.sinh(v) * np.sin(theta)
z = np.sqrt(b**2 + nu) * np.sinh(v) * np.cos(theta)
hyper_up = mlab.mesh(x, y, z, color=green)
hyper_down = mlab.mesh(-x, y, z, color=green)
hyper_up2 = mlab.mesh(x, -y, z, representation='wireframe', color=green)
hyper_down2 = mlab.mesh(-x, -y, z, representation='wireframe', color=green)
```


```
ellip
```

## References


1. Wikipedia contributors. ["Cylindrical coordinate system."](https://en.wikipedia.org/wiki/Cylindrical_coordinate_system) Wikipedia, The Free Encyclopedia. Wikipedia, The Free Encyclopedia, 12 Dec. 2016. Web. 9 Feb. 2017.

2. Wikipedia contributors. ["Spherical coordinate system."](https://en.wikipedia.org/wiki/Spherical_coordinate_system) Wikipedia, The Free Encyclopedia. Wikipedia, The Free Encyclopedia, 29 Jan. 2017. Web. 9 Feb. 2017.

3. Enthought Inc. [Mayavi: Tips and Trick](http://docs.enthought.com/mayavi/mayavi/tips.html). Web. 9 Feb. 2017.


```

```


```
from IPython.core.display import HTML
def css_styling():
    styles = open('./styles/custom_barba.css', 'r').read()
    return HTML(styles)
css_styling()
```


```

```

c26537a0ceead68868bff53976c3cfb04e44356c
ipynb
Jupyter Notebook
notebooks/vector_calculus-mayavi.ipynb
nicoguaro/AdvancedMath
2749068de442f67b89d3f57827367193ce61a09c
MIT
2017-06-29T17:45:20.000Z
2022-02-06T20:14:29.000Z
notebooks/vector_calculus-mayavi.ipynb
nicoguaro/AdvancedMath
2749068de442f67b89d3f57827367193ce61a09c
MIT
notebooks/vector_calculus-mayavi.ipynb
nicoguaro/AdvancedMath
2749068de442f67b89d3f57827367193ce61a09c
MIT
2019-04-22T08:08:56.000Z
2022-01-27T08:15:53.000Z
Qwen/Qwen-72B
1. YES
2. YES