# Create Numpy Array With Random Values ​​| python

Learn examples from DataCamp guru:

Here we use the Bonnet recurrence relation of legendary polynomials, i.e. —

This can be implemented using Python, 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 evaluation point. Basic cases: if n is 0, then the value of the polynomial is always 1, and this is x when the order is 1. These are the initial values ​​needed for the repetition relation.
For other values ​​of n, the function is defined recursively, directly from the Bonnet recursion. Thus, P (n, x) recursively returns the Legendre polynomial values ​​(a function effectively defined with other base cases of the same function.)

Below is a Python implementation —

 ` # Legendre polynomial ` ` def ` ` P ( n, x): ` ` if ` ` (n ` ` = ` ` = ` ` 0 ` `): ` ` ` ` return ` ` 1 ` ` # P0 = 1 ` ` elif ` ` (n ` ` = ` ` = ` ` 1 ` `): ` ` return ` ` x ` ` # P1 = x ` ` ` ` else ` `: ` ` ` ` return ` ` (((` ` 2 ` ` * ` ` n) ` ` - ` ` 1 ` `) ` ` * ` ` x ` ` * ` ` P (n ` ` - ` ` 1 ` `, x ) ` ` - ` ` (n ` ` - ` ` 1 ` `) ` ` * ` ` P (n < / code> - 2 , x)) / float (n) ````    # Suppose we want to find a value # 3rd order: legend polynomial for x = 5 # We can display the value as:   # Driver program n = 3 X = 5 print ( " The value of the polynomial at given point is: " , P (n, X)) ```

Output:

` The value of the polynomial at given point is: 305.0 `

Now we can also plot Legendre polynomials (say 1st to 4th order) using matplotlib.

` `

``` import matplotlib   # This is for use in a web browser, can be ignored. matplotlib.use ( `Agg` )     import matplotlib.pyplot as plt import numpy as np   # Create array of x values ​​ x = np.linspace ( - 1 , 1 , 200 )    # for which polynomials are evaluated and plotted for i in range ( 1 , 5 ):   # Marking as ordered plt.plot (x, P (i, x), label = “P” + str (i ))    plt.legend (loc = "best" ) plt.xlabel ( "X" ) plt.ylabel ( "Pn" ) plt.show () ```

Learn examples from DataCamp guru: