# How to Plot Trapezoidal and Simpson's rule plot with given curve or function?

**URL:** <https://discourse.julialang.org/t/how-to-plot-trapezoidal-and-simpsons-rule-plot-with-given-curve-or-function/91767>\
**Category:** General Usage\
**Tags:** plotting, plots\
**Created:** [December 17, 2022, 9:43am UTC](https://discourse.julialang.org/t/how-to-plot-trapezoidal-and-simpsons-rule-plot-with-given-curve-or-function/91767 "2022-12-17T09:43:56Z")\
**Posts on this page:** 7\
**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:** [December 17, 2022, 9:43am UTC](https://discourse.julialang.org/t/how-to-plot-trapezoidal-and-simpsons-rule-plot-with-given-curve-or-function/91767/1 "2022-12-17T09:43:56Z")

</div>

Hi all,

I want to know how to plot the partitions and the trapezoids when I have the input of the function only and the number of interval? This is the images that I want to achieve

![Capture d’écran_2022-12-17_16-34-52](https://global.discourse-cdn.com/julialang/original/3X/6/3/6311706f7cdbd5079e6f20bf00de26ddd8255d93.png)

![Capture d’écran_2022-12-17_16-35-13](https://global.discourse-cdn.com/julialang/original/3X/a/f/aff21cff354e7f285fa1ab5c55007315d3c5e5f5.png)

I have this code for starter, but have no idea to convert it to Trapezoidal rule and to Simpson’s rule:

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

f(x) = 3x^2 + x + 1

function make_intervals(N=10)
    xs = range(-1, stop=1, length=N+1)
    return [xs[i]..xs[i+1] for i in 1:length(xs)-1]
end

# Plot Riemann Sums
intervals = make_intervals(10)
 
p = plot(aspect_ratio=:equal)
for X in intervals
    Y = f(X)

    plot!(IntervalBox(X, Interval(0, Y.hi)), c=:blue, label="", alpha=0.1)
end

plot!(f, -1, 1, xtick=-1:1:1, xlims=(-1,1),label=L"3x^2 + x + 1",
	bottom_margin=5mm)

annotate!([(-0.9,-0.07, (L"x_{0}", 6, :black))])
annotate!([(-0.7,-0.07, (L"x_{1}", 6, :black))])
annotate!([(-0.5,-0.07, (L"x_{2}", 6, :black))])
annotate!([(0,-0.07, (L"\dots", 6, :black))])
annotate!([(0.77,-0.07, (L"x_{n-1}", 6, :black))])
annotate!([(1.07,-0.07, (L"x_{n}", 6, :black))])

```

![Capture d’écran_2022-12-17_16-43-29](https://global.discourse-cdn.com/julialang/original/3X/1/d/1d3f16898fe64cacbd8e1b151fa3e81e129a7cf3.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 17, 2022, 11:41pm UTC](https://discourse.julialang.org/t/how-to-plot-trapezoidal-and-simpsons-rule-plot-with-given-curve-or-function/91767/2 "2022-12-17T23:41:51Z")

</div>

Instead of plotting the interval boxes:

```julia
for X in intervals
    Y = f(X)
    plot!(IntervalBox(X, Interval(0, Y.hi)), c=:blue, label="", alpha=0.1)
end

```

try plotting polygon trapezoidal shapes:

```julia
for X in intervals
    x = [X.lo, X.hi, X.hi, X.lo]
    y = [0, 0, f(X.hi), f(X.lo)]
    plot!(Shape(x, y), c=:blue, label="", alpha=0.1)
end

```

---

<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 18, 2022, 9:35am UTC](https://discourse.julialang.org/t/how-to-plot-trapezoidal-and-simpsons-rule-plot-with-given-curve-or-function/91767/3 "2022-12-18T09:35:22Z")

</div>

I see the trapezoidal shapes works, but the dimension of the plot is weird, it is very small, how can I get the normal resolution?

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

f(x) = x^2 - log(2x) + 8

function make_intervals(N=8)
    xs = range(0, stop=2, length=N+1)
    return [xs[i]..xs[i+1] for i in 1:length(xs)-1]
end

# Plot Riemann Sums
intervals = make_intervals(8)
 
p = plot(aspect_ratio=:equal)
for X in intervals
    x = [X.lo, X.hi, X.hi, X.lo]
    y = [0, 0, f(X.hi), f(X.lo)]
    plot!(Shape(x, y), c=:blue, label="", alpha=0.1)
end

plot!(f, xlims=(0,5),
	size=(720, 480), framestyle=:zerolines, label=L"3x^2 + x + 1",
	bottom_margin=5mm)

```

![Capture d’écran_2022-12-18_16-35-35](https://global.discourse-cdn.com/julialang/original/3X/0/d/0deb215a6429162895808f202d930a3d0b457766.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 18, 2022, 9:54am UTC](https://discourse.julialang.org/t/how-to-plot-trapezoidal-and-simpsons-rule-plot-with-given-curve-or-function/91767/4 "2022-12-18T09:54:09Z")

</div>

This is a tricky matter.  
As you have set `aspect_ratio=:equal`, your plot, as is, will be very tall.  
Some tips:

- use `size=(400,1200)` to make the plot window better fit the plot area.
- use `ylims=(0,20)` to make plot less tall
- change the font size for better readability
- use `dpi=600` to save high-resolution pictures.

**NB:**  
_you have a singularity at_ `x=0`

---

<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 18, 2022, 12:08pm UTC](https://discourse.julialang.org/t/how-to-plot-trapezoidal-and-simpsons-rule-plot-with-given-curve-or-function/91767/5 "2022-12-18T12:08:14Z")

</div>

Yes, because of the log function I have singularity at x=0

here is the new code:

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

f(x) = sin(x^2) - cos(2x) + 8

function make_intervals(N=8)
    xs = range(0, stop=4, length=N+1)
    return [xs[i]..xs[i+1] for i in 1:length(xs)-1]
end

# Plot Riemann Sums
intervals = make_intervals(8)
 
p = plot()
for X in intervals
    x = [X.lo, X.hi, X.hi, X.lo]
    y = [0, 0, f(X.hi), f(X.lo)]
    plot!(Shape(x, y), c=:blue, label="", alpha=0.1)
end

plot!(f, xlims=(0,5), ylims=(0,11), dpi=600,
	size=(360,400), framestyle=:zerolines, label=L"\sin(x^{2})\ - \cos(2x) + 8",
	bottom_margin=5mm)

```

my question is:

why the left side of my plot has a big empty spot, the picture should be located centered, instead of going to the right.

 ![Capture d’écran_2022-12-18_19-07-58](https://global.discourse-cdn.com/julialang/original/3X/b/b/bbe1d4474a8cb7335bd5a2cca09c5890e4bfa68a.png)

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 18, 2022, 12:11pm UTC](https://discourse.julialang.org/t/how-to-plot-trapezoidal-and-simpsons-rule-plot-with-given-curve-or-function/91767/6 "2022-12-18T12:11:25Z")

</div>

It does display centered when I run your last code. Try killing the plot window and running the code again.

---

<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 18, 2022, 12:30pm UTC](https://discourse.julialang.org/t/how-to-plot-trapezoidal-and-simpsons-rule-plot-with-given-curve-or-function/91767/7 "2022-12-18T12:30:36Z")

</div>

Yes killing the plot window and run the code again works. Thanks!
