File Exchange

image thumbnail

First derivative of (normal) associated Legendre polynomials

version (6.7 KB) by Rody Oldenhuis
First derivative of (normalized) associated Legendre polynomials


Updated 25 Sep 2019

GitHub view license on GitHub

Computing accurate derivatives of the associated Legendre polynomials can be tricky. Even in advanced texts, they are usually written as recurrence relations and/or with (normalization) factors involving factorials.
A naive software implementation will therefore quickly run into the limits of IEEE754 double-precision, resulting in NaN/inf or significant loss of precision, already at very low degree N.
LEGENDRE_DERIVATIVE is a fully vectorized, numerically stable and robustly validated implementation of the derivative computation. It allows fast and accurate computations of the derivatives for any degree N.

It works the same as MATLAB's own LEGENDRE, except it does not compute the polynomial values, but the values of the derivatives.

Cite As

Rody Oldenhuis (2020). First derivative of (normal) associated Legendre polynomials (, GitHub. Retrieved .

Comments and Ratings (10)

@GardMatthew (and others): I just updated this submission—I added units tests with close to 100% coverage. These tests include the edge cases under all normalizations, and they all pass ?


Hi again Rody, hoping you can supply the updated code soon as I have yet to find a fix myself. Was hoping you uploaded to github alone but doesnt seem to have been updated either. Thanks!

Jorg Woehl

Thanks for your work - is there an easy way that you would suggest to fix the edge cases (x = 1, -1)?

To all: YES! I plan to fix this! I am aware that the edge cases are inaccurate, and that the Schmitt semi-normalized values are computed inefficiently and inaccurately. I have actually already fixed these problems as part of a task in my daytime job, now it's "just" a matter of finding the time to copy those fixes over to this repository.


Hi this is great, but I have the same question as matzl_m -> whats causing the NaN's in the first columns? Do you plan on fixing this?


Thanks for the nice work. I noticed that derivatives at x = 1,-1 can't be calculated and just give NaNs for every Legendre-polynomial, even though they are finite for all |m| ~= 1. Did you plan to fix this?


thank Rody Oldenhuis


Added units tests
Corrected bug related to edge cases for normalized polynomials

Title update

MATLAB Release Compatibility
Created with R2010a
Compatible with R2008a to any release
Platform Compatibility
Windows macOS Linux