Skip to content

Instantly share code, notes, and snippets.

@miku
Last active September 21, 2016 13:38
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 miku/d83be6ec61d05f1fa4ed5b70cf5b59b9 to your computer and use it in GitHub Desktop.
Save miku/d83be6ec61d05f1fa4ed5b70cf5b59b9 to your computer and use it in GitHub Desktop.
Power series expansion of e, gifified.

Q

One way to express $e$ is via a power series:

$$e = \sum_{n=0}^{\infty} \frac{x^n}{n!}$$

If we plot the first k terms of this series for increasing x we observe a normal distribution of the terms.

#!/usr/bin/env python
# coding: utf-8
from __future__ import division
from pandas import Series
from math import factorial
import matplotlib.pyplot as plt
import seaborn as sns
def a_x(x, n):
return x**n / factorial(n)
def plot_exp_x_up_to_n(x, n=10):
s = Series((a_x(x, i) for i in range(n)))
with sns.color_palette("Greys_r"):
s.plot(kind='bar')
plt.title('terms of e(x) series for x = %d' % x)
plt.minorticks_off()
plt.xlabel('term n')
plt.savefig('x_%04d_n_%04d.png' % (x, n))
if __name__ == '__main__':
for i in range(0, 40):
plot_exp_x_up_to_n(i, 40)
SHELL = /bin/bash
images:
python expansion.py
gifs:
for fn in *png; do convert "$$fn" "$${fn%.png}.gif"; done
anim.gif:
gifsicle --colors 16 --delay=35 --loop x_*.gif > anim.gif
clean:
rm -f x_*_n_*.png
rm -f x_*_n_*.gif
rm -f anim.gif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment