<p>I don't have a specific reference that defines the recursion in the right form, but I've transcribed it from code I have that I've verified the output of. I think you want these, but please correct me if this doesn't give the result you expect:</p>
<p>Where <code>P(n,m)</code> is the Schmidt normalized associated Legendre polynomial of degree <code>n</code> and order <code>m</code>, and <code>dP(n,m)</code> is the derivative with respect to co-latitude <code>theta</code>.</p>
<pre><code>dP(0, 0) = 0
dP(1, 1) = cos(theta)
dP(n, n) = sqrt(1 - 1/(2n))*(sin(theta)*dP(n-1, n-1) + cos(theta)*P(n-1, n-1))
dP(n, m) = (2n - 1)/sqrt(n^2 - m^2)*(cos(theta)*dP(n-1, m) - sin(theta)*P(n-1, m)) - sqrt(((n-1)^2 - m^2)/(n^2 - m^2))*dP(n-2, m)
</code></pre>
<p>I'm sure there is an IGRF implementation in C somewhere, but you can find C code for a different model that contains the required Legendre functionality <a href="https://geomag.us/models/pomme11.html" rel="nofollow noreferrer">here</a>.</p>
<p>Also check the implementation of the Legendre polynomials you are using and whether they include the Condon-Shortley phase factor of <code>(-1)^m</code>, if they do, the normalisations also need to include it, so that it ultimately cancels back out. It is standard in geomagnetism to not apply this factor.</p>

CC BY-SA 4.0
2021-06-14T19:37:17.677Z
2021-06-15T09:23:50.650Z
2021-06-15T09:23:50.650Z
WJB
<p>I have been trying to implement the IGRF-13 geomagnetic model on my own in C code. I have followed the equations from the website below and I am getting correct results for the B<span class="math-container">$_{\phi}$</span> and B<sub>r</sub> magnetic field strength components.</p>
<p>But right now I am completely stuck on the B<span class="math-container">$_{\theta}$</span> component which requires calculating the Partial derivatives of the Schmidt Normalized Associated Legendre Polynomials with respect to theta according to equation 3b.
<a href="https://i.sstatic.net/AcGwh.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/AcGwh.png" alt="enter image description here" /></a></p>
<p>Now, how do I do that? The recursive equations 19 a, b, and c are not producing the correct results, and there is no information on the internet about this.</p>
<p>I would really appreciate it if someone could provide a sample code that calculates the &quot;Partial derivatives of the Associated Legendre Polynomials&quot; or maybe provide the equations that will allow me to calculate them.</p>
<p>Reference: <a href="https://hanspeterschaub.info/Papers/UnderGradStudents/MagneticField.pdf" rel="nofollow noreferrer">Mathematical Modeling of Earth’s Magnetic Field</a> (PDF)</p>

CC BY-SA 4.0
2021-06-11T06:52:38.383Z
Does playing around with this on Wolfram Alpha help? This is a table of the first number of derivatives you're looking for (I think!), perhaps you can reconstruct the recurrence relationships from there? https://www.wolframalpha.com/input/?i=Table%5B+D%5BLegendreP%5Bn%2C+m%2C+θ+%5D%2C+θ+%5D+%2C+%7Bn%2C0%2C5%7D%2C+%7Bm%2C0%2C5%7D+%5D
Erik
CC BY-SA 4.0
2021-06-11T06:58:15.077Z
And if the question is instead about (3a)--(3c), you may want to have a look at the "Wertz" reference to check for any typos in the notes you have.
Erik
CC BY-SA 4.0
2021-06-11T09:00:23.780Z
If you want to migrate this to Scicomp, please flag for moderator attention and one of us will migrate it.
gerrit
<geophysics><geomagnetism>
IGRF -13 Geomagnetic field, Calculating Partial Derivatives of Legendre Polynomials
Sandman
# IGRF -13 Geomagnetic field, Calculating Partial Derivatives of Legendre Polynomials

(Asked by: username_0 on 2021-06-09)

I have been trying to implement the IGRF-13 geomagnetic model on my own in C code. I have followed the equations from the website below and I am getting correct results for the B$\_{\phi}$ and Br magnetic field strength components.

But right now I am completely stuck on the B$\_{\theta}$ component which requires calculating the Partial derivatives of the Schmidt Normalized Associated Legendre Polynomials with respect to theta according to equation 3b.
[![enter image description here](https://i.sstatic.net/AcGwh.png)](https://i.sstatic.net/AcGwh.png)

Now, how do I do that? The recursive equations 19 a, b, and c are not producing the correct results, and there is no information on the internet about this.

I would really appreciate it if someone could provide a sample code that calculates the "Partial derivatives of the Associated Legendre Polynomials" or maybe provide the equations that will allow me to calculate them.

Reference: [Mathematical Modeling of Earth’s Magnetic Field](https://hanspeterschaub.info/Papers/UnderGradStudents/MagneticField.pdf) (PDF)

**Tags:** geophysics, geomagnetism

> **Comments:**
> **username_1**: Does playing around with this on Wolfram Alpha help? This is a table of the first number of derivatives you're looking for (I think!), perhaps you can reconstruct the recurrence relationships from there? https://www.wolframalpha.com/input/?i=Table%5B+D%5BLegendreP%5Bn%2C+m%2C+θ+%5D%2C+θ+%5D+%2C+%7Bn%2C0%2C5%7D%2C+%7Bm%2C0%2C5%7D+%5D  
> **username_1**: And if the question is instead about (3a)--(3c), you may want to have a look at the "Wertz" reference to check for any typos in the notes you have.  
> **username_2**: If you want to migrate this to Scicomp, please flag for moderator attention and one of us will migrate it.  

---

## 1 answers

### Answer by username_3 on 2021-06-14. Score: 1

I don't have a specific reference that defines the recursion in the right form, but I've transcribed it from code I have that I've verified the output of. I think you want these, but please correct me if this doesn't give the result you expect:

Where `P(n,m)` is the Schmidt normalized associated Legendre polynomial of degree `n` and order `m`, and `dP(n,m)` is the derivative with respect to co-latitude `theta`.

```
dP(0, 0) = 0
dP(1, 1) = cos(theta)
dP(n, n) = sqrt(1 - 1/(2n))*(sin(theta)*dP(n-1, n-1) + cos(theta)*P(n-1, n-1))
dP(n, m) = (2n - 1)/sqrt(n^2 - m^2)*(cos(theta)*dP(n-1, m) - sin(theta)*P(n-1, m)) - sqrt(((n-1)^2 - m^2)/(n^2 - m^2))*dP(n-2, m)

```

I'm sure there is an IGRF implementation in C somewhere, but you can find C code for a different model that contains the required Legendre functionality [here](https://geomag.us/models/pomme11.html).

Also check the implementation of the Legendre polynomials you are using and whether they include the Condon-Shortley phase factor of `(-1)^m`, if they do, the normalisations also need to include it, so that it ultimately cancels back out. It is standard in geomagnetism to not apply this factor.


---

