Faster calculation of complex functions

Letting gnuplot do the calculations may be a lot faster. For this case the pure pgfplots code typically took just over 5s on my machine, while the gnuplot version took between 0.5s and 0.6s.

It does of course require that gnuplot is installed, and one has to compile with shell-escape enabled, e.g. pdflatex --shell-escape file.tex.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\begin{document}

\def\anga{47} % latitude
\begin{tikzpicture}
\begin{axis}

\foreach \i in {-4,-3,...,4}{%
\addplot [black] gnuplot [raw gnuplot,id = p\i, mark=none] {
set parametric;
set trange [0:360];
set samples 60;
set angles degrees;
fx(t) = 180 + asin(cos(23.44*sin(t))*sin(15*(\i+0.165*sin(2*t) - 0.128*sin(t+5616/73)))/sqrt(1-(sin(\anga)*sin(23.44*sin(t)) + cos(\anga)*cos(23.44*sin(t))*cos(15*(\i+0.165*sin(2*t) - 0.128*sin(t+5616/73))))^2));
fy(t) = asin(sin(\anga)*sin(23.44*sin(t)) + cos(\anga)*cos(23.44*sin(t))*cos(15*(\i+0.165*sin(2*t) - 0.128*sin(t+5616/73))));
plot fx(t),fy(t)
};
}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here


Here is a solution using LuaLaTeX:

\documentclass{minimal}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{luacode}

\begin{document}

\begin{luacode*}
anga=47 --  latitude
function sindeg(x)
    return math.sin(x*math.pi/180)
end
function cosdeg(x)
    return math.cos(x*math.pi/180)
end
function plot_curve(i)
    local N=60
    tex.sprint("\\addplot[line width=1pt,black] coordinates {")
    for theta=0,360,360/N do
        fct1=0.165*sindeg(2*theta)-0.128*sindeg(theta+5616/73)
        fct2=cosdeg(23.44*sindeg(theta))
        x=180+180/math.pi*math.asin(fct2*sindeg(15*(i+fct1))/math.sqrt(1-(sindeg(anga)*sindeg(23.44*sindeg(theta))+cosdeg(anga)*fct2*cosdeg(15*(i+fct1)))^2))
        y=180/math.pi*math.asin(sindeg(anga)*sindeg(23.44*sindeg(theta))+cosdeg(anga)*fct2*cosdeg(15*(i+fct1)))
        tex.sprint("("..x..","..y..")")
    end
    tex.print("};")
end
\end{luacode*}

\begin{tikzpicture}
\begin{axis}

\foreach \i in {-4,-3,...,4}{%
    \directlua{plot_curve(\i);}
}
\end{axis}
\end{tikzpicture}
\end{document}

Since Lua uses angles in radians, I had to redefine the sine and cosine functions in radians. It's then very fast.

Of course you should compile it using lualatex command.

Tags:

Pgfplots