I need help to check my code if my way to generate the gradient of a function that contains Legendre polynomials is correct or not.

if f is a function in θ and κ ,I need the gradient of f with respect to θ, where its derivative with respect to κ is zero because κ is a constant.The last equation to obtain gradient f is correct or there is another way?

k = 0.70945;
tl0 = -0.021720662277045163 - 0.007146744456323989 I;
tl1 = 0.00019355365528147435 + 6.204118924315627*^-6 I;
tl2 = 0.0031603066656386166 + 0.0003213571906147552 I;
tl3 = 0.00019355365528147435 + 6.204118924315627*^-6 I;
t = {tl0, tl1, tl2, tl3}
t // MatrixForm

kappa = 0.0814776349681311;
lmax = 3
θmax = 10;
θarray =
Table[(π i)/θmax, {i, 0, θmax}] // N;

f = Sqrt[2/(k*\[Kappa])]*Sum[(2*l + 1)*t[[l + 1]]*LegendreP[l, Cos[\[Theta]array]], {l, 0, lmax}]

gradf = (1/\[Kappa])*Sqrt[2/(k*\[Kappa])]*Sum[(2*l + 1)*t[[l + 1]]*LegendreP[l, 1, Cos[\[Theta]array]],
{l, 0, lmax}]

• OK, but where the parameter θ is used? – Alex Trounev Dec 5 '18 at 22:29
• does your answer mean that my way is correct??I used θ as an array in Cosθ. I used LegendreP[n,m,z], where n=L,m=1,z=Cosθ to express dP[L,z]/dθ – Ghady Dec 6 '18 at 1:29
• Please post code in InputForm; you may find this meta Q&A helpful. – Michael E2 Dec 6 '18 at 1:58
• Thanks Michael! Now, I posted in InputForm. – Ghady Dec 6 '18 at 2:26

One can directly verify that the derivative $$dP_l(\cos(\theta))/d\theta$$ and $$P_l^1(\cos(\theta))$$ coincide in the interval $$(0,\pi )$$ using the well-known formula f = Sqrt[2/(k*\[Kappa])]*Sum[(2*l + 1)*t[[l + 1]]*LegendreP[l, Cos[\[Theta]array]], {l, 0, lmax}]