# ==============================================================================
# Aufgabe 5: Flächeninhalt zwischen zwei Funktionen
# ==============================================================================
# Dieser Code berechnet den Inhalt der Flächenstücke, die von den Graphen 
# von f und g begrenzt werden, und visualisiert diese.

# Benötigtes Paket für die Plots laden (ggplot2 ist Standard in Tidyverse)
if(!require(ggplot2)) install.packages("ggplot2")
## Loading required package: ggplot2
library(ggplot2)

# ------------------------------------------------------------------------------
# TEIL A) f(x) = 1 - x^2  und  g(x) = x - 1
# ------------------------------------------------------------------------------

cat("\n--- Lösung Teil A ---\n")
## 
## --- Lösung Teil A ---
# 1. Funktionen definieren
f_a <- function(x) { 1 - x^2 }
g_a <- function(x) { x - 1 }

# 2. Schnittpunkte berechnen (Grenzen des Integrals)
# Ansatz: f(x) = g(x)  =>  1 - x^2 = x - 1  =>  x^2 + x - 2 = 0
# Wir lösen die quadratische Gleichung x^2 + x - 2 = 0
# Koeffizienten für p-q-Formel oder allgemeine Form: ax^2 + bx + c = 0
# Hier: 1x^2 + 1x - 2 = 0
roots_a <- polyroot(c(-2, 1, 1)) 

# Da wir reelle Grenzen brauchen, nehmen wir den Realteil (Imaginärteil ist 0)
x1_a <- Re(roots_a[1])
x2_a <- Re(roots_a[2])

# Sortieren, damit x1 immer die linke Grenze ist
grenzen_a <- sort(c(x1_a, x2_a))
x1_a <- grenzen_a[1]
x2_a <- grenzen_a[2]

cat("Schnittpunkte bei x1 =", x1_a, "und x2 =", x2_a, "\n")
## Schnittpunkte bei x1 = -2 und x2 = 1
# 3. Flächeninhalt berechnen
# Formel: Integral von x1 bis x2 über |f(x) - g(x)| dx
# In diesem Intervall liegt die Parabel (f) über der Geraden (g), also f(x) - g(x)
differenz_funktion_a <- function(x) { f_a(x) - g_a(x) }

flaeche_a <- integrate(differenz_funktion_a, lower = x1_a, upper = x2_a)

cat("Der Flächeninhalt für Teil A beträgt:", abs(flaeche_a$value), "FE (Flächeneinheiten)\n")
## Der Flächeninhalt für Teil A beträgt: 4.5 FE (Flächeneinheiten)
# 4. Plot für Teil A
x_vals_a <- seq(x1_a - 1, x2_a + 1, length.out = 100)
df_a <- data.frame(
  x = x_vals_a,
  f = f_a(x_vals_a),
  g = g_a(x_vals_a)
)

plot_a <- ggplot(df_a, aes(x = x)) +
  geom_line(aes(y = f, color = "f(x) = 1 - x²"), linewidth = 1.2) +
  geom_line(aes(y = g, color = "g(x) = x - 1"), linewidth = 1.2) +
  geom_ribbon(data = df_a, aes(ymin = g, ymax = f), fill = "blue", alpha = 0.3) +
  geom_vline(xintercept = c(x1_a, x2_a), linetype = "dashed", color = "gray") +
  labs(title = "Aufgabe 5a: Fläche zwischen f und g",
       subtitle = paste("Flächeninhalt =", round(abs(flaeche_a$value), 2), "FE"),
       x = "x", y = "y",
       color = "Legend") +
  theme_minimal()

print(plot_a)

# ------------------------------------------------------------------------------
# TEIL B) f(x) = x^2 - 2  und  g(x) = 7
# ------------------------------------------------------------------------------

cat("\n--- Lösung Teil B ---\n")
## 
## --- Lösung Teil B ---
# 1. Funktionen definieren
f_b <- function(x) { x^2 - 2 }
g_b <- function(x) { 7 }

# 2. Schnittpunkte berechnen
# Ansatz: f(x) = g(x)  =>  x^2 - 2 = 7  =>  x^2 = 9  =>  x = ±3
# Wir lösen x^2 - 9 = 0
roots_b <- polyroot(c(-9, 0, 1))

x1_b <- Re(roots_b[1])
x2_b <- Re(roots_b[2])

grenzen_b <- sort(c(x1_b, x2_b))
x1_b <- grenzen_b[1]
x2_b <- grenzen_b[2]

cat("Schnittpunkte bei x1 =", x1_b, "und x2 =", x2_b, "\n")
## Schnittpunkte bei x1 = -3 und x2 = 3
# 3. Flächeninhalt berechnen
# Hier liegt die Konstante g(x) = 7 OBERHALB der Parabel f(x).
# Also integrieren wir g(x) - f(x).
differenz_funktion_b <- function(x) { g_b(x) - f_b(x) }

flaeche_b <- integrate(differenz_funktion_b, lower = x1_b, upper = x2_b)

cat("Der Flächeninhalt für Teil B beträgt:", abs(flaeche_b$value), "FE (Flächeneinheiten)\n")
## Der Flächeninhalt für Teil B beträgt: 36 FE (Flächeneinheiten)
# 4. Plot für Teil B
x_vals_b <- seq(x1_b - 2, x2_b + 2, length.out = 100)
df_b <- data.frame(
  x = x_vals_b,
  f = f_b(x_vals_b),
  g = g_b(x_vals_b)
)

plot_b <- ggplot(df_b, aes(x = x)) +
  geom_line(aes(y = f, color = "f(x) = x² - 2"), linewidth = 1.2) +
  geom_line(aes(y = g, color = "g(x) = 7"), linewidth = 1.2) +
  geom_ribbon(data = df_b, aes(ymin = f, ymax = g), fill = "green", alpha = 0.3) +
  geom_vline(xintercept = c(x1_b, x2_b), linetype = "dashed", color = "gray") +
  labs(title = "Aufgabe 5b: Fläche zwischen f und g",
       subtitle = paste("Flächeninhalt =", round(abs(flaeche_b$value), 2), "FE"),
       x = "x", y = "y",
       color = "Legend") +
  theme_minimal()

print(plot_b)