---
title: "Lab: adding sinusoids with arrows in Python"
heading: Turning a pile of sine waves into one
eyebrow: A2 · Lesson 27 · Lab: adding sinusoids
xp: 60
---

::: hook
A three-phase motor, an array of antennas and every AC circuit you'll analyze have the same job at their core: add up several sine waves of one frequency. The last two lessons did it by hand with arrows. In this lab you'll teach Python to do it: convert between rectangular and polar form, add any number of sinusoids, and then check the result the slow way by adding the waves sample by sample. If you haven't started A4 yet, its lessons on functions, loops and lists (lessons 1 to 9) cover the Python used here.
:::

::: play
- Two sinusoids of the same frequency, drawn as arrows: $A\angle\varphi$ and $B\angle\theta$. The third arrow is their sum, drawn tip to tail.
- Change the lengths and angles. The sum's length and angle are the amplitude and phase of the combined wave.
- Find two settings where the sum is shorter than either arrow on its own.
:::

```sim plot
view: complex
range: 5
slider: A = 0..4 = 3 step 0.5
slider: phi = -180..180 = 0 ° step 15
slider: B = 0..4 = 4 step 0.5
slider: theta = -180..180 = 90 ° step 15
arrow: A∠phi | label: first
arrow: B∠theta | from: A∠phi | label: second
arrow: A∠phi + B∠theta | color: ch3 | label: sum
readout: amplitude of the sum = abs(A*exp(j*phi*pi/180) + B*exp(j*theta*pi/180)) | digits: 4
readout: phase of the sum, degrees = arg(A*exp(j*phi*pi/180) + B*exp(j*theta*pi/180))*180/pi | digits: 4
```

::: predict
## Three sine waves of amplitude 10 at 0°, 120° and 240° are added. What is the amplitude of the sum?

- [x] 0
- [ ] 10
- [ ] 17.3
- [ ] 30

::: reveal
It's zero. The three arrows, drawn tip to tail, close into an equilateral triangle and end where they started. That's why a balanced three-phase load needs no return current: the neutral wire carries the sum of the three currents, and it's zero. If one current is a little bigger, the neutral carries the difference.
:::
:::

::: nameit
## Rectangular and polar form in Python

A sinusoid $A\cos(\omega t + \varphi)$ is the arrow $A\angle\varphi$. Its components are

$$x = A\cos\varphi, \qquad y = A\sin\varphi$$

and going back, $A = \sqrt{x^2 + y^2}$ and $\varphi$ is the angle of $(x, y)$. Python's `math.atan2(y, x)` returns that angle in radians in the right quadrant, from $-180°$ to $180°$, which a plain `math.atan(y / x)` can't do (it can't tell $(1, 1)$ from $(-1, -1)$).

## Adding sinusoids

Sinusoids of the same frequency add as their arrows do:
1. convert each to components;
2. add all the $x$ parts and all the $y$ parts;
3. convert the totals back to polar form.

## Checking by samples

The arrow method claims that $\sum_k A_k\cos(\theta + \varphi_k) = A\cos(\theta + \varphi)$ for every $\theta$. A computer can test the claim directly: evaluate both sides at many angles and find the largest difference. If the method is right, that difference is only rounding error, around $10^{-15}$ times the size of the numbers.
:::

::: example
## Add $3\cos\omega t$ and $4\cos(\omega t + 90°)$.

::: solution
1. **Components.** $3\angle 0°$ is $(3, 0)$ and $4\angle 90°$ is $(0, 4)$.
2. **Add.** The sum is $(3, 4)$, of length 5 and angle
   $$\arctan(4/3) = 53.13°$$
   So the sum is $5\cos(\omega t + 53.13°)$.
3. **Check at ωt = 0.** The left side is $3 + 4\cos 90° = 3$, and the right side is $5\cos 53.13° = 3$.

**Answer:** the sum is $5\cos(\omega t + 53.13°)$.
:::
:::

::: example
## The three phase currents of a slightly unbalanced load have amplitudes 10, 10 and 12 A, at 0°, −120° and 120°. What current flows in the neutral wire?

::: solution
1. **Add the x components.**
   $$\begin{aligned} x &= 10 + 10\cos(-120°) \\ &\quad + 12\cos 120° \\ &= 10 - 5 - 6 \\ &= -1 \end{aligned}$$
2. **Add the y components.**
   $$\begin{aligned} y &= 10\sin(-120°) \\ &\quad + 12\sin 120° \\ &= -8.660 + 10.392 \\ &= 1.732 \end{aligned}$$
3. **Back to polar.** The sum is $(-1, 1.732)$, of length 2 at 120°.
4. **Read the result.** The neutral carries 2 A at 120°, the same as the extra 2 A in the third phase: the three balanced 10 A currents cancel and only the excess remains.

**Answer:** 2 A flows in the neutral, at 120°.
:::
:::

::: example
## Two antennas pick up the same 1 mV signal, 150° out of phase. What does a receiver that adds them get?

::: solution
1. **Components.** Add $1\angle 0°$ and $1\angle 150°$:
   $$\begin{aligned} & (1 + \cos 150°,\ \sin 150°) \\ &= (0.134,\ 0.5) \end{aligned}$$
2. **Back to polar.**
   $$\begin{aligned} r &= \sqrt{0.134^2 + 0.5^2} \\ &= 0.518\ \text{mV} \\ \theta &= \arctan(0.5/0.134) \\ &= 75° \end{aligned}$$
   The sum is weaker than either antenna alone.
3. **The general rule.** Equal arrows always add to $2\cos(\delta/2)$ times one of them, pointing halfway between them. Here that's
   $$2\cos 75° = 0.518$$
4. **Where it's used.** Phased-array antennas use exactly this to cancel signals from some directions and reinforce others.

**Answer:** the receiver gets 0.518 mV at 75°, weaker than either antenna alone.
:::
:::

::: check
given: A = 2 | 5 | 10; d = 30 | 60 | 150
answer: A*sqrt(2 + 2*cos(d°))
tolerance: 0.5%

## Two sinusoids of amplitude {A} are {d}° apart. What is the amplitude of their sum?

::: right
Right. The arrows make a rhombus, and the sum is its long diagonal: {answer}.
:::

::: misses
- [2*A] That's when they're in phase.
- [A*sqrt(2 - 2*cos(d°))] That's their difference.
- [any] Not yet. Add $A\angle 0°$ and $A\angle\given{d}°$ by components.
:::
:::

::: check
given: x = -3 | -1 | -2; y = -4 | 2 | 5
answer: arg(x + j*y)*180/pi
unit: °
tolerance: 0.5%

## What angle does `math.atan2(y, x)` give, in degrees, for the point $(\given{x}, \given{y})$?

::: right
Right. It's {answer}, in the right quadrant.
:::

::: misses
- [atan(y/x)*180/pi] That's `math.atan(y / x)`, which can't tell this point from the one opposite it.
- [any] Not yet. Find the quadrant first, then the angle from the positive $x$ axis.
:::
:::

::: check
kind: python

## Write `to_rect(A, deg)`, returning the components `(x, y)` of the arrow $A\angle$`deg`, and `to_polar(x, y)`, returning `(A, deg)` with the angle from `math.atan2` in degrees.

```python starter
import math

def to_rect(A, deg):
    return A, 0

def to_polar(x, y):
    return x, 0

print(to_rect(5, 53.13), to_polar(-3, -4))
```

```python solution
import math

def to_rect(A, deg):
    r = math.radians(deg)
    return A * math.cos(r), A * math.sin(r)

def to_polar(x, y):
    return math.hypot(x, y), math.degrees(math.atan2(y, x))

print(to_rect(5, 53.13), to_polar(-3, -4))
```

```python tests
import math
x, y = to_rect(2, 90)
assert abs(x) < 1e-12 and abs(y - 2) < 1e-12, "2 at 90 degrees points straight up: (0, 2)."
x, y = to_rect(10, -120)
assert abs(x + 5) < 1e-12 and abs(y + 5 * math.sqrt(3)) < 1e-12, "10 at -120 degrees is (-5, -8.66)."
A, d = to_polar(-3, -4)
assert abs(A - 5) < 1e-12 and abs(d + 126.8698976) < 1e-6, f"(-3, -4) is 5 at -126.87 degrees; got {A} at {d}."
A, d = to_polar(-1, 0)
assert abs(A - 1) < 1e-12 and abs(abs(d) - 180) < 1e-12, "(-1, 0) points at 180 degrees."
for A0, d0 in [(1, 10), (3, 170), (0.5, -45), (7, -179)]:
    A1, d1 = to_polar(*to_rect(A0, d0))
    assert abs(A1 - A0) < 1e-12 and abs(d1 - d0) < 1e-9, "Converting to rectangular and back should return the same arrow."
```

::: right
Right. With `atan2`, every quadrant comes back correctly.
:::
:::

::: check
kind: python

## Write `add_sinusoids(waves)`. `waves` is a list of `(A, deg)` pairs, each standing for $A\cos(\omega t + \text{deg})$, all at the same frequency. Return the single `(A, deg)` that equals their sum. You can use your functions from the last task, or write the components out again.

```python starter
import math

def add_sinusoids(waves):
    return 0, 0

print(add_sinusoids([(3, 0), (4, 90)]))
```

```python solution
import math

def add_sinusoids(waves):
    x = sum(A * math.cos(math.radians(d)) for A, d in waves)
    y = sum(A * math.sin(math.radians(d)) for A, d in waves)
    return math.hypot(x, y), math.degrees(math.atan2(y, x))

print(add_sinusoids([(3, 0), (4, 90)]))
```

```python tests
import math
A, d = add_sinusoids([(3, 0), (4, 90)])
assert abs(A - 5) < 1e-12 and abs(d - 53.130102) < 1e-6, f"3 at 0 plus 4 at 90 is 5 at 53.13 degrees; got {A} at {d}."
A, d = add_sinusoids([(10, 0), (10, -120), (12, 120)])
assert abs(A - 2) < 1e-12 and abs(d - 120) < 1e-9, f"The unbalanced currents should leave 2 A at 120 degrees; got {A} at {d}."
A, d = add_sinusoids([(5, 0), (5, 120)])
assert abs(A - 5) < 1e-12 and abs(d - 60) < 1e-9, "Two equal waves 120 degrees apart make one of the same size at 60 degrees."
A, d = add_sinusoids([(2, 37)])
assert abs(A - 2) < 1e-12 and abs(d - 37) < 1e-9, "One wave alone should come back unchanged."
```

::: right
Right. This one function does the arithmetic for every AC circuit you'll meet.
:::
:::

::: check
kind: python

## Write `max_difference(waves, A, deg)`. It evaluates $\sum_k A_k\cos(\theta + \varphi_k)$ and $A\cos(\theta + \varphi)$ at 3600 angles $\theta$ spread over a full turn (0°, 0.1°, 0.2°, ...) and returns the largest absolute difference between them. The test uses it to confirm `add_sinusoids`, and to show that a wrong answer is caught.

```python starter
import math

def max_difference(waves, A, deg):
    return 0

print(max_difference([(3, 0), (4, 90)], 5, 53.13010235415598))
```

```python solution
import math

def max_difference(waves, A, deg):
    worst = 0
    for k in range(3600):
        th = math.radians(k / 10)
        total = sum(a * math.cos(th + math.radians(d)) for a, d in waves)
        one = A * math.cos(th + math.radians(deg))
        worst = max(worst, abs(total - one))
    return worst

print(max_difference([(3, 0), (4, 90)], 5, 53.13010235415598))
```

```python tests
import math
assert max_difference([(3, 0), (4, 90)], 5, 53.13010235415598) < 1e-12, "3 at 0 plus 4 at 90 really is 5 at 53.13 degrees, so the difference should be rounding error."
assert abs(max_difference([(3, 0), (4, 90)], 7, 45) - 2.1688) < 1e-3, f"Adding the amplitudes (7 at 45 degrees) is wrong, and the largest error is about 2.17; got {max_difference([(3, 0), (4, 90)], 7, 45)}."
assert abs(max_difference([(1, 0)], 0, 0) - 1) < 1e-12, "Against a zero wave, the largest difference is the wave's own peak."
assert max_difference([(10, 0), (10, 120), (10, 240)], 0, 0) < 1e-12, "Three balanced phases sum to zero at every angle."
```

::: right
Right. The arrow arithmetic and the sample-by-sample sum agree to rounding error, which is the whole justification for using arrows.
:::
:::

::: explain
## Why can sinusoids of the same frequency be added as arrows, but sinusoids of different frequencies can't?

::: model
Each sinusoid $A\cos(\omega t + \varphi)$ is the horizontal shadow of an arrow of length $A$ turning at the rate $\omega$, starting at angle $\varphi$. When every arrow turns at the same rate, they keep the same angles between them forever, so the whole picture just rotates as one rigid shape. The sum of the shadows is then the shadow of the sum of the arrows, and that sum is itself a fixed-length arrow turning at $\omega$: a single sinusoid of the same frequency. If the frequencies differ, the arrows turn at different rates, their relative angles keep changing, and their sum changes length over time. That's the beating you hear from two close tones, and no single fixed arrow can describe it.
:::
:::

::: practice

::: check
given: a = 0.5..10; b = 0.5..10; p = 0..180; q = -180..0
answer: abs(a*exp(j*p*pi/180) + b*exp(j*q*pi/180))
tolerance: 0.5%

## Amplitude of $\given{a}\angle\given{p}° + \given{b}\angle\given{q}°$?

::: misses
- [any] Add by components, then take the length.
:::
:::

::: check
given: x = -10..10; y = -10..-1
answer: arg(x + j*y)*180/pi
tolerance: 0.5%

## `atan2` angle, in degrees, of $(\given{x}, \given{y})$?

::: misses
- [any] Find the quadrant, then the angle.
:::
:::
:::
