---
title: "Lab: curves in code"
heading: Lengths, areas and phases from parametric and polar curves
eyebrow: A6 · Lesson 51 · Lab: curves in code
xp: 60
---

::: hook
The perimeter of an ellipse has no formula in elementary functions, yet a few lines of Python give it to ten digits. A scope in XY mode draws an ellipse whose shape encodes the phase between two signals, and Python can read that phase back from the samples. In this lab you'll compute arc lengths and polar areas numerically, check them against exact answers where they exist, and measure a phase from a Lissajous figure's data.
:::

::: play
- The curve is a Lissajous figure, $x = \sin t$, $y = \sin(t + \varphi)$: what a scope shows in XY mode for two equal sines with a phase difference $\varphi$.
- At $\varphi = 0$ it's a line; at 90° a circle; in between, a tilted ellipse.
- The readout is the height where it crosses the $y$ axis, which is $\sin\varphi$.
:::

```sim plot
view: complex
range: 1.5
slider: p = 0..180 = 45 ° step 5
curve: sin(u) + j*sin(u + p*pi/180) | over: u = 0..6.2832 | label: Lissajous
readout: y-axis crossing, sin φ = sin(p*pi/180) | digits: 4
```

::: predict
## A scope's Lissajous ellipse crosses the $y$ axis at 0.5 (for a peak of 1). What is the phase difference?

- [x] 30° (or 150°)
- [ ] 60°
- [ ] 45°
- [ ] 0.5 rad

::: reveal
30°, or 150°: where $x = \sin t = 0$, $y = \sin(t + \varphi) = \pm\sin\varphi$, so the crossing height is $\sin\varphi = 0.5$. The tilt of the ellipse (rising or falling) tells which of the two it is. Reading the phase from samples does better than reading a crossing by eye, using every point instead of one.
:::
:::

::: nameit
## Arc length of a parametric curve

$L = \int_a^b\sqrt{\dot x^2 + \dot y^2}\,dt$. When the speed has no simple antiderivative, as for an ellipse, Simpson's rule on the speed converges fast. Ramanujan's approximation, $\pi\left[3(a + b) - \sqrt{(3a + b)(a + 3b)}\right]$, is a check good to a few parts per million for moderate eccentricity.

## Area of a polar region

$A = \frac12\int_\alpha^\beta r(\theta)^2\,d\theta$. A cardioid $r = 1 + \cos\theta$ encloses $\frac{3\pi}{2}$.

## Phase from samples

With $x = \sin t$ and $y = \sin(t + \varphi)$ sampled evenly over one period,
$$2\,\overline{y\sin t} = \cos\varphi, \qquad 2\,\overline{y\cos t} = \sin\varphi$$
by the orthogonality of sines and cosines. So $\varphi$ = `atan2(2*mean(y*cos t), 2*mean(y*sin t))`, using all the samples.
:::

::: example
## Find the perimeter of the ellipse with semi-axes 3 and 2.

::: solution
1. **Set up the integral.**
   $$L = \int_0^{2\pi}\sqrt{9\sin^2t + 4\cos^2t}\,dt$$
2. **Evaluate it numerically.** Simpson's rule with 1000 slices gives 15.865 440.
3. **Compare with Ramanujan's formula.** It gives 15.865 438: they agree to two parts in ten million.
4. **Compare with a circle.** A circle of the same average radius, 2.5, has circumference 15.708: an ellipse is a little longer.

**Answer:** the perimeter is 15.865 440.
:::
:::

::: example
## A cable with $a$ = 1000 m spans 200 m. Find its length numerically and exactly.

::: solution
1. **Set up the integral.**
   $$L = \int_{-100}^{100}\cosh(x/1000)\,dx$$
2. **Evaluate it numerically.** Simpson's rule gives 200.333 500 04 m.
3. **Find the exact value.**
   $$2000\sinh(0.1) = 200.333\,500\,04\ \text{m}$$
4. **Compare with the span.** The cable is 33 cm longer than the span.

**Answer:** the cable is 200.333 500 04 m long, 33 cm more than its span.
:::
:::

::: check
given: a = 3 | 5; b = 1 | 2
answer: pi*(3*(a + b) - sqrt((3*a + b)*(a + 3*b)))
tolerance: 0.5%

## What does Ramanujan's formula give for the perimeter of an ellipse with semi-axes {a} and {b}?

::: right
Right. {answer}.
:::

::: misses
- [pi*(a + b)] That's a rough first guess; Ramanujan's formula refines it.
- [any] Not yet. $\pi\left[3(a + b) - \sqrt{(3a + b)(a + 3b)}\right]$.
:::
:::

::: check
given: p = 20 | 60 | 70
answer: sin(p*pi/180)
tolerance: 0.5%

## Two equal sines differ in phase by {p}°. Where does their Lissajous ellipse cross the $y$ axis, for a peak of 1?

::: right
Right. $\sin\varphi$ = {answer}.
:::

::: misses
- [cos(p*pi/180)] At the crossing $x = 0$, so $t = 0$ and $y = \sin\varphi$.
- [any] Not yet. Put $x = \sin t = 0$.
:::
:::

::: check
kind: python

## Write `arc_length(dx, dy, a, b, n=1000)` returning $\int_a^b\sqrt{\dot x^2 + \dot y^2}\,dt$ by Simpson's rule, where `dx` and `dy` are the derivative functions. Set `L` to the perimeter of the ellipse with semi-axes 3 and 2.

```python starter
import math

def arc_length(dx, dy, a, b, n=1000):
    return 0

L = arc_length(lambda t: -3 * math.sin(t), lambda t: 2 * math.cos(t), 0, 2 * math.pi)
print(L)
```

```python solution
import math

def arc_length(dx, dy, a, b, n=1000):
    h = (b - a) / n
    f = lambda t: math.hypot(dx(t), dy(t))
    s = f(a) + f(b)
    for k in range(1, n):
        s += (4 if k % 2 else 2) * f(a + k * h)
    return s * h / 3

L = arc_length(lambda t: -3 * math.sin(t), lambda t: 2 * math.cos(t), 0, 2 * math.pi)
print(L)
```

```python tests
import math
ram = math.pi * (3 * 5 - math.sqrt(11 * 9))
assert abs(L - ram) < 1e-5, f"The perimeter should agree with Ramanujan's 15.86544; got {L}."
c = arc_length(lambda t: -2 * math.sin(t), lambda t: 2 * math.cos(t), 0, 2 * math.pi)
assert abs(c - 4 * math.pi) < 1e-9, "A circle of radius 2 has circumference 4 pi."
cyc = arc_length(lambda t: 1 - math.cos(t), lambda t: math.sin(t), 0, 2 * math.pi)
assert abs(cyc - 8) < 1e-6, "One arch of the cycloid has length 8."
```

::: right
Right. The same few lines measure any parametric curve you can differentiate.
:::
:::

::: check
kind: python

## Write `polar_area(r, a, b, n=1000)` returning $\frac12\int_a^b r(\theta)^2\,d\theta$ by Simpson's rule. Set `A` to the area inside the cardioid $r = 1 + \cos\theta$.

```python starter
import math

def polar_area(r, a, b, n=1000):
    return 0

A = polar_area(lambda th: 1 + math.cos(th), 0, 2 * math.pi)
print(A)
```

```python solution
import math

def polar_area(r, a, b, n=1000):
    h = (b - a) / n
    f = lambda th: 0.5 * r(th) ** 2
    s = f(a) + f(b)
    for k in range(1, n):
        s += (4 if k % 2 else 2) * f(a + k * h)
    return s * h / 3

A = polar_area(lambda th: 1 + math.cos(th), 0, 2 * math.pi)
print(A)
```

```python tests
import math
assert abs(A - 1.5 * math.pi) < 1e-9, f"The cardioid encloses 3 pi / 2; got {A}."
assert abs(polar_area(lambda th: 2, 0, 2 * math.pi) - 4 * math.pi) < 1e-9, "A circle r = 2 encloses 4 pi."
petal = polar_area(lambda th: math.cos(3 * th), -math.pi / 6, math.pi / 6)
assert abs(petal - math.pi / 12) < 1e-9, "One petal of r = cos 3 theta has area pi/12."
```

::: right
Right. Polar areas follow from one formula, whatever the curve.
:::
:::

::: check
kind: python
given: p = 0.3 | 0.7 | 1.2 | 2.5

## The starter samples $x = \sin t$ and $y = \sin(t + \varphi)$ at 1000 points over one period, with an unknown $\varphi$. Set `phi` to the phase measured from all the samples, using the orthogonality sums in the reading.

```python starter
import math
N = 1000
t = [2 * math.pi * k / N for k in range(N)]
x = [math.sin(v) for v in t]
y = [math.sin(v + p) for v in t]

phi = 0
print(phi)
```

```python solution
import math
N = 1000
t = [2 * math.pi * k / N for k in range(N)]
x = [math.sin(v) for v in t]
y = [math.sin(v + p) for v in t]

s = 2 * sum(yy * math.sin(v) for v, yy in zip(t, y)) / N
c = 2 * sum(yy * math.cos(v) for v, yy in zip(t, y)) / N
phi = math.atan2(c, s)
print(phi)
```

```python tests
assert abs(phi - p) < 1e-9, f"The measured phase should be {p}; got {phi}."
```

::: right
Right. Using every sample makes the measurement exact here, and robust to noise in practice.
:::
:::

::: check
kind: python
given: a = 200 | 1000; S = 100 | 300

## A catenary $y = a\cosh(x/a)$ with $a$ = {a} m spans {S} m. Use your `arc_length` idea with $x$ as the parameter to set `L_num` to its length by Simpson's rule, and `L_exact` to $2a\sinh\frac{S}{2a}$.

```python starter
import math

L_num = L_exact = 0
print(L_num, L_exact)
```

```python solution
import math

def simpson(f, a, b, n=1000):
    h = (b - a) / n
    s = f(a) + f(b)
    for k in range(1, n):
        s += (4 if k % 2 else 2) * f(a + k * h)
    return s * h / 3

L_num = simpson(lambda x: math.sqrt(1 + math.sinh(x / a) ** 2), -S / 2, S / 2)
L_exact = 2 * a * math.sinh(S / (2 * a))
print(L_num, L_exact)
```

```python tests
import math
assert abs(L_exact - 2 * a * math.sinh(S / (2 * a))) < 1e-9, "L_exact should be 2a sinh(S/2a)."
assert abs(L_num - L_exact) < 1e-8, f"The numerical length should match {L_exact:.6f}; got {L_num}."
assert L_num > S, "The cable is longer than its span."
```

::: right
Right. With $\sqrt{1 + \sinh^2} = \cosh$, the integrand is smooth and Simpson's rule is essentially exact.
:::
:::

::: check
kind: python

## The area under one arch of the cycloid $x = t - \sin t$, $y = 1 - \cos t$ is $\int y\,dx = \int_0^{2\pi}(1 - \cos t)^2\,dt$. Set `A` to it by Simpson's rule (1000 slices) and `exact` to $3\pi$.

```python starter
import math

A = exact = 0
print(A, exact)
```

```python solution
import math

def simpson(f, a, b, n=1000):
    h = (b - a) / n
    s = f(a) + f(b)
    for k in range(1, n):
        s += (4 if k % 2 else 2) * f(a + k * h)
    return s * h / 3

A = simpson(lambda t: (1 - math.cos(t)) ** 2, 0, 2 * math.pi)
exact = 3 * math.pi
print(A, exact)
```

```python tests
import math
assert abs(exact - 3 * math.pi) < 1e-12 and abs(A - exact) < 1e-9, f"The arch encloses 3 pi; got {A}."
```

::: right
Right. Three times the area of the rolling circle: a result Galileo estimated by cutting shapes from paper and weighing them.
:::
:::

::: explain
## Why does measuring phase from all the samples beat reading where the Lissajous figure crosses the axis?

::: model
Reading the crossing uses essentially one point of the curve, so any noise there, any offset in the signals or any error in judging exactly where the axis is crossed goes straight into the answer. The orthogonality sums use every sample: multiplying $y$ by $\sin t$ and $\cos t$ and averaging over a whole period picks out exactly the in-phase and quadrature parts of $y$, and random noise in individual samples averages down as the square root of their number. A DC offset or a harmonic at another frequency averages to zero against the sine and cosine too. This is the same principle a lock-in amplifier or a network analyzer uses to measure phase precisely.
:::
:::

::: practice

::: check
given: a = 1..10; b = 0.5..10
answer: pi*(3*(a + b) - sqrt((3*a + b)*(a + 3*b)))
tolerance: 0.5%

## Ramanujan's ellipse perimeter: semi-axes {a} and {b}?

::: misses
- [any] $\pi\left[3(a + b) - \sqrt{(3a + b)(a + 3b)}\right]$.
:::
:::

::: check
given: a = 0.1..5
answer: 1.5*pi*a^2
tolerance: 0.5%

## Area inside the cardioid $r = \given{a}(1 + \cos\theta)$?

::: misses
- [any] $\frac{3\pi}{2}a^2$.
:::
:::
:::
