Learn R Programming

vectorialcalculus (version 1.0.5)

riemann_sum_1d_plot: 1D Riemann sums with optional plot

Description

Computes lower, upper, and midpoint Riemann sums for a scalar function f(x) on an interval [xmin, xmax]. Optionally draws a 2D plot with rectangles and, if requested, the true curve.

Usage

riemann_sum_1d_plot(
  f,
  xlim,
  n = 12L,
  methods = c("lower", "upper", "mid"),
  show_curve = TRUE,
  curve_res = 400L,
  colors = list(lower = "#a1d99b", upper = "#fc9272", mid = "#9ecae1"),
  alpha = 0.8,
  edge_color = "black",
  edge_width = 1.2,
  curve_color = "black",
  curve_width = 2,
  show_baseline = TRUE,
  baseline = 0,
  baseline_color = "gray50",
  baseline_width = 1,
  warn_heavy = TRUE
)

Value

A list with components:

  • lower_sum Lower Riemann sum.

  • upper_sum Upper Riemann sum.

  • mid_sum Midpoint Riemann sum.

  • dx Subinterval width.

  • x_breaks Partition points.

  • figure A plotly object, or NULL if not available.

Arguments

f

Function function(x) returning numeric values.

xlim

Numeric vector c(xmin, xmax) with xmax > xmin.

n

Integer. Number of subintervals.

methods

Character vector with any of "lower", "upper", "mid" indicating which rectangle types to draw.

show_curve

Logical. If TRUE, overlays the curve f(x).

curve_res

Integer. Number of points used to draw the curve.

colors

Named list specifying fill colors for list(lower=..., upper=..., mid=...).

alpha

Numeric in [0,1]. Fill opacity for rectangles.

edge_color

Color for rectangle borders.

edge_width

Border width.

curve_color

Color for the curve.

curve_width

Line width for the curve.

show_baseline

Logical. If TRUE, draws a horizontal baseline.

baseline

Numeric. Y-value for the baseline.

baseline_color

Baseline color.

baseline_width

Baseline width.

warn_heavy

Logical. If TRUE, warns when n is very large.

Examples

Run this code
f <- function(x) sin(2*x)
out <- riemann_sum_1d_plot(
  f, xlim = c(0, pi), n = 10,
  methods = c("lower","upper","mid"),
  show_curve = TRUE
)
out$mid_sum

Run the code above in your browser using DataLab