MATLAB: Legendre polynomial Symbolic derivative

derivativelegendre polynomial functionsymbolic

I need to find derivative with respect to the argument of Associated Legendre functions symbolically . The following does not work:
x=sym('x', 'real');
q=sym('q', 'positive'); % harmonic index
t=sym('t', 'real');
x=cos(t);
P=legendre(q,x )% Associated Legendre function
P_1st_der=diff(P,x)
Can anyone help me?
Thanks
Irfan

Best Answer

That's a bit messy because the MUPAD legendre() function returns an array of all the associated Legendre values of order 0 to q.
When LegendreP(q, u, f(x)) is the associated Legendre function of degree q and order u, u from 0:q, then
diff(LegendreP(q, u, f(x)), x)
is
((q-u+1)*LegendreP(q+1, u, f(x))-(q+1)*f(x)*LegendreP(q, u, f(x)))*(diff(f(x), x))/(f(x)^2-1)
If x is a vector then that would probably have to be
((q-u+1)*LegendreP(q+1, u, f(x))-(q+1)*f(x).*LegendreP(q, u, f(x))).*(diff(f(x), x)) ./ (f(x).^2-1)
To construct an entire matrix of these symbolically over u = 0:q, you would have to throw in some repmat() on the portions having to do with f(x) appearing outside of your corresponding legendre() calls (since legendre() returns a matrix so you would have to copy f(x) q+1 times in order to do the element-wise multiplication and division)
It would be easier if you only needed one particular degree and order -- e.g.,
LegendreP(q+1, u, f(x))
would become
Lq1 = legendre(q+1,f(x));
then use
Lq1(u+1,:)