Skip to content

Instantly share code, notes, and snippets.

@noppoMan
Last active August 4, 2020 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noppoMan/12af7afc688d1d6e070d0b2b508d8956 to your computer and use it in GitHub Desktop.
Save noppoMan/12af7afc688d1d6e070d0b2b508d8956 to your computer and use it in GitHub Desktop.
from matplotlib import pyplot as plt
import sympy as sym
from IPython.display import display, Math
x, n, m = sym.symbols('x, n, m')
expn = sym.sin(x)**2
f = lambda n: sym.expand_trig(expn)/n
F = lambda f: sym.Integral(f, (x, 0, 2))
display(Math("f_n(x) = "+ sym.latex(f(n))))
display(Math("\|f_m - f_n\| = "+ sym.latex(F(f(n) - f(m)))))
epsilons = []
distances = []
n_range = np.arange(1, 301, 2)
m_range = np.arange(2, 301, 2)
for n, m in zip(n_range, m_range):
epsilon = np.sqrt(1.189/n**2 + 1.189/m**2)
d = F(f(n) - f(m)).evalf()
epsilons.append(epsilon)
distances.append(d)
plt.figure(figsize=(12, 6))
plt.plot(n_range, distances)
plt.plot(n_range, epsilons, alpha=0.7)
plt.xlabel('n')
plt.ylabel('value')
plt.legend(['‖f_m - f_n‖', 'epsilon'], prop={'size': 15})
plt.show()
plt.figure(figsize=(12, 6))
plt.plot(n_range[100:], distances[100:])
plt.xlabel('n')
plt.ylabel('value')
plt.plot(n_range[100:], epsilons[100:], alpha=0.7)
plt.title("n, m > 200")
plt.legend(['‖f_m - f_n‖', 'epsilon'], prop={'size': 15})
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment