---
title: "Arc Length of a Curve"
theme: flatly 
date: today
toc: true
format:
  html: 
    code-overflow: wrap
    toc: true
    toc-title: "Contents"
    # hide code?

execute:
  warning: false
  message: false
---

The arc length of a curve defined by a function \[ y = f(x) \] from \[ x = a \] to \[ x = b \] can be calculated using the following formula:

$$ L = \int_{a}^{b} \sqrt{1 + \left( \frac{dy}{dx} \right)^2} \, dx $$

where $\frac{dy}{dx}$ is the derivative of the function $f(x)$.
To compute the arc length, follow these steps:

## Steps to Calculate Arc Length

1. **Find the Derivative**: Compute $\frac{dy}{dx}$ for the function $f(x) $.
2. **Set Up the Integral**: Substitute $\frac{dy}{dx}$ into the arc length formula.
3. **Evaluate the Integral**: Calculate the definite integral from $a$ to $b$.
This will give you the length of the curve between the points $ (a, f(a))$ and $(b, f(b))$.

## Example 1

Consider the function $f(x) = x^2$ from $x = 0$ to $x = 1$:

1. **Find the Derivative**: $\frac{dy}{dx} = 2x$
2. **Set Up the Integral**:
$L = \int_{0}^{1} \sqrt{1 + (2x)^2} \, dx = \int_{0}^{1} \sqrt{1 + 4x^2} \, dx$
3. **Evaluate the Integral**: This integral can be computed using a suitable method (e.g., trigonometric substitution or numerical integration) to find the arc length.
The result will give you the length of the curve $y = x^2$ from $x = 0$ to $x = 1$.

```{r}
# R code to compute the arc length of y = x^2 from x = 0 to x = 1
library(pracma) 
f <- function(x) { sqrt(1 + (2*x)^2) }
arc_length <- integral(f, 0, 1)
arc_length
```

## Via: Trigonometric Substitution

You can use trigonometric substitution to evaluate the integral:

1. Let $x = \frac{1}{2} \tan(\theta)$, then $dx = \frac{1}{2} \sec^2(\theta) d\theta$.

2. Substitute into the integral:

$$L = \int \sqrt{1 + 4\left(\frac{1}{2} \tan(\theta)\right)^2} \cdot \frac{1}{2} \sec^2(\theta) d\theta$$

3. Simplify: 

$$ L = \frac{1}{2} \int \sec(\theta) \sec^2(\theta) d\theta = \frac{1}{2} \int \sec^3(\theta) d\theta$$

The integral of $\sec^3(\theta)$ can be computed using integration by parts as folows:
$$\int \sec^3(\theta) d\theta = \int \sec(\theta)(\sec^2(\theta)) d\theta$$

4. Integration by parts:

Let $u = \sec(\theta)$ and $dv = \sec^2(\theta) d\theta$,
then $du = \sec(\theta) \tan(\theta) d\theta$ and $v = \tan(\theta)$.

Applying integration by parts:
$$\int \sec^3(\theta) d\theta = \sec(\theta) \tan(\theta) - \int \sec(\theta) \tan^2(\theta) d\theta$$
Using the identity $\tan^2(\theta) = \sec^2(\theta) - 1$, we have:
$$\int \sec^3(\theta) d\theta = \sec(\theta) \tan(\theta) - \int \sec^3(\theta) d\theta + \int \sec(\theta) d\theta.$$
Rearranging gives:
$$2 \int \sec^3(\theta) d\theta = \sec(\theta) \tan(\theta) + \int \sec(\theta) d\theta.$$
The integral of $\sec(\theta)$ is:
$$\int \sec(\theta) d\theta = \ln|\sec(\theta) + \tan(\theta)| + .C$$

5. Putting it all together, we get:

$$L = \frac{1}{2} \left[ \frac{1}{2} \sec(\theta) \tan(\theta) + \frac{1}{2} \ln|\sec(\theta) + \tan(\theta)| \right] + C$$.

Note, the first \frac{1}{2} comes from the substitution for dx.

## Get back to x
To express the result back in terms of $x$, we 

1. use the substitutions:
    + $\sec(\theta) = \sqrt{1 + 4x^2}$ 

        . (this comes from the original substitution and thinking about the right triangle where the hypotenuse is $\sqrt{1 + 4x^2}$ and the adjacent side is 1

    + $\tan(\theta) = 2x$.

2. Then we substitute these back into the expression for $L$ and evaluate it from $x = 0$ to $x = 1$ to find the arc length of the curve $y = x^2$ over that interval:

$$L = \frac{1}{4} \left[ \sqrt{1 + 4x^2} \cdot 2x + \ln\left|\sqrt{1 + 4x^2} + 2x\right| \right]_{0}^{1}$$

Evaluating this expression at the limits will give you the arc length of the curve from $x = 0$ to $x = 1$.
```{r}
# R code to compute the arc length of y = x^2 from x = 0 to x = 1 using the trigonometric substitution result
L = (1/4) * ((sqrt(1 + 4*1^2) * 2*1 + log(abs(sqrt(1 + 4*1^2) + 2*1))) - 
            (sqrt(1 + 4*0^2) * 2*0 + log(abs(sqrt(1 + 4*0^2) + 2*0))))
L
```

## Example 2

Find the arc length of the curve $$ y^2 = x^3$$ from (1,1) to (4,8).

1. **Express y in terms of x**: From the equation $y^2 = x^3$, we have $y = x^{3/2}$.

2. **Find the Derivative**: $\frac{dy}{dx} = \frac{3}{2} x^{1/2}$

3. **Set Up the Integral**:
$$ L = \int_{1}^{4} \sqrt{1 + \left( \frac{3}{2} x^{1/2} \right)^2} \, dx = \int_{1}^{4} \sqrt{1 + \frac{9}{4} x} \, dx $$

4. **Evaluate the Integral**: This integral can be computed using u-substitution) to find the arc length.

5. The result will give you the length of the curve $y^2 = x^3$ from $(1,1)$ to $(4,8)$.

## Step-by-step solution via u-substitution
1. Let $u = 1 + \frac{9}{4} x$, then $du = \frac{9}{4} dx$ or $dx = \frac{4}{9} du$.

2. Change the limits of integration:
   - When $x = 1$, $u = 1 + \frac{9}{4} \cdot 1 = \frac{13}{4}$.
   - When $x = 4$, $u = 1 + \frac{9}{4} \cdot 4 = 10$.

3. Substitute into the integral:
$$ L = \int_{\frac{13}{4}}^{10} \sqrt{u} \cdot \frac{4}{9} du = \frac{4}{9} \int_{\frac{13}{4}}^{10} u^{1/2} du $$

4. Evaluate the integral:
$$ L = \frac{4}{9} \left[ \frac{2}{3} u^{3/2} \right]_{\frac{13}{4}}^{10} = \frac{8}{27} \left[ 10^{3/2} - \left(\frac{13}{4}\right)^{3/2} \right] $$

```{r}
# R code to compute the arc length of y^2 = x^3 from (1,1) to (4,8)
L2 = (8/27) * (10^(3/2) - (13/4)^(3/2))
L2
```