10

I can make sphere with pgfplots through the following code.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}

    \begin{axis}[%
        axis equal,
        width=10cm,
        height=10cm,
        axis lines = none,
    %    xlabel = {$x$},
    %    ylabel = {$y$},
    %    zlabel = {$z$},
        ticks=none,
        enlargelimits=0.3,
        view/h=45,
        scale uniformly strategy=units only,
    ]

 \addplot3[%
        opacity = 0.5,
        surf,
        z buffer = sort,
        samples = 21,
        variable = \u,
        variable y = \v,
        domain = 0:180,
        y domain = 0:360,
    ]
    ({cos(u)*sin(v)}, {sin(u)*sin(v)}, {cos(v)});

    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

I'm wondering how to get such rotated tilted sphere with pgfplots. Thanks

enter image description here

MYaseen208
  • 8,587
  • You can change the formula of the sphere such that principal axes are not on the xyz axes. – percusse Jul 22 '13 at 13:02
  • Thanks @percusse for your comment. Would you mind to more elaborate your comment?. Thanks – MYaseen208 Jul 22 '13 at 13:08
  • I mean it is possible to derive a formula for the rotated ellipsoid. And using that formula will directly draw the tilted sphere instead of canvas rotations etc. on the nonrotated one. – percusse Jul 22 '13 at 14:01
  • Do you want the sphere to be tilted with respect to the axes, i.e. should the z axis still point directly up, or would it also be okay if the axes were also tilted? – Jake Jul 24 '13 at 14:09
  • I want to have tilted and rotated sphere as shown in last figure. Thanks – MYaseen208 Jul 24 '13 at 14:15
  • @MYaseen208: So you will never switch on the axis lines (you will never remove axis lines = none), correct? – Jake Jul 24 '13 at 14:21
  • Yes, you right @jake. – MYaseen208 Jul 24 '13 at 14:23
  • Multiply the x component by 3 and y,z components by 0.5. Then use view={55}{-100}. Is that close? – percusse Jul 24 '13 at 15:23

1 Answers1

10

1. Remarks

For explanations and implementations, see the "attached" document in section 3.

All the matrix multiplication stuff was done using Mathematica.

To learn more about rotations see https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations or take a course on classical mechanics in your local university, especially rigid body dynamics.

2. Implementation

Compile with pdflatex, lualatex or xelatex. Tested on TeXLive 2013.

\documentclass[DIV=16]{scrartcl}
\pagestyle{empty}
\usepackage{amsmath}
\usepackage{url}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}

To rotate a body around a given axis, its parametrization vector $\boldsymbol{r}$
needs to be multiplied with the corresponding rotation matrix.

The parametrization vector of an ellipsoid is
%
\begin{equation*}
    \boldsymbol{r}
    =
    \begin{pmatrix}
        x \\
        y \\
        z \\
    \end{pmatrix}
    =
    \begin{pmatrix}
        a \sin\vartheta \cos\varphi \\
        b \sin\vartheta \sin\varphi \\
        c \cos\vartheta \\
    \end{pmatrix}
\end{equation*}
%
with the half-axes $a,b,c$.

\textbf{Example:} Rotation around the $z$-axis at the angle $\alpha$.
%
\begin{align*}
    \boldsymbol{r}^*
    =
    \mathcal{R}_z(\alpha) \cdot \boldsymbol{r}
    &=
    \begin{pmatrix}
        \cos\alpha & -\sin\alpha & 0 \\
        \sin\alpha & \cos\alpha & 0 \\
        0 & 0 & 1 \\
    \end{pmatrix}
    \cdot
    \begin{pmatrix}
        a \sin\vartheta \cos\varphi \\
        b \sin\vartheta \sin\varphi \\
        c \cos\vartheta \\
    \end{pmatrix}
    \\
    &=
    \begin{pmatrix}
        a \cos\alpha \sin\vartheta \cos\varphi - b \sin\alpha \sin\vartheta \sin\varphi \\
        b \cos\alpha \sin\vartheta \sin\varphi + a \sin\alpha \sin\vartheta \cos\varphi \\
        c \cos\vartheta \\
    \end{pmatrix}
\end{align*}

\begin{center}
    \def\a{2}
    \def\b{1}
    \def\c{0.5}
    \def\z{30}
    \begin{tikzpicture}
        \begin{axis}[%
            axis equal,
            width=20cm,
            height=20cm,
            axis lines = center,
            ticks=none,
            view/h=45,
            enlargelimits=0.3,
            scale uniformly strategy=units only,
        ]

            \addplot3[%
                opacity = 0.5,
                surf,
                z buffer = sort,
                samples = 21,
                variable = \u,
                variable y = \v,
                domain = 0:180,
                y domain = 0:360,
            ]
            (
                {\a*cos(\z)*sin(u)*cos(v) + \b*sin(\z)*sin(u)*sin(v)},
                {\b*cos(\z)*sin(u)*sin(v) - \a*sin(\z)*sin(u)*cos(v)},
                {\c*cos(u)}
            );
    ,
    ,

        \end{axis}
    \end{tikzpicture}
\end{center}

\textbf{Example:} Rotation around the $z$-axis at the angle $\alpha$ and the $x$-axis at the angle $\beta$. This is analogous to the example above except, that now we need to multiply the vector with two rotation matrices. Keep in mind, that rotations do not commute! That means if you rotate around the $z$-axis first and then around the $x$-axis you are really rotating around the $x^*$-axis, which was created when rotating around the $z$-axis.
%
\begin{align*}
    \boldsymbol{r}^*
    &=
    \mathcal{R}_x(\beta) \cdot \mathcal{R}_z(\alpha) \cdot \boldsymbol{r}
    \\
    &=
    \begin{pmatrix}
        1 & 0 & 0 \\
        0 & \cos\beta & -\sin\beta\\
        0 & \sin\beta & \cos\beta \\
    \end{pmatrix}
    \cdot
    \begin{pmatrix}
        \cos\alpha & -\sin\alpha & 0 \\
        \sin\alpha & \cos\alpha & 0 \\
        0 & 0 & 1 \\
    \end{pmatrix}
    \cdot
    \begin{pmatrix}
        a \sin\vartheta \cos\varphi \\
        b \sin\vartheta \sin\varphi \\
        c \cos\vartheta \\
    \end{pmatrix}
    \\
    &=
    \begin{pmatrix}
        a \cos\alpha \cos\varphi \sin\vartheta
        - b \sin\alpha \sin\vartheta \sin\varphi
    \\
        - c \cos\vartheta \sin\beta
        + a \cos\beta \cos\varphi \sin\alpha \sin\vartheta
        + b \cos\alpha \cos\beta \sin\vartheta \sin\varphi
    \\
        c \cos\beta \cos\vartheta
        + a \cos\varphi \sin\alpha \sin\beta \sin\vartheta
        + b \cos\alpha \sin\beta \sin\vartheta \sin\varphi \\
    \end{pmatrix}
\end{align*}

\begin{center}
    \def\a{2}
    \def\b{1}
    \def\c{0.5}
    \def\z{30}
    \def\x{15}
    \begin{tikzpicture}
        \begin{axis}[%
            axis equal,
            width=20cm,
            height=20cm,
            axis lines = center,
            ticks=none,
            view/h=45,
            enlargelimits=0.3,
            scale uniformly strategy=units only,
        ]

            \addplot3[%
                opacity = 0.5,
                surf,
                z buffer = sort,
                samples = 21,
                variable = \u,
                variable y = \v,
                domain = 0:180,
                y domain = 0:360,
            ]
            (
                {\a*cos(v)*cos(\z)*sin(u) - \b*sin(u)*sin(v)*sin(\z)},
                {\b*cos(\x)*cos(\z)*sin(u)*sin(v) - \c*cos(u)*sin(\x) + \a*cos(v)*cos(\x)*sin(u)*sin(\z)},
                {\c*cos(u)*cos(\x) + \b*cos(\z)*sin(u)*sin(v)*sin(\x) + \a*cos(v)*sin(u)*sin(\x)*sin(\z)}
            );

        \end{axis}
    \end{tikzpicture}
\end{center}

For more information about rotation matrices see \url{https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations}.

\end{document}

3. Output

On writeLaTeX, with a few modifications proposed by percusse, thank you: https://www.writelatex.com/283014krmwmg

enter image description here

Henri Menke
  • 109,596
  • 1
    This is probably one of the right places to use compat=newest then it will use whatever the last version is installed on the server. You also need to take out scale uniformly strategy=units only, line to make it work on writelatex – percusse Jul 25 '13 at 08:50
  • @percusse Thank you very much. I will try it later and update my post, if it works. – Henri Menke Jul 25 '13 at 09:03