---
title: "The Derivative as the Slope of the Tangent"
author: "Quantitative Ecology"
output: html_document
---

# The concept of the derivative

The derivative of a function at a point expresses the slope of the tangent line at that particular point.

For the function:

\[
y = x^2
\]

the derivative is:

\[
\frac{dy}{dx} = 2x
\]

At the point \(x=20\):

\[
f'(20)=2 \times 20 = 40
\]

So the tangent has a slope of 40.

---

# R Code

```{r, echo=TRUE, fig.width=9, fig.height=7}

# ===============================
# Function y = x^2
# ===============================

# x values
x <- seq(0, 40, length = 500)

# Function
y <- x^2

# ===============================
# Point of contact
# ===============================

x0 <- 20
y0 <- x0^2

# Derivative of x^2 -> 2x
slope <- 2 * x0

# ===============================
# Tangent equation
# y = y0 + slope*(x-x0)
# ===============================

tangent <- y0 + slope * (x - x0)

# ===============================
# Plot
# ===============================

plot(x, y,
     type = "l",
     lwd = 3,
     col = "blue",
     xlab = "x",
     ylab = "y",
     main = expression(paste(
       "Derivative as slope of the tangent at ", y==x^2)),
     ylim = c(0, max(y)))

# x and y axes inside the plot
abline(h = 0, col = "green", lwd = 1.5)
abline(v = 0, col = "green", lwd = 1.5)

# Tangent
lines(x, tangent,
      col = "red",
      lwd = 3,
      lty = 2)

# Point of contact
points(x0, y0,
       pch = 19,
       cex = 1.5)

# Point label
text(x0 - 1, y0 - 80,
     labels = "(20, 400)",
     pos = 4)

# ===============================
# Drawing the angle theta
# ===============================

theta <- atan(slope)

r <- 4

angles <- seq(0, theta, length = 100)

arc_x <- x0 + r * cos(angles)
arc_y <- y0 + r * sin(angles)

lines(arc_x, arc_y, lwd = 2)

text(x0 + 1.5,
     y0 + 30,
     expression(theta))

# ===============================
# Right triangle
# (hypotenuse = tangent)
# ===============================

# Horizontal side: from (20, 400) to (30, 400)
segments(x0, y0,
         x0 + 10, y0,
         lwd = 2)

# Vertical side: from (30, 400) to (30, y_tangent at x=30)
x1 <- x0 + 10
y1 <- y0 + slope * (x1 - x0)

segments(x1, y0,
         x1, y1,
         lwd = 2)

# Right-angle marker (scaled with respect to the axis ratio)
sq_x <- 1
sq_y <- 50
segments(x1 - sq_x, y0,         x1 - sq_x, y0 + sq_y, lwd = 1)
segments(x1 - sq_x, y0 + sq_y,  x1,        y0 + sq_y, lwd = 1)

# Δx and Δy labels on the perpendicular sides
text((x0 + x1)/2, y0, labels = expression(Delta * x == 10), pos = 1)
text(x1, (y0 + y1)/2, labels = expression(Delta * y == 400), pos = 4)

# ===============================
# Projections of the vertices onto the axes
# ===============================

# Vertical projections (onto the x axis)
segments(x0, 0, x0, y0, col = "gray60", lty = 3)
segments(x1, 0, x1, y1, col = "gray60", lty = 3)

# Horizontal projections (onto the y axis)
segments(0, y0, x0, y0, col = "gray60", lty = 3)
segments(0, y1, x1, y1, col = "gray60", lty = 3)

# Values on the axes
axis(1, at = c(x0, x1), labels = c(x0, x1),
     col.axis = "gray30", tick = FALSE, line = -0.7)
axis(2, at = c(y0, y1), labels = c(y0, y1),
     col.axis = "gray30", tick = FALSE, line = -0.7, las = 1)

# ===============================
# Legend
# ===============================

legend("topleft",
       legend = c(
         expression(y == x^2),
         "Tangent at the point with abscissa x = 20 and ordinate y = 400",
         expression(f~"'"~"(20)=40"),
         expression(tan(theta) == "Δy/Δx = 400/10 = 40")
       ),
       col = c("blue", "red", "black", "black"),
       lwd = c(3,3,NA,NA),
       lty = c(1,2,NA,NA),
       bty = "n")

```

---
# Necessary definitions
- The slope of an angle θ is defined as the tangent of the angle θ (tan(θ))
- The slope of a line with respect to the X axis is defined as the slope of the angle formed between the line and the X axis.
- The derivative at a point x of a function f(x) equals the slope of the tangent line at the point x.


# Interpretation of the plot

- The blue curve is the function:

\[
y=x^2
\]

- The red dashed line is the tangent at the point \(x=20\).

- The derivative at this point is:

\[
f'(20)=40
\]

- The derivative corresponds to the slope of the tangent.

- The angle \(\theta\) that the tangent forms with the \(x\) axis satisfies:

\[
\tan(\theta)=40
\]

that is, the tangent of the angle equals the derivative of the function at that particular point.
