How to Plot this Riemann Sum Correctly?

If I modify your code as such:


f(x) = x^3 - 5x^2 + 2x + 8
	
function make_intervals(N=6)
    xs = range(0, stop=5, length=N+1)
    return [[xs[i],xs[i+1]] for i in 1:length(xs)-1]
end
	
# Plot Riemann Sums
intervals = make_intervals(6)
p = plot()
for X in intervals
    Y = f.(X)
	box = IntervalBox([X[1],0],[X[2],(Y[1]+Y[2])/2],)
	plot!(box,c=:blue,legend=nothing,alpha=.3)
end
plot!(f, 0, 5,c=:red)

I get the following output:
image

You will see that I take the mean of the function values for each interval, you can change that in the way that you find suitable.

3 Likes