f_a <- function(x) { (1/3)*x^3 + x^2 }

# Daten fĂĽr den Plot
x_vals <- seq(-4.5, 0.5, length.out = 200)
df_a <- data.frame(x = x_vals, y = f_a(x_vals))

# Flächen-Datenframes für ggplot (Polygone)
# Fläche 1: -4 bis -3
poly1 <- data.frame(x = c(-4, seq(-4, -3, length.out=50), -3), 
                    y = c(0, f_a(seq(-4, -3, length.out=50)), 0))
# Fläche 2: -3 bis 0
poly2 <- data.frame(x = c(-3, seq(-3, 0, length.out=50), 0), 
                    y = c(0, f_a(seq(-3, 0, length.out=50)), 0))

ggplot(df_a, aes(x = x, y = y)) +
  geom_line(color = "darkblue", linewidth = 1) +
  geom_polygon(data = poly1, aes(x = x, y = y), fill = "red", alpha = 0.3) +
  geom_polygon(data = poly2, aes(x = x, y = y), fill = "blue", alpha = 0.3) +
  geom_vline(xintercept = -4, linetype = "dashed", color = "gray40") +
  geom_vline(xintercept = -3, linetype = "dotted", color = "black") +
  geom_vline(xintercept = 0, linetype = "dotted", color = "black") +
  geom_hline(yintercept = 0, color = "black") +
  labs(title = TeX("f(x) = 1/3 x^3 + x^2"),
       subtitle = "Rote Fläche (links) = Blaue Fläche (rechts) = 2.25 FE",
       x = "x", y = "f(x)") +
  theme_minimal()

f_b <- function(x) { exp(x) - exp(-x) }

x_vals <- seq(-1.2, 1.2, length.out = 200)
df_b <- data.frame(x = x_vals, y = f_b(x_vals))

# Flächen
poly1_b <- data.frame(x = c(-1, seq(-1, 0, length.out=50), 0), 
                      y = c(0, f_b(seq(-1, 0, length.out=50)), 0))
poly2_b <- data.frame(x = c(0, seq(0, 1, length.out=50), 1), 
                      y = c(0, f_b(seq(0, 1, length.out=50)), 0))

ggplot(df_b, aes(x = x, y = y)) +
  geom_line(color = "darkgreen", linewidth = 1) +
  geom_polygon(data = poly1_b, aes(x = x, y = y), fill = "red", alpha = 0.3) +
  geom_polygon(data = poly2_b, aes(x = x, y = y), fill = "blue", alpha = 0.3) +
  geom_vline(xintercept = c(-1, 0, 1), linetype = "dashed", color = "gray50") +
  geom_hline(yintercept = 0, color = "black") +
  labs(title = TeX("f(x) = e^x - e^{-x}"),
       subtitle = "Punktsymmetrie: Flächen links und rechts sind gleich groß (~1.09 FE)",
       x = "x", y = "f(x)") +
  theme_minimal()

f_c <- function(x) { 0.5 * sin((pi/2) * x) }

x_vals <- seq(2.8, 5.2, length.out = 200)
df_c <- data.frame(x = x_vals, y = f_c(x_vals))

# Flächen
poly1_c <- data.frame(x = c(3, seq(3, 4, length.out=50), 4), 
                      y = c(0, f_c(seq(3, 4, length.out=50)), 0))
poly2_c <- data.frame(x = c(4, seq(4, 5, length.out=50), 5), 
                      y = c(0, f_c(seq(4, 5, length.out=50)), 0))

ggplot(df_c, aes(x = x, y = y)) +
  geom_line(color = "purple", linewidth = 1) +
  geom_polygon(data = poly1_c, aes(x = x, y = y), fill = "red", alpha = 0.3) +
  geom_polygon(data = poly2_c, aes(x = x, y = y), fill = "blue", alpha = 0.3) +
  geom_vline(xintercept = c(3, 4, 5), linetype = "dashed", color = "gray50") +
  geom_hline(yintercept = 0, color = "black") +
  labs(title = TeX("f(x) = 1/2 sin(pi/2 * x)"),
       subtitle = "Periodizität: Flächen zwischen den Nullstellen sind gleich (1/pi FE)",
       x = "x", y = "f(x)") +
  theme_minimal()