library(ggplot2)
f <- function(x) 0.5*x^2 + 2
A <- function(u) (3-u) * f(u)
# Kurvenpunkte für das Stück BC
df_curve <- data.frame(x = seq(0, 3, length.out = 400))
df_curve$y <- f(df_curve$x)
B <- data.frame(x = 0, y = f(0), label = "B(0|2)")
C <- data.frame(x = 3, y = f(3), label = "C(3|13/2)")
# Beispielwert (nur für Teil a)
u0 <- 1.2
v0 <- f(u0)
# Rechteckpunkte
Q <- data.frame(x = u0, y = v0, label = "Q(u|v)")
P <- data.frame(x = 3, y = v0, label = "P(3|v)")
R <- data.frame(x = u0, y = 0, label = "R(u|0)")
S <- data.frame(x = 3, y = 0, label = "S(3|0)")
rect_pts <- rbind(Q, P, S, R) # Reihenfolge für Polygon
ggplot(df_curve, aes(x, y)) +
geom_line(linewidth = 1.1) +
geom_point(data = rbind(B, C), aes(x, y), inherit.aes = FALSE, size = 2.5) +
geom_text(data = rbind(B, C), aes(x, y, label = label),
inherit.aes = FALSE, nudge_y = 0.25, hjust = 0) +
geom_polygon(data = rect_pts, aes(x, y), inherit.aes = FALSE,
fill = "firebrick", alpha = 0.15, color = "firebrick", linewidth = 1) +
geom_point(data = rbind(Q, P, R, S), aes(x, y),
inherit.aes = FALSE, color = "firebrick", size = 2.3) +
geom_text(data = rbind(Q, P, R, S), aes(x, y, label = label),
inherit.aes = FALSE, color = "firebrick", nudge_y = 0.25) +
coord_cartesian(xlim = c(0, 3), ylim = c(0, f(3) + 1)) +
labs(
title = "Kurvenstück BC und Beispiel-Rechteck PQRS",
x = "x", y = "y"
) +
theme_minimal(base_size = 12)

df_A <- data.frame(u = seq(0, 3, length.out = 400))
df_A$A <- A(df_A$u)
df_end <- data.frame(u = c(0, 3), A = c(A(0), A(3)), label = c("A(0)=6", "A(3)=0"))
ggplot(df_A, aes(u, A)) +
geom_line(linewidth = 1.1) +
geom_point(data = df_end, aes(u, A), inherit.aes = FALSE, size = 2.5) +
geom_text(data = df_end, aes(u, A, label = label),
inherit.aes = FALSE, nudge_y = 0.25) +
coord_cartesian(xlim = c(0, 3)) +
labs(
title = "Flächeninhalt A(u) des Rechtecks",
x = "u", y = "A(u)"
) +
theme_minimal(base_size = 12)

u_max <- 0
v_max <- f(u_max)
Qm <- data.frame(x = u_max, y = v_max, label = "Qmax=B(0|2)")
Pm <- data.frame(x = 3, y = v_max, label = "P(3|2)")
Rm <- data.frame(x = u_max, y = 0, label = "R(0|0)")
Sm <- data.frame(x = 3, y = 0, label = "S(3|0)")
rect_max <- rbind(Qm, Pm, Sm, Rm)
ggplot(df_curve, aes(x, y)) +
geom_line(linewidth = 1.1) +
geom_polygon(data = rect_max, aes(x, y), inherit.aes = FALSE,
fill = "steelblue", alpha = 0.18, color = "steelblue", linewidth = 1) +
geom_point(data = rbind(Qm, Pm, Rm, Sm), aes(x, y),
inherit.aes = FALSE, color = "steelblue", size = 2.3) +
geom_text(data = rbind(Qm, Pm, Rm, Sm), aes(x, y, label = label),
inherit.aes = FALSE, color = "steelblue", nudge_y = 0.25) +
coord_cartesian(xlim = c(0, 3), ylim = c(0, f(3) + 1)) +
labs(
title = "Maximales Rechteck PQRS (bei u=0)",
x = "x", y = "y"
) +
theme_minimal(base_size = 12)
