When using this bit of code to calculate the upper and lower sum of a function (using the Archimedes strip method) and the plot doesn't end on y = 0 but "in the air", an access rectangle at the end appears, what would be a way to remove this and make it look cleaner? Please excuse me if I made some stupid mistake, I am kinda new to this stuff :) (took me a way too long time to realize the artefacts I was experiencing could be fixed with a simple +1
x = np.linspace(a, b, N+1)
Anyways, here is the code:
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return 0.5*x*x
a = 0 #lower bound
b = np.pi #upper bound
N = 30 #number of rectangles
#width of each rectangle
width = (b-a)/N
#x-coordinates of the left edge of each rectangle
x = np.linspace(a, b, N+1)
#y-coordinates of the bottom edge of each rectangle
y = f(x)
#height of each rectangle
height = y
#calculate the area using the formula: area = width * height
area = width * height
#calculate the lower sum
lower_sum = np.sum(area)
#x-coordinates of the midpoint of each rectangle
x_mid = x + width/2
#y-coordinates of the midpoint of each rectangle
y_mid = f(x_mid)
#height of each rectangle
mid_height = y_mid
#calculate the area using the formula: area = width * height
mid_area = width * mid_height
#calculate the upper sum
upper_sum = np.sum(mid_area)
print("The lower sum is:", lower_sum)
print("The upper sum is:", upper_sum)
lower_bar = x - width * 0.5
upper_bar = x + width * 0.5
#plot the graph of the function
plt.plot(x, y, 'b-')
plt.bar(lower_bar, y, width = width, alpha = 0.5, color = 'blue')
plt.bar(upper_bar, y, width = width, alpha = 0.5, color = 'red')
plt.savefig('plot.pdf')
Thank you in advance :D
