Skip to content

Instantly share code, notes, and snippets.

@marshareb
Created March 29, 2022 14:13
Show Gist options
  • Save marshareb/b63a82956f71c907c8b128f6c74c9842 to your computer and use it in GitHub Desktop.
Save marshareb/b63a82956f71c907c8b128f6c74c9842 to your computer and use it in GitHub Desktop.
animation of left Riemann sums
import matplotlib.pyplot as plt
import numpy
import matplotlib.animation as pltani
# GENERAL FORMAT
# Here, f stands for function, a stands for the beginning of the interval, b stands for the end, and n stands for the number of subdivisions you want to do.
# LEFT RIEMANN PLOT
def plotleftriemannsum(f, a, b, n):
# plot the function
x = numpy.linspace(a, b)
plt.plot(x, f(x).astype(numpy.float))
# plot the boxes
delta = (b - a) / n
x = numpy.arange(a, b, delta)
y = f(x)
plt.bar(x, y, width=delta, alpha=0.5, facecolor='orange')
# show
plt.show()
# RIGHT RIEMANN PLOT
def plotrightriemannsum(f, a, b, n):
# plot the function
x = numpy.linspace(a, b)
plt.plot(x, f(x).astype(numpy.float))
# plot the boxes
delta = (b - a) / n
x = numpy.arange(a + delta, b + delta, delta)
y = f(x)
x = numpy.arange(a, b, delta)
plt.bar(x, y, width=delta, alpha=0.5, facecolor='red')
# show
plt.show()
def riemannsum(f, a, b, n):
# LEFT RIEMANN
def leftriemannsum(f, a, b, n):
return sum([f(a + ((b - a) / n) * i) * ((b - a) / n) for i in range(n)])
# RIGHT RIEMANN
def rightriemannsum(f, a, b, n):
return sum([f(a + ((b - a) / n) * i) * ((b - a) / n) for i in range(1, n + 1)])
def midriemannsum(f,a,b,n):
return sum([f((a + ((b - a) / n) * i + (a + ((b-a)/n) * (i-1)))/2) * ((b - a) / n) for i in range(1, n + 1)])
# BOTH
def plotboth(f, a, b, n):
# plot the function
x = numpy.linspace(a, b)
plt.plot(x, f(x).astype(numpy.float))
# plot the left riemann sum
delta = (b - a) / n
x = numpy.arange(a, b, delta)
y = f(x)
plt.bar(x, y, width=delta, alpha=0.5, align='edge', facecolor='red')
# plot the right riemann sum
x1 = numpy.arange(a + delta, b + delta, delta)
y = f(x1)
plt.bar(x, y, width=delta, alpha=0.5, align='edge', facecolor='orange')
# show
# plot the mid riemann sum
x2 = (x + x1)/2
y = f(x2)
plt.bar(x, y, width=delta, alpha=0.5, align='edge', facecolor='blue')
plt.show()
print('LEFT RIEMANN SUM:')
print(leftriemannsum(f, a, b, n))
print('RIGHT RIEMANN SUM:')
print(rightriemannsum(f, a, b, n))
print('MID RIEMANN SUM:')
print(midriemannsum(f,a,b,n))
plotboth(f, a, b, n)
def f(x):
return x**2
# leftpoint animation
a = 0
b = 1
n = 4
fig, ax = plt.subplots()
xdata = numpy.arange(a, b, 1 / n)
ydata = f(xdata)
x = numpy.linspace(a, b)
ax.set_xlim(0, 1)
ax.set_ylim(-1, 1)
plt.plot(x, f(x).astype(numpy.float))
def animate(n):
plt.clf()
x = numpy.linspace(a, b)
plt.plot(x, f(x).astype(numpy.float))
n+=1
delta = (b - a) / n
x = numpy.arange(a, b, delta)
y = f(x)
plt.bar(x, y, width=delta, alpha=0.5, align='edge', facecolor='red')
ani = pltani.FuncAnimation(fig, animate, frames = 100, interval=100, repeat=True)
# save animation
# ani.save(PATH,writer='imagemagick', fps=10)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment