# How to Plot this Riemann Sum Correctly?

**URL:** <https://discourse.julialang.org/t/how-to-plot-this-riemann-sum-correctly/90944>\
**Category:** General Usage\
**Tags:** question, plotting\
**Created:** [November 28, 2022, 10:52am UTC](https://discourse.julialang.org/t/how-to-plot-this-riemann-sum-correctly/90944 "2022-11-28T10:52:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [November 28, 2022, 10:52am UTC](https://discourse.julialang.org/t/how-to-plot-this-riemann-sum-correctly/90944/1 "2022-11-28T10:52:16Z")

</div>

Hi all,

I have a function : x^{3} - 5x^{2} + 2x + 8

![Capture d’écran_2022-11-28_17-50-09](https://global.discourse-cdn.com/julialang/original/3X/0/1/01e049f5774c09c327fcde0b97be4fcb135144e6.png)

and I have this code:

```julia
using Plots, LaTeXStrings, ValidatedNumerics
gr()

f(x) = x^3 - 5x^2 + 2x + 8

#plot(f, 0, 2, aspect_ratio=:equal, fill=(0, :green), alpha=0.2, label="")

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(aspect_ratio=:equal)
for X in intervals
    Y = f(X)
    plot!(IntervalBox(X, Interval(0, Y.lo)), c=:blue, label="", alpha=0.1)
    plot!(IntervalBox(X, Interval(Y.lo, Y.hi)), c=:red, label="", alpha=0.1)
end

plot!(f, 0, 5, xlims=(0, 5), ylims=(-1,18), label=L"x^{3} - 5x^{2} + 2x + 8")

```

but the plot is far from the image I want to achieve. Which lines should I modify?

![Capture d’écran_2022-11-28_17-51-32](https://global.discourse-cdn.com/julialang/original/3X/a/b/ab837189bb11581dbab9868dc62a16ad6b9c0293.png)

---

<div class="post-metadata">

**Author:** ![zamk](https://avatars.discourse-cdn.com/v4/letter/z/ecd19e/32.png) [@zamk](https://discourse.julialang.org/u/zamk)\
**Post date:** [November 28, 2022, 2:42pm UTC](https://discourse.julialang.org/t/how-to-plot-this-riemann-sum-correctly/90944/2 "2022-11-28T14:42:52Z")

</div>

If I modify your code as such:

```julia

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](https://global.discourse-cdn.com/julialang/original/3X/5/5/55b5558e048a0b64fa0f79bcecb91e51fe7237fb.png)

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.

---

<div class="post-metadata">

**Author:** ![Freya\_the\_Goddess](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/freya_the_goddess/32/36835_2.png) [@Freya\_the\_Goddess](https://discourse.julialang.org/u/Freya_the_Goddess)\
**Post date:** [December 3, 2022, 8:48am UTC](https://discourse.julialang.org/t/how-to-plot-this-riemann-sum-correctly/90944/3 "2022-12-03T08:48:14Z")

</div>

It is great, what if I want to create the partition points on the interval [0,5] for x at 0 \< 1.1 \< 2 \< 3.2 \< 4 \< 5 ? can I change the red line or adjust it to hit the right points?

```julia
using Plots, LaTeXStrings, ValidatedNumerics, Plots.PlotMeasures
gr()

f(x) = x^3 - 5x^2 + 2x + 8
	
function make_intervals(N=5)
    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(5)
p = plot()
for X in intervals
    Y = f.(X)
	box = IntervalBox([X[1],0],[X[2],(Y[1]+Y[2])/1.5],)
	plot!(box,c=:blue,legend=nothing,alpha=.3)
end
plot!(f, 0, 5, xlims=(0, 5), ylims=(-6,18), 
	label="",
	c=:red)

plot!([0.5,0.5],[0,f(0.5)], label="", linecolor=:green, linestyle=:dot)
plot!([1.5,1.5],[0,f(1.5)], label="", linecolor=:green, linestyle=:dot)
plot!([2.5,2.5],[0,f(2.5)], label="", linecolor=:green, linestyle=:dot)
plot!([3.6,3.6],[0,f(3.6)], label="", linecolor=:green, linestyle=:dot)

```
