Learn R Programming

vectorialcalculus (version 1.0.5)

riemann_sum_2d_plot: 2D Riemann sums (upper, lower, midpoint) with a 3D plot

Description

Visualize 2D Riemann sums for a scalar field \(f(x,y)\) over a rectangular domain \([x_{\min},x_{\max}]\times[y_{\min},y_{\max}]\). The function computes upper, lower, and midpoint sums and renders a 3D figure showing step tiles for the selected methods. Optionally overlays the true surface \(z=f(x,y)\) and a base grid on the \(xy\)-plane.

Usage

riemann_sum_2d_plot(
  f,
  xlim,
  ylim,
  nx = 8,
  ny = 8,
  methods = c("lower", "upper", "mid"),
  show_surface = TRUE,
  surface_res = c(60L, 60L),
  surface_colorscale = "Viridis",
  surface_opacity = 0.5,
  base_plane = TRUE,
  z0 = 0,
  base_opacity = 0.15,
  base_color = "lightgray",
  show_base_grid = TRUE,
  grid_color = "gray50",
  grid_width = 1,
  tile_opacity = 0.92,
  colors = list(lower = "#a1d99b", upper = "#fc9272", mid = "#9ecae1"),
  edge_color = "black",
  edge_width = 1.2,
  warn_heavy = TRUE
)

Value

A list with:

  • lower_sum, upper_sum, mid_sum: numeric estimates,

  • dx, dy: partition widths,

  • breaks: list with x_breaks, y_breaks,

  • figure: the plotly object (or NULL if plotly is not available).

Arguments

f

function(x, y) returning a numeric scalar \(f(x,y)\).

xlim, ylim

Numeric length-2 vectors c(min, max) for the domain.

nx, ny

Positive integers: number of subintervals along \(x\) and \(y\).

methods

Character vector with any of c("lower","upper","mid"). Controls which Riemann tiles are drawn. (All estimates are returned.)

show_surface

Logical; if TRUE overlays the true surface \(z=f(x,y)\).

surface_res

Integer vector c(nx_s, ny_s) for the surface mesh.

surface_colorscale

Plotly colorscale for the true surface (e.g. "Viridis").

surface_opacity

Opacity of the true surface (0–1).

base_plane

Logical; if TRUE draws a faint base plane at z0.

z0

Numeric; height of the base plane (typically 0).

base_opacity

Opacity of the base plane (0–1).

base_color

Color of the base plane.

show_base_grid

Logical; if TRUE draws partition grid lines on the base plane.

grid_color

Color of the base grid lines.

grid_width

Line width of the base grid.

tile_opacity

Opacity of Riemann tiles (0–1).

colors

Named list for tile colors (hex or rgba), e.g.: list(lower="#a1d99b", upper="#fc9272", mid="#9ecae1").

edge_color

Edge color for vertical edges of tiles.

edge_width

Line width for tile edges.

warn_heavy

Logical; if TRUE, warns when nx*ny is large.

Details

Upper/lower tiles use corner samples (minimum/maximum of the four corners), and the midpoint tiles sample at the cell center.

Examples

Run this code
if (interactive()) {
f <- function(x, y) exp(-(x^2 + y^2)) * (1 + 0.3 * cos(3*x) * sin(2*y))
out <- riemann_sum_2d_plot(
  f, xlim = c(-2, 2), ylim = c(-2, 2),
  nx = 8, ny = 7, methods = c("lower","mid","upper"),
  show_surface = TRUE, surface_res = c(80, 80),
  surface_colorscale = "YlGnBu", surface_opacity = 0.45
)
out$lower_sum; out$mid_sum; out$upper_sum
}

Run the code above in your browser using DataLab