-
Notifications
You must be signed in to change notification settings - Fork 0
/
LegendrePoly.py
64 lines (53 loc) · 1.91 KB
/
LegendrePoly.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import numpy as np
import sympy as sp
import matplotlib.pyplot as plt
from scipy.special import legendre
from sympy.interactive import printing
printing.init_printing(use_latex=True)
xvals = np.linspace(-1, 1, 1000)
# First 5 Legendre Polynomials as a function of x
plt.title(r"First 5 Legendre Polynomials - These are solutions to $(1-x^2)\frac{d^2y}{dx^2} -2x\frac{dy}{dx} + n(n+1)y = 0$")
plt.xlabel(sp.Symbol(r'$x$'))
plt.ylabel(sp.Symbol(r'$y$'))
plt.xlim(-1, 1)
for i in range(1, 6):
yvals = legendre(i)(xvals)
plt.plot(xvals, yvals, label=fr"$P_{i}(x)$")
plt.legend()
plt.grid()
plt.show()
#Legendre Polynomials as a function of trig & hyperbolic functions
fig, ax = plt.subplots(3,2)
fig.suptitle(r"First 5 Legendre Polynomials - These are solutions to $(1-x^2)\frac{d^2y}{dx^2} -2x\frac{dy}{dx} + n(n+1)y = 0$")
ax[0,0].set(xlabel=r"$\sin(x)$", ylabel=r"$y$")
ax[1,0].set(xlabel=r"$\cos(x)$", ylabel=r"$y$")
ax[2,0].set(xlabel=r"$\tan(x)$", ylabel=r"$y$")
ax[0,1].set(xlabel=r"$\sinh(x)$", ylabel=r"$y$")
ax[1,1].set(xlabel=r"$\cosh(x)$", ylabel=r"$y$")
ax[2,1].set(xlabel=r"$\tanh(x)$", ylabel=r"$y$")
for i in range(1, 6):
yvals = legendre(i)(np.sin(xvals))
ax[0,0].plot(xvals, yvals, label=fr"$P_{i}(x)$")
ax[0,0].grid()
for i in range(1, 6):
yvals = legendre(i)(np.cos(xvals))
ax[1,0].plot(xvals, yvals, label=r"$P_{i}(x)$")
ax[1,0].grid()
for i in range(1, 6):
yvals = legendre(i)(np.tan(xvals))
ax[2,0].plot(xvals, yvals, label=r"$P_{i}(x)$")
ax[2,0].grid()
for i in range(1, 6):
yvals = legendre(i)(np.sinh(xvals))
ax[0,1].plot(xvals, yvals, label=fr"$P_{i}(x)$")
ax[0,1].grid()
for i in range(1, 6):
yvals = legendre(i)(np.cosh(xvals))
ax[1,1].plot(xvals, yvals, label=r"$P_{i}(x)$")
ax[1,1].grid()
for i in range(1, 6):
yvals = legendre(i)(np.tanh(xvals))
ax[2,1].plot(xvals, yvals, label=r"$P_{i}(x)$")
ax[2,1].grid()
fig.tight_layout()
plt.show()