Legendre polynomials are a type of orthogonal polynomials which occur often in science and engineering. Hence, their generation is crucial to those fields. There are different ways to evaluate a Legendre polynomial, using generating functions, Rodrigues formula, recurrence relation, Gram-Schmidt orthogonalization etc. One of the easiest and also
one of the most accurate, method is using recurrence relation.
Here we use Bonnet’s recurrence relation of legendre polynomials, i.e, –
It can be implemented using Python by proceeding as follows-
We define Legendre polynomials as a function named P(n, x), where n is called the order of the polynomial and x is the point of evaluation. The base cases are if n is 0, then The value of the polynomial is always 1, and it is x when order is 1. These are the seed values required for the recurrence relation.
For other values of n, the function is recursively defined, directly from the Bonnet’s recurrence. Thus, P(n, x) returns values of the Legendre polynomial, by recursion method (A function effectively defined with other base cases of the same function itself.)
Below is the Python implementation –
The value of the polynomial at given point is: 305.0
We can now also plot the Legendre polynomials (say from 1st order to 4th order) using matplotlib.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Python Program for Legendre\'s Conjecture
- Python | Handling recursion limit
- Python | All Permutations of a string in lexicographical order without using recursion
- Generating subarrays using recursion
- Generating all possible Subsequences using Recursion
- Python - Read blob object in python using wand library
- Python | Index of Non-Zero elements in Python list
- Python | PRAW - Python Reddit API Wrapper
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- twitter-text-python (ttp) module - Python
- MySQL-Connector-Python module in Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.