I'm trying to tell maxima about a recurrence relation for the hermite polynomials:
My first expression is like this:
phi[0]:exp(-1/2*x^2);
phi[1]:sqrt(2)*x*phi[0];
wxplot2d([phi[0],phi[1]], [x,-5,5]);
So far so good, but I'd like now to define all the others by:
phi[n]:sqrt(2/n)*x*phi[n-1] - sqrt((n-1)/n)*phi[n-2];
This just bombs (stack overflow). What do I really want to say so that
wxplot2d(phi[10], [x,-5,5]) will give me a sensible picture?
There's more than one way to handle this. Here is one way that works.
(%i2) phi[n](x) := sqrt(2/n)*x*phi[n-1](x) - sqrt((n-1)/n)*phi[n-2](x) $
(%i3) phi[0] : lambda ([x], exp(-1/2*x^2)) $
(%i4) phi[1] : lambda ([x], sqrt(2)*x*phi[0](x)) $
(%i5) phi[0];
(%o5) lambda([x],exp((-1)/2*x^2))
(%i6) phi[1];
(%o6) lambda([x],sqrt(2)*x*phi[0](x))
(%i7) phi[2];
(%o7) lambda([x],sqrt(2)*x^2*%e^-(x^2/2)-%e^-(x^2/2)/sqrt(2))
(%i8) phi[3];
(%o8) lambda([x],
sqrt(2)*x*(sqrt(2)*x^2*%e^-(x^2/2)-%e^-(x^2/2)/sqrt(2))/sqrt(3)
-2*x*%e^-(x^2/2)/sqrt(3))
(%i9) phi[10];
<very large expression here>
(%i10) plot2d (%, [x, -5, 5]);
<nice plot appears>
This makes use of so-called array functions. For any integer n
, phi[n]
is a lambda expression (unnamed function).
Note that this only works for literal integers (e.g., 0, 1, 2, 3, ...). If you need to work with phi[n]
where n
is a symbol, we can look for a different approach.