from __future__ import division
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as pt
Mini-Introduction to sympy
import sympy as sym
# Enable "pretty-printing" in IPython
sym.init_printing()
x = sym.Symbol("x")
myexpr = (x**2-3)**2
myexpr
myexpr.expand()
sym.integrate(myexpr, x)
sym.integrate(myexpr, (x, -1, 1))
Orthogonal polynomials
def inner_product(f1, f2):
return sym.integrate(f1*f2, (x, -1, 1))
inner_product(1, 1)
inner_product(1, x)
#basis = [1, x, x**2, x**3]
basis = [1, x, x**2, x**3, x**4, x**5]
# Now for plain old Gram-Schmidt
orth_basis = []
for q in basis:
for prev_q in orth_basis:
q = q - inner_product(prev_q, q)*prev_q
q = q / sym.sqrt(inner_product(q, q))
orth_basis.append(q)
orth_basis
(Note: normalization different than in the book.)
mesh = np.linspace(-1, 1, 100)
pt.figure(figsize=(8,8))
for f in orth_basis:
f = sym.lambdify(x, f)
pt.plot(mesh, [f(xi) for xi in mesh])