0

I am trying to do some plots with spherical coordinates and have a problem when computing some numbers in PGFPLOTS.

I add a MWE here:

\documentclass[12pt]{standalone}
\usepackage[pdftex]{graphicx}
\usepackage{amsmath}
\usepackage{tikz,pgfplots}


\begin{document}

 % compute angles
    \pgfmathsetmacro\a{-pi/5}
    \pgfmathsetmacro\b{pi/10}
    \pgfmathsetmacro\phil{\a-\b}
    \pgfmathsetmacro\phih{\a+\b}
    \pgfmathsetmacro\thetal{\a-\b}
    \pgfmathsetmacro\thetah{\a+\b}

    \pgfmathsetmacro\s{cos(\thetal)*sin(\phil)}
    % this should be about -0.476
    \pgfmathprintnumber{\s}


\end{document}

Here is the result:

wrong computation

I should get $-0.4755282581475768$

3 Answers 3

4

sin and cos want their argument in degree. Though they can be given in radians as well by appending r (manual, section 94.3.4).

\documentclass[12pt]{standalone}
\usepackage[pdftex]{graphicx}
\usepackage{amsmath}
\usepackage{tikz,pgfplots}


\begin{document}

 % compute angles
 %   \pgfmathsetmacro\a{-180/5}
 %   \pgfmathsetmacro\b{180/10}
    \pgfmathsetmacro\a{-pi/5 r}
    \pgfmathsetmacro\b{pi/10 r}
    \pgfmathsetmacro\phil{\a-\b}
    \pgfmathsetmacro\phih{\a+\b}
    \pgfmathsetmacro\thetal{\a-\b}
    \pgfmathsetmacro\thetah{\a+\b}

    \pgfmathsetmacro\s{cos(\thetal)*sin(\phil)}
    % this should be about -0.476
    \pgfmathprintnumber{\s}


\end{document}
3
  • Thanks for the answer. Is there a way to tell PFGPLOTS that the unit for angle is radians? I ask becuause I had this set up in my script and it ignored it for computations: trig format plots=rad, Sep 22, 2020 at 12:37
  • 1
    @HermanJaramillo Yes, see edit.
    – AlexG
    Sep 22, 2020 at 12:44
  • 2
    @HermanJaramillo While experimental (see page 1037-38 in the TikZ manual), you can try \pgfkeys{/pgf/trig format=rad}. Sep 22, 2020 at 14:34
2

For me, I would rather go with xfp package to do the calculations for me due to its wide list of functions and the ability to handle big numbers operations without such limitations.

enter image description here

\documentclass[12pt,border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{xfp,siunitx}


\begin{document}
 % compute angles
    \edef\a{-pi/5}
    \edef\b{pi/10}
    \edef\phil{\a-\b}
    \edef\phih{\a+\b}
    \edef\thetal{\a-\b}
    \edef\thetah{\a+\b}

    \edef\s{\fpeval{cos(\thetal)*sin(\phil)}}
    % this should be about -0.476
    \num{\s}
\end{document}

enter image description here

0

A dirty and overcomplicated trick

% xop.tex
%   run 
% pdflatex xop.tex
% asy xop-*.asy
% pdflatex xop.tex

\documentclass[12pt]{standalone}
\usepackage[pdftex]{graphicx}
\usepackage{amsmath}
\usepackage{tikz,pgfplots}
\usepackage[inline]{asymptote}

\begin{document}

\gdef\setasy#1{\gdef\s{#1}}
\pgfmathsetmacro\s{0}
\begin{asy}
  // compute angles
  real a=-pi/5;
  real b=pi/10;
  real phil=a-b;
  real phih=a+b;
  real thetal=a-b;
  real thetah=a+b;
  real s=cos(thetal)*sin(phil);
  tex("\setasy{"+string(s)+"}");
\end{asy}

\pgfmathprintnumber{\s} (\s)

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .