I am making some symbolical calculations for my quantum chemistry class. I am supposed to build a wave function composed of multiple other functions, each of which is also composite of some lower level ones. The problem is I have polynomials defined through differentiation, with up and down actually being parameters:

Legendre[up_, down_] = (1 - x^2)^(up/2)*D[1/(2^down*down!)* D[ (x^2 - 1)^down, {x, down}], {x, up}];


Laguerre[up_, down_] = D[E^x*D[x^down*E^(-x), {x, down}], {x, up}];

Which despite complicated calculation are simply polynomials of x: Simplify[Leg[3, 4]] yields 105 x (1 - x^2)^(3/2) (well, polynomials with roots...). Now when I need to substitute an argument 2*r/n in place of x it will only work as long as up and down arguments are provided as numbers, otherwise it will throw an ivar::error when trying to differentiate with respect to 2*r/n. In further steps of calculation I'd like to use R[n_, l_, r_] = (2*r/n)^l*Legendre[2 l + 1, n + l, 2*r/n]*E^(-r/n); (n_, l_ are parameters, r the actual variable) which will trigger said error, as the parameter 2*r/n is not a proper variable. Is there a way to define a function Legendre2[up_,down_,a_] which would work in above equation? I need to make mathematica do two things: accept the fact that such polynomial has its coefficients dependent on parameters up and down, and then to substitute it with my variable only once its form is known, and do so regardless of its derivation history (differentiation, factorials and all that).

By googling this ivar problem seems to be a common thing when plotting derivatives, but the solutions I've found, such as swapping = for := or using Evaluate[] only work as long as the function is evaluated as it is, and not plugged into some higher level functions.

  • 1
    $\begingroup$ I'd like to point out that much of what you're doing is unnecessary, since the associated Legendre polynomials and Laguerre polynomials are already built-in to Mathematica via LegendreP and LaguerreL. $\endgroup$ Nov 23, 2014 at 17:10
  • 1
    $\begingroup$ Likewise, seeing as you're probably using this for simulating hydrogen-like wavefunctions, note that the spherical harmonics are already built-in to Mathematica via SphericalHarmonicY. So you can basically skip all these constructions and just enter the entire atomic wavefunction directly in terms of Laguerre polynomials and spherical harmonics, which can be done in a single line of code. $\endgroup$ Nov 23, 2014 at 17:14
  • $\begingroup$ Unless your assignment specifically is to use Mathematica, you can do these calculations by hand with little difficulty, and learn more in the process. If your assignment calls for using Mathematica, that too can be done, of course. $\endgroup$
    – bbgodfrey
    Nov 23, 2014 at 17:30

1 Answer 1


Seeing as you're trying to evaluate hydrogen wavefunctions, note that the necessary special functions are already built-in, so you can skip the step of defining the special functions entirely, and just do this:

ψ[n_, l_, m_, ρ_, θ_, ϕ_] := 
  Sqrt[(2/(n a0))^3 (n - l - 1)!/(2 n (n + l)!)]
    Exp[-ρ/2] ρ^
   l LaguerreL[n - l - 1, 2 l + 1, ρ] SphericalHarmonicY[l, 
    m, θ, ϕ];

If you still would like to implement the Legendre polynomials yourself, you can modify your code as follows:

Legendre[up_, down_, 
   X_] := ((1 - x^2)^(up/2)*
     D[1/(2^down*down!)*D[(x^2 - 1)^down, {x, down}], {x, up}]) /. 
   x -> X;
Plot[Legendre[2, 4, a], {a, -1, 1}]
Legendre[2, 3, 2 r/n]

which produces

enter image description here

(30 r (1 - (4 r^2)/n^2))/n

The reason this works is because it first differentiates with respect to the symbolic parameter x, and then replaces all occurrences of x with the third argument of Legendre. Your original code doesn't work because it tries to differentiate with respect to the expression 2r/n, which is not a valid variable.

A similar fix will make your Laguerre definition work correctly.

  • $\begingroup$ Thank you a lot for those suggestions, but one of points of this exercise is to build the functions 'manually', that is from basics. Regardless of the exact goal of that particular calculation I would also like to know how to handle this kind of situations in future $\endgroup$
    – Jatentaki
    Nov 23, 2014 at 17:19
  • $\begingroup$ @Jatentaki: Does my edit help? $\endgroup$ Nov 23, 2014 at 17:35
  • $\begingroup$ Yes, thank you a lot! $\endgroup$
    – Jatentaki
    Nov 23, 2014 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.