This is my attempt at trying to resolve the issue of different results from $P_{n}\prime cos \theta$ in eqn. 12.98. 




For this solution App II, eqn. 65 is referred to. 



Applying the appendix II eqn. 65, to 12.98, I'd done the following. 


```python


from sympy import symbols, diff, legendre, cos, sin, simplify, pi
alpha, m,n,z,theta = symbols('alpha m n z theta')

```


```python
Pnz = legendre(n,z) # the 'generic term'
```

## 1) The 'naive' substitution where $z = cos \theta$
This is what I'd done before, and shown Gaurav.



```python
generic_subs = simplify(diff(Pnz, z).subs(z, cos(theta)))
generic_subs

```




$\displaystyle - \frac{n \left(\cos{\left(\theta \right)} P_{n}\left(\cos{\left(\theta \right)}\right) - P_{n - 1}\left(\cos{\left(\theta \right)}\right)\right)}{\sin^{2}{\left(\theta \right)}}$



## 2) Do the partial derivative ( $\frac{\partial}{\partial \theta}P_{n}\prime cos \theta$ ) anew with sympy


```python
Pncostheta = legendre(n, cos(theta))
specific_partialdiff = simplify(diff(Pncostheta, theta))
specific_partialdiff
```




$\displaystyle \frac{n \left(\cos{\left(\theta \right)} P_{n}\left(\cos{\left(\theta \right)}\right) - P_{n - 1}\left(\cos{\left(\theta \right)}\right)\right)}{\sin{\left(\theta \right)}}$



This already gives something different!!

## 3) eqn. 12.98 as written in textbook


```python
eqn1298 = - (n*(n+1)/((2*n+1)*sin(theta)))*(legendre(n-1, cos(theta))-legendre(n+1, cos(theta)))
eqn1298
```




$\displaystyle - \frac{n \left(n + 1\right) \left(P_{n - 1}\left(\cos{\left(\theta \right)}\right) - P_{n + 1}\left(\cos{\left(\theta \right)}\right)\right)}{\left(2 n + 1\right) \sin{\left(\theta \right)}}$




```python
n_values = [2, 10, 21]
theta_values = [pi/4, pi/15, 1.98*pi]

for nval, thetaval in zip(n_values, theta_values):
    values = {'theta': thetaval,
            'n': nval}
    # substitute numeric values and see what happens
    subst_values = [ eqn1298.subs(values).evalf(), specific_partialdiff.subs(values).evalf(), generic_subs.subs(values).evalf()]
    print(subst_values)
```

    [-1.50000000000000, -1.50000000000000, 2.12132034355964]
    [-5.85744699177188, -5.85744699177188, 28.1727639688434]
    [11.4510073170455, 11.4510073170455, 182.368411710620]


The order of numeric values is from the equations defined by cases 3),2) and 1) in that order.

## The textbook eqn. 12.98 is CORRECT! 
It was my naive substitution that led to different results somewhere. 

## Matter 2: *m,n* index ordering. 
This is me verifying stuff for the [Mathematics Exchange post](https://math.stackexchange.com/q/4156607/933933) I opened. 


In the textbook, the solution for $K_{mn}$ (eqn. 12.107), when $m \neq n$ is given by: 

$\frac{sin\:\alpha( P_{m}(cos\:\alpha)P^{\prime}_{n}(cos\:\alpha) - P_{n}(cos\:\alpha)P^{\prime}_{m}(cos\:\alpha))}{m(m+1) - n(n+1)}$

However, the Mathematica code implementation has the equivalent of:

$\frac{sin\:\alpha( P_{n}(cos\:\alpha)P^{\prime}_{m}(cos\:\alpha) - P_{m}(cos\:\alpha)P^{\prime}_{n}(cos\:\alpha))}{m(m+1) - n(n+1)}$




```python
Pncosalpha = legendre(n, cos(alpha))
Pmcosalpha = Pncosalpha.subs(n,m)

Pnpr_cosalpha = diff(Pncosalpha, alpha)
Pmpr_cosalpha = diff(Pmcosalpha, alpha)

```


```python
intextbook = (sin(alpha)/(m*(m+1)-n*(n+1)))*(Pmcosalpha*Pnpr_cosalpha - Pncosalpha*Pmpr_cosalpha)
intextbook
```




$\displaystyle \frac{\left(\frac{m \left(\cos{\left(\alpha \right)} P_{m}\left(\cos{\left(\alpha \right)}\right) - P_{m - 1}\left(\cos{\left(\alpha \right)}\right)\right) \sin{\left(\alpha \right)} P_{n}\left(\cos{\left(\alpha \right)}\right)}{\cos^{2}{\left(\alpha \right)} - 1} - \frac{n \left(\cos{\left(\alpha \right)} P_{n}\left(\cos{\left(\alpha \right)}\right) - P_{n - 1}\left(\cos{\left(\alpha \right)}\right)\right) \sin{\left(\alpha \right)} P_{m}\left(\cos{\left(\alpha \right)}\right)}{\cos^{2}{\left(\alpha \right)} - 1}\right) \sin{\left(\alpha \right)}}{m \left(m + 1\right) - n \left(n + 1\right)}$




```python
incode = (sin(alpha)/(m*(m+1)-n*(n+1)))*(Pncosalpha*Pmpr_cosalpha - Pmcosalpha*Pnpr_cosalpha)
incode
```




$\displaystyle \frac{\left(- \frac{m \left(\cos{\left(\alpha \right)} P_{m}\left(\cos{\left(\alpha \right)}\right) - P_{m - 1}\left(\cos{\left(\alpha \right)}\right)\right) \sin{\left(\alpha \right)} P_{n}\left(\cos{\left(\alpha \right)}\right)}{\cos^{2}{\left(\alpha \right)} - 1} + \frac{n \left(\cos{\left(\alpha \right)} P_{n}\left(\cos{\left(\alpha \right)}\right) - P_{n - 1}\left(\cos{\left(\alpha \right)}\right)\right) \sin{\left(\alpha \right)} P_{m}\left(\cos{\left(\alpha \right)}\right)}{\cos^{2}{\left(\alpha \right)} - 1}\right) \sin{\left(\alpha \right)}}{m \left(m + 1\right) - n \left(n + 1\right)}$




```python
subs_vals = {'alpha':pi/3, 'm':5,'n':2}
incode.subs(subs_vals)
```




$\displaystyle - \frac{147}{32768}$




```python
intextbook.subs(subs_vals)
```




$\displaystyle \frac{147}{32768}$




```python

```

c96ab46bdc09217b22b32d07dc9fc4a7e34eaded
ipynb
Jupyter Notebook
beamshapes/workshop/derivative-issue.ipynb
faroit/bat_beamshapes
9c2919b4e56dbd0cfc1039edc608ce1592b78df3
MIT
2022-01-05T02:06:33.000Z
2022-03-16T09:00:08.000Z
beamshapes/workshop/derivative-issue.ipynb
faroit/bat_beamshapes
9c2919b4e56dbd0cfc1039edc608ce1592b78df3
MIT
2021-08-10T20:48:34.000Z
2022-01-05T08:02:04.000Z
beamshapes/workshop/derivative-issue.ipynb
faroit/bat_beamshapes
9c2919b4e56dbd0cfc1039edc608ce1592b78df3
MIT
2022-01-04T14:50:17.000Z
2022-01-28T02:33:34.000Z
Qwen/Qwen-72B
1. YES
2. YES