I'm trying to plot Legendre polynomials, which can be done very easily by calling a recursive function. For instance, in Python this would like like this:
def legendre(xi, p):
if p==0:
return 1.
elif p==1:
return xi
elif p>=2:
return (1/p)*((2*p-1)*xi* legendre(xi, p-1) + (1-p)*legendre(xi, p-2))
However, if I want to plot these in pgfplots and it has turned to be very hard. I tried to follow this answer, but because of the recursive nature to the function (which calls the function with two different polynomial values), I can't implement it the same way as in the posted answer for the Fibonacci numbers. This is what I have so far:
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}[
evaluate={
function legendre(\x,\p) {
if \p == 0 then {
return 1;
} else {
return legendre2(\x, \p);
};
};
function legendre2(\x, \p) {
if \p == 1 then {
return \x;
} else {
return (1/\p)*((2*\p-1)*\x* legendre2(\x, \p-1) + (1-\p)*legendre2(\x, \p-2))
};
};
},
]
\begin{axis}
\addplot+ [domain=-1:1] {legendre(x,0)};
\addplot+ [domain=-1:1] {legendre(x,1)};
\addplot+ [domain=-1:1] {legendre(x,2)};
\end{axis}
\end{tikzpicture}
\end{document}
which can plot the first 2 Legendre polynomials. For the 3rd I enter in an infinite loop because I'm not calling the right function. I was wondering if there is a workaround this (I did not find a way to put a simple or statement like if \p == 0 or \p == 1 then so I could discriminate in this case for the first two orders.

