# Definition von f'
f_prime <- function(x) {
-0.2 * (x + 1) * (x - 1.5) * (x - 3)
}
# Numerische Ableitungen mittels zentraler Differenzen
central_diff <- function(f, x, h = 1e-5) {
(f(x + h) - f(x - h)) / (2 * h)
}
# x-Werte im Intervall [-1, 3.5]
x_vals <- seq(-1, 3.5, length.out = 500)
# Werte von f', f'' und f'''
f1_values <- f_prime(x_vals)
f2_func <- function(x) central_diff(f_prime, x)
f2_values <- sapply(x_vals, f2_func)
f3_func <- function(x) central_diff(f2_func, x)
f3_values <- sapply(x_vals, f3_func)
# Daten in Long-Format für ggplot umwandeln
df <- data.frame(
x = x_vals,
f_prime = f1_values,
f_double_prime = f2_values,
f_triple_prime = f3_values
) %>%
pivot_longer(
cols = starts_with("f_"),
names_to = "Funktion",
values_to = "Wert"
)
# Faktor mit Ausdrucks-Labels für die Legende
df$Funktion <- factor(df$Funktion,
levels = c("f_prime", "f_double_prime", "f_triple_prime"),
labels = c(expression(f*"'"), expression(f*{"''"}), expression(f*{"'''"}))
)
# Plot der Funktionen
ggplot(df, aes(x = x, y = Wert, color = Funktion)) +
geom_line(size = 1) +
geom_hline(yintercept = 0, linetype = "dashed", color = "gray40") +
scale_color_manual(
values = c("blue", "red", "darkgreen"),
name = "Funktion"
) +
theme_minimal() +
labs(
title = "Graphen von f', f'' und f''' im Intervall [-1, 3.5]",
x = "x",
y = "Funktionswert"
) +
theme(
text = element_text(size = 14),
legend.title = element_text(size = 14),
legend.text = element_text(size = 13)
)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
