# Function rotated about the x-axis

I try to reproduce some figure from that file: http://tug.org/TUGboat/tb33-1/tb103wolcott.pdf

I exactly need Figure 7: f(x) = sin x rotated about the x-axis. The code for Figure 7 is in appendix B. Used packages:

\usepackage{tikz}
\usepackage{ifthen}\newboolean{color}


Errors from Texmaker:

! Missing number, treated as zero.<to be read again>{ }
! Illegal unit of measure (pt inserted).<to be read again>{ }


Errors appears at the middle of these section:

\foreach \x in
\pgfmathsetmacro\xsh{(cos(\phi))*({\x})}
\tikzset{xyplane/.estyle={cm={
cos(\phi - 90), 0,0,1, (\xsh, 0)}}}
\draw[xyplane,\backColor,ultra thick,opacity=.6]
(0, \rad) arc (90 : 270 : \rad);
} %%% errors appears here %%%
\foreach \theta in
{0, \rotationGridStepsize, ..., 180} {
\tikzset{xyplane/.estyle={cm={
cos(\phi), 0, sin(\theta)*sin(\phi),
cos(\theta), (0, 0)}}}
\draw[xyplane,smooth] plot (\x, \fcn) ;
}


I know something about LaTeX but nothing about PGF/TikZ. How to erase these errors?

-

It should compile if you remove the curly braces around \x in \pgfmathsetmacro\xsh{...}:

\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage{ifthen}\newboolean{color}

\begin{document}

\begin{figure}[h]
\begin{center}
%%%%%%%% Set function values %%%%%%%
% Set the x = a and x = b values of the
% domain here where a <= x <= b.
\def\bDomain{3.14159}

% Set the function.
% The variable must be \x, e.g. \x^2.
\def\fcn{sin(\x r)}
%\def\fcn{sqrt(\x)}

% Set min and max values of the function
% (c <= f(x) <= d). Used for the y-axis.
\def\cRange{0}
\def\dRange{1}

% Set the color of the back half.
% This can look good as a different color
% if it looks like the inside.
\def\backColor{red!70!black}

% Set the color of the front half. lightgray looks
% good for both back and front.
\def\frontColor{red!70!black}

% Set the number of shading circles to draw.
% More gives a more even color. Enter 1 for
% no shading; a large number makes it slow.
% Use the following two lines while editing and then
% change the speed to 100 for the final version.
%\def\speed{1}

% Set the number of x radius grid circles.
\def\xGridSteps{8}

% Set the number of radial grid lines.
\def\rotationGridSteps{18}

% Set the viewing elevation angle,
% which is the angle up from horizontal.
\def\phi{15}
%%%%%%%%%%%%%%%%%%%%%%%%%

\pgfmathsetmacro\scaleAttempt{3.4/\dRange}

\begin{tikzpicture}[scale= \scaleAttempt, domain= \aDomain: \bDomain]
\pgfmathsetmacro\xGridStepsize{\intervalLength/\xGridSteps}
\pgfmathsetmacro\rotationGridStepsize{360/\rotationGridSteps}

% Draw the shading of the back half.
% Left half of a circle, rotated right
% (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.

\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
\pgfmathsetmacro\xsh{(cos(\phi))*(\x)}
\tikzset{xyplane/.estyle={cm={cos(\phi - 90), 0,0,1, (\xsh, 0)}}}
\draw[xyplane,\backColor,ultra thick,opacity=.6] (0, \rad) arc (90 : 270 : \rad);
}

% Back longitude lines.
% Rotates graph around y-axis,
% then projects to xy-plane.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
\tikzset{xyplane/.estyle={cm={
cos(\phi), 0, sin(\theta)*sin(\phi),
cos(\theta), (0, 0)}}}
\draw[xyplane,smooth] plot (\x, \fcn) ;
}

% Back latitude lines.
% Left half of a circle, rotated right
% (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.
\foreach \x in {\aDomain,\nextStep, ...,\bDomain} {
\pgfmathsetmacro\xsh{(cos(\phi))*(\x)}
\tikzset{xyplane/.estyle={cm={cos(\phi - 90), 0,0,1,(\xsh, 0)}}}
\draw[xyplane,black,thin,opacity=1] (0, \rad) arc (90 : 270 : \rad);
}

% Draw the axis.
\pgfmathsetmacro\xdim{
\bDomain + \dRange*sin(\phi) + .5}
\draw[->] (0, -\dRange - .5) -- (0, \dRange + .5)
node[above] {$y$};

% Comment out the next four commands
% if you don't want an x-axis, and labels.
\draw[<->] (\aDomain -.5, 0) -- (\xdim, 0) node[right] {$x$};
\pgfmathsetmacro\xLabel{cos(\phi)*\bDomain}
\draw (\xLabel, .1) -- (\xLabel, -.1) node[below right] {\bDomain};
\draw (-.1, \dRange) -- (.1, \dRange) node[right] {\dRange};

% Draw the shading of the front half.
% Right half of a circle, rotated right
% (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.
\foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
\pgfmathsetmacro\xsh{(cos(\phi))*(\x)}
\tikzset{xyplane/.estyle={cm={cos(\phi - 90),0,0,1,(\xsh, 0)}}}
\draw[xyplane,\frontColor,ultra thick,opacity=.6] (0, -\rad) arc (-90 : 90 : \rad);
}

% Front longitude lines.
\foreach \theta in {0, \rotationGridStepsize, ..., 180} {
\tikzset{xyplane/.estyle={cm={cos(\phi), 0, sin(\theta)*sin(\phi),cos(\theta),(0, 0)}}}
\draw[xyplane,smooth] plot (\x, \fcn) ;
}

% Front latitude lines.
% Right half of a circle, rotated right
% (around y-axis) 90 - \phi degrees and
% shifted right or left to the correct height.
\foreach \x in {\aDomain, \nextStep, ..., \bDomain}{
\pgfmathsetmacro\xsh{(cos(\phi))*(\x)}
\tikzset{xyplane/.estyle={cm={cos(\phi-90),0,0,1, (\xsh, 0)}}}
\draw[xyplane] (0, -\rad) arc (-90 : 90 : \rad);
}
\end{tikzpicture}
\caption{$f(x)=\sin{x}$ rotated about the $x$-axis.}
\label{rot1x}
\end{center}
\end{figure}

\end{document}


-
Thanks so much! I have one silly question more: How to scale this figure to exact match to the width of text column? – krul Jul 12 '14 at 12:09
Ok, I found it: \pgfmathsetmacro\scaleAttempt{3.4/\dRange} – krul Jul 12 '14 at 12:17