函数绕 x 轴旋转

函数绕 x 轴旋转

我尝试从该文件中重现一些图形: http://tug.org/TUGboat/tb33-1/tb103wolcott.pdf

我正好需要图 7:f(x) = sin x 绕 x 轴旋转。图 7 的代码在附录 B 中。使用的包:

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

Texmaker 的错误:

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

这些部分的中间出现错误:

\foreach \x in
{\aDomain, \nextShadingStep, ..., \bDomain} {
\pgfmathsetmacro\xsh{(cos(\phi))*({\x})}
\pgfmathsetmacro\rad{(\fcn)}
\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) ;
}

我对 LaTeX 有所了解,但对 PGF/TikZ 一无所知。如何消除这些错误?

答案1

\x如果删除周围的花括号,它应该可以编译\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\aDomain{0}
    \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}
    %\pgfmathsetmacro\xShadingSteps{3* \speed}
    \pgfmathsetmacro\xShadingSteps{300}

    % 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\intervalLength{\bDomain - \aDomain}
      \pgfmathsetmacro\xGridStepsize{\intervalLength/\xGridSteps}
      \pgfmathsetmacro\xShadingStepsize{\intervalLength/\xShadingSteps}
      \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.
      \pgfmathsetmacro\nextShadingStep{\aDomain + \xShadingStepsize}

      \foreach \x in {\aDomain, \nextShadingStep, ..., \bDomain} {
        \pgfmathsetmacro\xsh{(cos(\phi))*(\x)}
        \pgfmathsetmacro\rad{(\fcn)}        
        \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.
      \pgfmathsetmacro\nextStep{\aDomain + \xGridStepsize}
      \foreach \x in {\aDomain,\nextStep, ...,\bDomain} {
        \pgfmathsetmacro\xsh{(cos(\phi))*(\x)}
        \pgfmathsetmacro\rad{(\fcn)}        
        \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)}
        \pgfmathsetmacro\rad{(\fcn)}
        \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)}
        \pgfmathsetmacro\rad{(\fcn)}
        \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}

在此处输入图片描述

相关内容