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

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

Capture d’écran_2022-12-17_16-35-13

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

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

Instead of plotting the interval boxes:

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:

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
1 Like

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?

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

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

1 Like

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

here is the new code:

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.

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

1 Like

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