7

I borrowed some code on Kepler's 2nd Law from the linked post here on TeX answered by Tosco. I haven't compiled the document it is in awhile but needed today. When I did, I was told dimension too large. The dimension too large points to the following lines:

% \draw[very thin,->](\oldx,\ol dy)--++(\forcex,\forcey);
\ifnumgreater{(\n - \startone) * (\endone - \n)}{-1}

Prior to some update(not sure which one broke it), this code compiled and worked correctly. Now it doesn't. What can be done about this?

\documentclass[convert = false, tikz]{standalone}

\usepackage{amsmath}
\usepackage{etoolbox}
\usetikzlibrary{backgrounds}

\begin{document}

\gdef\myposx{10.0}
\gdef\myposy{0.0}
\gdef\vx{0.0}
\gdef\vy{4.6}
\gdef\forcefactor{150}
\gdef\deltat{0.025}
\gdef\smallmass{1}
\gdef\startone{100}
\gdef\endone{200}
\gdef\starttwo{1800}
\gdef\endtwo{1900}
\gdef\pathone{}
\gdef\pathtwo{}

\begin{tikzpicture}[scale = .2]
  \filldraw(0, 0) circle [radius = 0.5cm];
  \foreach \n in {1, ..., 3625}
  {
    \pgfextra{%
      \global\let\oldx\myposx
      \global\let\oldy\myposy
      \pgfmathsetmacro{\currentsquareddistance}{\myposx * \myposx + \myposy *
        \myposy}
      \pgfmathsetmacro{\currentforce}{\forcefactor / \currentsquareddistance}
      \pgfmathsetmacro{\currentangle}{atan2(\myposx, \myposy)}
      \pgfmathsetmacro{\currentforcex}{-1 * \currentforce * cos(\currentangle)}
      \pgfmathsetmacro{\currentforcey}{-1 * \currentforce * sin(\currentangle)}
      \pgfmathsetmacro{\currentvx}{\vx + \deltat * \currentforcex / \smallmass}
      \pgfmathsetmacro{\currentvy}{\vy + \deltat * \currentforcey / \smallmass}
      \pgfmathsetmacro{\currentposx}{\myposx + \deltat * \currentvx}
      \pgfmathsetmacro{\currentposy}{\myposy + \deltat * \currentvy}
      \global\let\myposx\currentposx
      \global\let\myposy\currentposy
      \global\let\vx\currentvx
      \global\let\vy\currentvy
      \global\let\forcex\currentforcex
      \global\let\forcey\currentforcey
      \global\let\myangle\currentangle
      \ifnumequal{\n}{\startone}{%
        \global\let\startonex\oldx
        \global\let\startoney\oldy
        \xappto{\pathone}{(\oldx, \oldy)}
      }{}
      \ifnumequal{\n}{\starttwo}{%
        \global\let\starttwox\oldx
        \global\let\starttwoy\oldy
        \xappto{\pathtwo}{(\oldx, \oldy)}
      }{}
      \ifnumequal{\n}{\endone}{%
        \global\let\endonex\myposx
        \global\let\endoney\myposy
        \xappto{\pathone}{,(\myposx,\myposy)}
      }{}
      \ifnumequal{\n}{\endtwo}{%
        \global\let\endtwox\myposx
        \global\let\endtwoy\myposy
        \xappto{\pathtwo}{,(\myposx, \myposy)}
      }{}
    }
    % \draw[very thin,->](\oldx,\ol dy)--++(\forcex,\forcey);
    \ifnumgreater{(\n - \startone) * (\endone - \n)}{-1}
    {
      \pgfextra{%
        \xappto{\pathone}{,(\myposx, \myposy)}
      }
    }
    {}
    \ifnumgreater{(\n - \starttwo) * (\endtwo - \n)}{-1}
    {
      \pgfextra{%
        \xappto{\pathtwo}{,(\myposx, \myposy)}
      }
    }
    {}
    \draw(\oldx, \oldy)--(\myposx, \myposy);
  }
  \begin{scope}[on background layer]
    \filldraw[blue, opacity = .5] (0, 0)%
    \foreach \point in \pathone
    {%
      --\point
    }--(0, 0);
    \filldraw[blue, opacity = .5] (0, 0)%
    \foreach \point in \pathtwo
    {%
      --\point
    }--(0, 0);
  \end{scope}
\end{tikzpicture}
\end{document}
yo'
  • 51,322
dustin
  • 18,617
  • 23
  • 99
  • 204

1 Answers1

10

The problem is that the syntax of atan2, the two-argument variant of arctangent, changed between TikZ 2.10 and TikZ 3.0.

The effect of this change of syntax on Toscho's code makes the trajectory of the orbiting body (an asteroid?) become completely unstable very quickly: the asteroid quickly "shoots off the page". As as a result, the dimension of the resulting diagram rapidly exceeds what TeX can handle (about 19 feet), and TeX complains by issuing Dimension too large errors.

Let's examine the description of atan2 in the v2.10 and v3.0 manuals a bit closer...

TikZ v2.10, released 2010/12/20 (63.2.3 Trigonometric functions)

enter image description here

TikZ 3.0, released 2013/12/20 (90.2.4 Trigonometric functions)

enter image description here

Problem

In both manuals, the inputs in the examples are the same, the syntax is the same... or is it?! As pointed out by percusse in his comment, the two arguments, x and y, have been swapped between the two releases! Therefore, the outputs differ. Those TikZ maintainers sure are sneaky }:-)

Solution

Simply swapping the arguments of atan2 in your code, i.e. writing

\pgfmathsetmacro{\currentangle}{atan2(\myposy, \myposx)}

instead of

\pgfmathsetmacro{\currentangle}{atan2(\myposx, \myposy)}

produces the correct output, shown below.

enter image description here

jub0bs
  • 58,916
  • Can you confirm that the trajectory is supposed to be elliptical with Toscho's values? Maybe the tikz maintainers changed the implementation of the atan2 function and introduced some bug in the process... – jub0bs Apr 16 '14 at 01:27
  • The only difference between what I had and the original was the first filldraw circle and delta t (I took a bigger step). Changing to Toscho's code still produces the same error as well. – dustin Apr 16 '14 at 01:34
  • 1
    Notice that the arguments are swapped. – percusse Apr 16 '14 at 09:28
  • @percusse You're right! I hadn't noticed it... I'm going to hit the max number of edits before CC wiki quickly on this answer... – jub0bs Apr 16 '14 at 09:30