-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex4.py
More file actions
36 lines (31 loc) · 1.15 KB
/
Copy pathex4.py
File metadata and controls
36 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from matplotlib import pyplot
import numpy as np
import math
class Ex4:
def __init__(self):
self.message = "Exercice 4 initialized"
print(self.message)
def run(self):
print("Running Exercice 4...")
self.hermite(4, 2)
x_values = np.linspace(-2, 2, 100)
y_valuesH0 = [self.hermite(0, x) for x in x_values]
y_valuesH1 = [self.hermite(1, x) for x in x_values]
y_valuesH2 = [self.hermite(2, x) for x in x_values]
y_valuesH3 = [self.hermite(3, x) for x in x_values]
pyplot.plot(x_values, y_valuesH0, 'r')
pyplot.plot(x_values, y_valuesH1, 'b')
pyplot.plot(x_values, y_valuesH2, 'g')
pyplot.plot(x_values, y_valuesH3, 'y')
pyplot.title("Hermite Polynomials")
pyplot.xlabel("x")
pyplot.ylabel("H_n(x)")
pyplot.legend(["H0", "H1", "H2", "H3"])
pyplot.grid()
pyplot.show()
def hermite(self, n, x):
H = 0
for k in range(int(np.floor(n/2) + 1)):
term = (-1)**k * math.factorial(n) / ( 2**k * math.factorial(k) * math.factorial(n - 2*k)) * x ** (n - 2*k)
H += term
return H