I'm having a lot of trouble with functions build from other functions. I'm struggling to understand the nuance of variable replacement. Here is an example:
p2[x_, l_, m_] := ((-1)^m / (2^l l!) * (1 - x^2)^(m/2) * D[(y^2 - 1)^l, {y, l + m}]) /. y -> x
p3[x_, l_, m_] = p2[x, l, m] * 1;
The p2 equation works, for example:
p2[x, 2, 1]
$-3x\sqrt{1-x^2}$, and p2[1/2, 2, 1]
$=-\frac{1}{4} \left(3 \sqrt{3}\right)$.
But the second equation doesn't work, p3[1/2, 2, 1]
gives the error: General::ivar: 1/2 is not a valid variable.
The second equation only works if I replace $x$ afterwards. p3[x, 2, 1] /. x -> 1/2
$=-\frac{1}{4} \left(3 \sqrt{3}\right)$. I don't understand this behavior.
SetDelayed
instead ofSet
. That is, add a colon:p3[x_, l_, m_] := p2[x, l, m] * 1;
$\endgroup$