# Trigonometric functions


{{< include ../_common_code.qmd >}}

This section uses the following add-on packages:


```{julia}
using CalculusWithJulia
using Plots
plotly()
using SymPy
```

---


We have informally used some of the trigonometric functions in examples so far. In this section we quickly review their definitions and some basic properties.


The trigonometric functions are used to describe relationships between triangles and circles as well as oscillatory motions. With such a wide range of utility it is no wonder that they pop up in many places and their [origins](https://en.wikipedia.org/wiki/Trigonometric_functions#History) date to Hipparcus and Ptolemy over $2000$ years ago.


## The 6 basic trigonometric functions


We measure angles in radians, where $360$ degrees is $2\pi$ radians. By proportions, $180$ degrees is $\pi$ radian, $90$ degrees is $\pi/2$ radians, $60$ degrees is $\pi/3$ radians, etc. In general, $x$ degrees is $2\pi \cdot x / 360$ radians (or, with cancellation, $x \cdot \frac{\pi}{180}$).


For a right triangle with angles $\theta$, $\pi/2 - \theta$, and $\pi/2$ ($0 < \theta < \pi/2$) we call the side opposite $\theta$ the "opposite" side, the shorter adjacent side the "adjacent" side and the longer adjacent side the hypotenuse.


```{julia}
#| hide: true
#| echo: false
p = plot(legend=false, xlim=(-1/4,5), ylim=(-1/2, 3),
         xticks=nothing, yticks=nothing, border=:none)
plot!([0,4,4,0],[0,0,3,0], linewidth=3)
del = .25
plot!([4-del, 4-del,4], [0, del, del], color=:black, linewidth=3)
annotate!([(.75, .25, "θ"), (4.0, 1.25, "opposite"), (2, -.25, "adjacent"), (1.5, 1.25, "hypotenuse")])
```

With these, the basic definitions for the primary trigonometric functions are

::: {.callout-note icon=false}
## Trigonometric definitions
$$
\begin{align*}
\sin(\theta) &= \frac{\text{opposite}}{\text{hypotenuse}} &\quad(\text{the sine function})\\
\cos(\theta) &= \frac{\text{adjacent}}{\text{hypotenuse}} &\quad(\text{the cosine function})\\
\tan(\theta) &= \frac{\text{opposite}}{\text{adjacent}}  &\quad(\text{the tangent function})
\end{align*}
$$
:::

:::{.callout-note}
## Note
Many students remember these through [SOH-CAH-TOA](http://mathworld.wolfram.com/SOHCAHTOA.html).

:::

Some algebra shows that $\tan(\theta) = \sin(\theta)/\cos(\theta)$. There are also $3$ reciprocal functions, the cosecant, secant and cotangent.


These definitions in terms of sides only apply for $0 \leq \theta \leq \pi/2$. More generally, if we relate any angle taken in the counter clockwise direction for the $x$-axis with a point $(x,y)$ on the *unit* circle, then we can extend these definitions - the point $(x,y)$ is also $(\cos(\theta), \sin(\theta))$.


```{julia}
#| hold: true
#| echo: false
#| cache: true
## {{{radian_to_trig}}}

gr()

function plot_angle(m)
    r = m*pi

    ts = range(0, stop=2pi, length=100)
    tit = "$m ⋅ pi -> ($(round(cos(r), digits=2)), $(round(sin(r), digits=2)))"
    p = plot(cos.(ts), sin.(ts), legend=false, aspect_ratio=:equal,title=tit)
    plot!(p, [-1,1], [0,0], color=:gray30)
    plot!(p,  [0,0], [-1,1], color=:gray30)

    if r > 0
        ts = range(0, stop=r, length=100)
    else
        ts = range(r, stop=0, length=100)
    end

    plot!(p, (1/2 .+ abs.(ts)/10pi).* cos.(ts), (1/2 .+ abs.(ts)/10pi) .* sin.(ts), color=:red, linewidth=3)
    l = 1 #1/2 + abs(r)/10pi
    plot!(p, [0,l*cos(r)], [0,l*sin(r)], color=:green, linewidth=4)

    scatter!(p, [cos(r)], [sin(r)], markersize=5)
    annotate!(p, [(1/4+cos(r), sin(r), "(x,y)")])

    p
end



## different linear graphs
anim = @animate for m in  -4//3:1//6:10//3
    plot_angle(m)
end

imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
caption = "An angle in radian measure corresponds to a point on the unit circle, whose coordinates define the sine and cosine of the angle. That is ``(x,y) = (\\cos(\\theta), \\sin(\\theta))``."

plotly()
ImageFile(imgfile, caption)
```

### The trigonometric functions in Julia


Julia has the $6$ basic trigonometric functions defined through the functions `sin`, `cos`, `tan`, `csc`, `sec`, and `cot`.


Two right triangles - the one with equal, $\pi/4$, angles; and the one with angles $\pi/6$ and $\pi/3$ can have the ratio of their sides computed from basic geometry. In particular, this leads to the following values, which are usually committed to memory:


$$
\begin{align*}
\sin(0) &= 0, \quad \sin(\pi/6) = \frac{1}{2}, \quad \sin(\pi/4) = \frac{\sqrt{2}}{2}, \quad\sin(\pi/3) = \frac{\sqrt{3}}{2},\text{ and } \sin(\pi/2) = 1\\
\cos(0) &= 1, \quad \cos(\pi/6) =  \frac{\sqrt{3}}{2}, \quad \cos(\pi/4) = \frac{\sqrt{2}}{2}, \quad\cos(\pi/3) = \frac{1}{2},\text{ and } \cos(\pi/2) = 0.
\end{align*}
$$


Using the circle definition allows these basic values to inform us of values throughout the unit circle.


These all follow from the definition involving the unit circle:


  * If the angle $\theta$ corresponds to a point $(x,y)$ on the unit circle, then the angle $-\theta$ corresponds to $(x, -y)$. So $\sin(\theta) = - \sin(-\theta)$ (an odd function), but $\cos(\theta) = \cos(-\theta)$ (an even function).
  * If the angle $\theta$ corresponds to a point $(x,y)$ on the unit circle, then rotating by $\pi$ moves the points to $(-x, -y)$. So $\cos(\theta) = x = - \cos(\theta + \pi)$, and $\sin(\theta) = y = -\sin(\theta + \pi)$.
  * If the angle $\theta$ corresponds to a point $(x,y)$ on the unit circle, then rotating by $\pi/2$ moves the points to $(-y, x)$. So $\cos(\theta) = x = \sin(\theta + \pi/2)$.


The fact that $x^2 + y^2 = 1$ for the unit circle leads to the "Pythagorean identity" for trigonometric functions:


$$
\sin^2(\theta) + \cos^2(\theta) = 1.
$$

This basic fact can be manipulated many ways. For example, dividing through by $\cos^2(\theta)$ gives the related identity: $\tan^2(\theta) + 1 = \sec^2(\theta)$.


`Julia`'s functions can compute values for any angles, including these fundamental ones:


```{julia}
[cos(theta) for theta in [0, pi/6, pi/4, pi/3, pi/2]]
```

To compute $\sin^2(\theta)$, the power is applied to the value of $\sin(\theta)$ and not the `sin` function. (Think of $\sin^2(\theta)$ as $(\sin(\theta))^2$:

```{julia}
theta = pi/8
sin(theta)^2
```


These values are floating point approximations, as can be seen clearly in the computation of `cos(pi/2)`, which is mathematically $0$. Symbolic math can be used by using `PI` for `pi` if exactness matters:

```{julia}
cos.([0, PI/6, PI/4, PI/3, PI/2])
```

The `sincos` function computes both `sin` and `cos` simultaneously, which can be more performant when both values are needed.


```{julia}
sincos(pi/3)
```

:::{.callout-note}
## Note
For really large values, round off error can play a big role. For example, the *exact* value of $\sin(1000000 \pi)$ is $0$, but the returned value is not quite $0$ `sin(1_000_000 * pi) = -2.231912181360871e-10`. For exact multiples of $\pi$ with large multiples the `sinpi` and  `cospi` functions are useful.

(Both functions are computed by first employing periodicity to reduce the problem to a smaller angle. However, for large multiples the floating-point roundoff becomes a problem with the usual functions.)

:::

##### Example


Measuring the height of a [tree](https://lifehacker.com/5875184/is-there-an-easy-way-to-measure-the-height-of-a-tree) may be a real-world task for some, but a typical task for nearly all trigonometry students. How might it be done? If a right triangle can be formed where the angle and adjacent side length are known, then the opposite side (the height of the tree) can be solved for with the tangent function. For example, if standing $100$ feet from the base of the tree the tip makes a $15$ degree angle the height is given by:


```{julia}
#| hold: true
theta = 15 * pi / 180
adjacent = 100
opposite = adjacent * tan(theta)
```

Having some means to compute an angle and then a tangent of that angle handy is not a given, so the linked to article provides a few other methods taking advantage of similar triangles.


You can also measure distance with your [thumb](http://www.vendian.org/mncharity/dir3/bodyruler_angle/) or fist. How? The fist takes up about $10$ degrees of view when held straight out. So, pacing off backwards until the fist completely occludes the tree will give the distance of the adjacent side of a right triangle. If that distance is $30$ paces what is the height of the tree? Well, we need some facts. Suppose your pace is $3$ feet. Then the adjacent length is $90$ feet. The multiplier is the tangent of $10$ degrees, or:


```{julia}
tan(10 * pi/180)
```

Which for sake of memory we will say is $1/6$ (a $5$ percent error). So that answer is *roughly* $15$ feet:


```{julia}
30 * 3 / 6
```

Similarly, you can use your thumb instead of your fist. To use your fist you can multiply by $1/6$ the adjacent side, to use your thumb about $1/30$ as this approximates the tangent of $2$ degrees:


```{julia}
1/30, tan(2*pi/180)
```

This could be reversed. If you know the height of something a distance away that is covered by your thumb or fist, then you would multiply that height by the appropriate amount to find your distance.


### Basic properties


The sine function is defined for all real $\theta$ and has a range of $[-1,1]$. Clearly as $\theta$ winds around the $x$-axis, the position of the $y$ coordinate begins to repeat itself. We say the sine function is *periodic* with period $2\pi$. A graph will illustrate:


```{julia}
plot(sin, 0, 4pi)
```

The graph shows two periods. The wavy aspect of the graph is why this function is used to model periodic motions, such as the amount of sunlight in a day, or the alternating current powering a computer.


From this graph - or considering when the $y$ coordinate is $0$ - we see that the sine function has zeros at any integer multiple of $\pi$, or $k\pi$, $k$ in $\dots,-2,-1, 0, 1, 2, \dots$.


The cosine function is similar, in that it has the same domain and range, but is "out of phase" with the sine curve. A graph of both shows the two are related:


```{julia}
plot(sin, 0, 4pi, label="sin")
plot!(cos, 0, 4pi, label="cos")
```

The cosine function is just a shift of the sine function (or vice versa). We see that the zeros of the cosine function happen at points of the form $\pi/2 + k\pi$, $k$ in $\dots,-2,-1, 0, 1, 2, \dots.$


The tangent function does not have all $\theta$ for its domain, rather those points where division by $0$ occurs are excluded. These occur when the cosine is $0$, or, again, at $\pi/2 + k\pi$, $k$ in $\dots,-2,-1, 0, 1, 2, \dots.$ The range of the tangent function will be all real $y$.


The tangent function is also periodic, but not with period $2\pi$, but rather just $\pi$. A graph will show this. Here we avoid the vertical asymptotes using `rangeclamp`:


```{julia}
plot(rangeclamp(tan), -10, 10, label="tan")
```

##### Example sums of sines


For the function $f(x) = \sin(x)$ we have an understanding of the related family of functions defined by linear transformations:


$$
g(x) = a + b \sin((2\pi n)x)
$$

That is a graph of $g$ will be the sine curve shifted up by $a$ units, scaled vertically by $b$ units and has a period of $1/n$. We see a simple plot here where we can verify the transformation:


```{julia}
g(x;  b=1, n=1) = b*sin(2pi*n*x)
g1(x) = 1 + g(x, b=2, n=3)
plot(g1, 0, 1)
```

We can consider the sum of such functions, for example


```{julia}
g2(x) = 1 + g(x, b=2, n=3) + g(x, b=4, n=5)
plot(g2, 0, 1)
```

Though still periodic, we can see with this simple example that sums of different sine functions can have somewhat complicated graphs.


Sine functions can be viewed as the `x` position of a point traveling around a circle so `g(x, b=2, n=3)` is the `x` position of point traveling around a circle of radius $2$ that completes a circuit in $1/3$ units of time.


The superposition of the two sine functions that `g2` represents could be viewed as the position of a circle moving around a point that is moving around another circle. The following graphic, with $b_1=1/3, n_1=3, b_2=1/4$, and $n_2=4$, shows an example that produces the related cosine sum (moving right along the $x$ axis), the sine sum (moving down along the $y$ axis, *and* the trace of the position of the point generating these two plots.


```{julia}
#| hold: true
#| echo: false
#| cache: true
gr()

function makegraph(t, b₁,n₁, b₂=0, n₂=1)

    f₁ = x -> b₁*[sin(2pi*n₁*x), cos(2pi*n₁*x)]
    f₂ = x -> b₂*[sin(2pi*n₂*x), cos(2pi*n₂*x)]
    h = x -> f₁(x) + f₂(x)

    ts = range(0, 2pi, length=1000)


    ylims = (-b₁-b₂-2, b₁ + b₂)
    xlims = (-b₁-b₂, b₁ + b₂ + 2)

    p = plot(; xlim=xlims, ylim=ylims,
             legend=false,
             aspect_ratio=:equal)

    α = 0.3
    # circle 1
    plot!(p, unzip(f₁.(range(0, 2pi/n₁, length=100))), alpha=α)
    scatter!(p, unzip([f₁(t)]), markersize=1, alpha=α)

    # circle 2
    us, vs = unzip(f₂.(range(0, 2pi/n₂, length=100)))
    a,b = f₁(t)
    plot!(p, a .+ us, b .+ vs, alpha=α)
    scatter!(p, unzip([h(t)]), markersize=5)

    # graph of (x,y) over [0,t]
    ts = range(0, t, length=200)
    plot!(p, unzip(h.(ts)), linewidth=1, alpha=0.5, linestyle=:dash)

    # graph of x over [0,t]
    ys′ = -ts
    xs′ = unzip(h.(ts))[1]
    plot!(p, xs′, ys′, linewidth=2)

    # graph of y over [0,t]
    xs′ = ts
    ys′ = unzip(h.(ts))[2]
    plot!(p, xs′, ys′, linewidth=2)


    p
end

# create animoation
b₁=1/3; n₁=3; b₂=1/4; n₂=4

anim = @animate for t ∈ range(0, 2.5, length=50)
    makegraph(t, b₁, n₁, b₂, n₂)
end

imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 5)

caption = "Superposition of sines and cosines represented by an epicycle"
plotly()
ImageFile(imgfile, caption)
```

As can be seen, even a somewhat simple combination can produce complicated graphs (a fact known to [Ptolemy](https://en.wikipedia.org/wiki/Deferent_and_epicycle)) . How complicated can such a graph get? This won't be answered here, but for fun enjoy this video produced by the same technique using more moving parts from the [`Javis.jl`](https://github.com/Wikunia/Javis.jl/blob/master/examples/fourier.jl) package:


```{julia}
#| echo: false
txt ="""
<iframe width="560" height="315" src="https://www.youtube.com/embed/rrmx2Q3sO1Y" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope;  picture-in-picture" allowfullscreen></iframe>
"""

HTMLoutput(txt; centered=true, caption="Julia logo animated")
```

### Functions using degrees


Trigonometric function are functions of angles which have two common descriptions: in terms of degrees or radians. Degrees are common when right triangles are considered, radians much more common in general, as the relationship with arc-length holds in that $r\theta = l$, where $r$ is the radius of a circle and $l$ the length of the arc formed by angle $\theta$.


The two are related, as a circle has both  $2\pi$ radians and $360$ degrees. So to convert from degrees into radians it takes multiplying by $2\pi/360$ and to convert from radians to degrees it takes multiplying by $360/(2\pi)$. The `deg2rad` and `rad2deg` functions are available for this task.


In `Julia`, the functions `sind`, `cosd`, `tand`, `cscd`, `secd`, and `cotd` are available to simplify the task of composing the two operations (that is `sin(deg2rad(x))` is the essentially same as `sind(x)`).


## The sum-and-difference formulas


Consider the point on the unit circle $(x,y) = (\cos(\theta), \sin(\theta))$. In terms of $(x,y)$ (or $\theta$) is there a way to represent the angle found by rotating an additional $\theta$, that is what is $(\cos(2\theta), \sin(2\theta))$?


More generally, suppose we have two angles $\alpha$ and $\beta$, can we represent the values of $(\cos(\alpha + \beta), \sin(\alpha + \beta))$ using the values just involving $\beta$ and $\alpha$ separately? The sum formulas express the sine and cosine of $\alpha + \beta$ in terms of the sines and cosines of $\alpha$ and $\beta$. We show variations on the basic decomposition of a right triangle using sine and cosine to illustrate the resulting formula. According to [Wikipedia](https://en.wikipedia.org/wiki/Trigonometric_functions#Identities) this geometric derivation  has ideas that date to Ptolemy.



Suppose both $\alpha$ and $\beta$ are positive with $\alpha + \beta \leq \pi/2$. Then using right triangle geometry we can associate the sine and cosine of $\alpha + \beta$ with distances in this figure:

```{julia}
#| echo: false

using Plots, LaTeXStrings

# two angles
α = pi/5
β = pi/6

## points
A = (0,0)
B = (cos(α + β), 0)
C = (cos(α)*cos(β), 0)
D = (cos(α + β), sin(α)cos(β))
E = (cos(α)*cos(β), sin(α)cos(β))
F = (cos(α + β), sin(α + β))

color1 = :royalblue
color2 = :forestgreen
color3 = :brown3
color4 = :mediumorchid2
canvas() = plot(axis=([],false), legend=false, aspect_ratio=:equal)
p1 = canvas()
plot!(Shape([A,B,F]), fill=(color4, 0.15))

r = 0.10
dae = sqrt(sum((A.-E).^2))
daf = sqrt(sum((A.-F).^2))
dbc = sqrt(sum((B.-C).^2))
dce = sqrt(sum((C.-E).^2))
def = sqrt(sum((E.-F).^2))
dde = sqrt(sum((D.-E).^2))
ddf = sqrt(sum((D.-F).^2))
Δ = 0.0

alphabeta = (r*cos(α/2 + β/2), r*sin(α/2 + β/2),
             text("α + β",:hcenter; rotation=pi/2))
cosαβ = (B[1]/2, 0, text("cos(α + β)", :top))
sinαβ = (B[1], F[2]/2, text("sin(α + β)"))

txtpoints = (
    one = (F[1]/2, F[2]/2, "1",:right),
    beta=(r*cos(α + β/2), r*sin(α + β/2),
          text("β", :hcenter)),
    alpha = (r*cos(α/2), r*sin(α/2),
             text("α",:hcenter)),
    alphaa = (F[1] + r*sin(α/2), F[2] - r*cos(α/2) ,
              text("α"),:hcenter),
    cosβ = (dae/2*cos(α),dae/2*sin(α) + Δ,
            text("cos(β)",:hcenter)),
    sinβ = (B[1] + dbc/2 + Δ/2, D[2] + ddf/2 + Δ/2,
            text("sin(β)",:bottom)),
    cosαcosβ = (C[1]/2, 0 - Δ, text("cos(α)cos(β)", :top)),
    sinαcosβ = (cos(α)*cos(β) - 0.1, dce/2 ,
                text("sin(α)cos(β)", :hcenter)),
    cosαsinβ = (D[1] - Δ, D[2] +  ddf/2 ,
                text("cos(α)sin(β)", :top)),
    sinαsinβ = (D[1] + dde/2, D[2] + Δ ,
                text("sin(α)sin(β)", :top)),
)

# Plot 1
p1 = canvas()
plot!(Shape([A,B,F]), fill=(color4, 0.15))
annotate!([txtpoints[:one], alphabeta, cosαβ, sinαβ])
plot!([A,B,F,A]; line=(5,:red, 0.25))


# Plot 2
p2 = canvas()
plot!(Shape([A,E,F]), fill=(color1, 0.15))
plot!([A,B,F,A]; line=(5,:red, 0.25))
annotate!(map(s ->getindex(txtpoints,s), [:one, :cosβ, :sinβ, :beta]))


# Plot 3
p3 = canvas()
plot!(Shape([A,E,F]), fill=(color1, 0.15))
plot!(Shape([A,C,E]), fill=(color2, 0.15))
annotate!(map(s ->getindex(txtpoints,s), [:alpha, :cosβ, :cosαcosβ, :sinαcosβ]))
plot!([A,B,F,A]; line=(5,:red, 0.25))
annotate!([txtpoints[:beta]])

# Plot 4
p4 = canvas()
plot!(Shape([A,E,D, F]), fill=(color1, 0.15))
plot!(Shape([A,C,E]), fill=(color2, 0.15))
plot!(Shape([D,E,F]), fill=(color3, 0.15))
annotate!(map(s ->getindex(txtpoints,s), [:alphaa, :sinβ, :sinαsinβ, :cosαsinβ]))
plot!([A,B,F,A]; line=(5,:red, 0.25))

# Plot 5
p5 = canvas()
plot!(Shape([A,E,D, F]), fill=(color1, 0.15))
plot!(Shape([A,C,E]), fill=(color2, 0.15))
plot!(Shape([D,E,F]), fill=(color3, 0.15))
plot!(Shape([F,B]), fill=(:black, 0.35))
annotate!(map(s ->getindex(txtpoints,s), collect(keys(txtpoints))))


p1
```

Another right triangle with hypotenuse of length $1$ can be made by isolating the angle $\beta$, as below:

```{julia}
#| echo: false
p2
```


We can make two more right triangles one with hypotenuse $\cos(\beta)$ and one with hypotenuse $\sin(\beta)$; each having an angle $\alpha$, the latter using some geometry, for which we can apply right-triangle trigonometry to find the length of their sides.

```{julia}
#| echo: false
plot(p3, p4)
```

From the left figure and the initial triangle, by comparing the lengths along the $x$ direction, we can see the decomposition:

$$
\cos(\alpha)\cos(\beta) = \cos(\alpha + \beta) + \sin(\alpha)\sin(\beta)
$$

Similarly, this relationship comes from considering the vertical lengths:

$$
\sin(\alpha+\beta) = \sin(\alpha)\cos(\beta) + \cos(\alpha)\sin(\beta)
$$

These lead to:

::: {.callout-note icon=false}
## The *sum* formulas for sine and cosine

$$
\begin{align*}
\sin(\alpha+\beta) &= \sin(\alpha)\cos(\beta) + \cos(\alpha)\sin(\beta) \\
\cos(\alpha + \beta) &= \cos(\alpha)\cos(\beta) - \sin(\alpha)\sin(\beta)
\end{align*}
$$
:::

Taking $\alpha = \beta$ we immediately get

::: {.callout-note icon=false}
## The "double-angle" formulas
$$
\begin{align*}
\sin(2\alpha) &= 2\sin(\alpha)\cos(\alpha)\\
\cos(2\alpha) &= \cos^2(\alpha) - \sin^2(\alpha).
\end{align*}
$$
:::

The latter looks like the Pythagorean identify, but has a minus sign. In fact, the Pythagorean identify is often used to rewrite this, for example $\cos(2\alpha) = 2\cos^2(\alpha) - 1$ or $1 - 2\sin^2(\alpha)$.


Applying the above with $\alpha = \beta/2$, we get that $\cos(\beta) = 2\cos^2(\beta/2) -1$. Similarly, using the Pythagorean identity a formula for sine can be done; when rearranged these yield the "half-angle" formulas:

::: {.callout-note icon=false}
## The "half-angle" formula
$$
\begin{align*}
\sin^2(\frac{\beta}{2}) &= \frac{1 - \cos(\beta)}{2}\\
\cos^2(\frac{\beta}{2}) &= \frac{1 + \cos(\beta)}{2}
\end{align*}
$$
:::

##### Example


Consider the expressions $\cos((n+1)\theta)$ and $\cos((n-1)\theta)$. These can be re-expressed as:


$$
\begin{align*}
\cos((n+1)\theta) &= \cos(n\theta + \theta) = \cos(n\theta) \cos(\theta) - \sin(n\theta)\sin(\theta), \text{ and}\\
\cos((n-1)\theta) &= \cos(n\theta - \theta) = \cos(n\theta) \cos(-\theta) - \sin(n\theta)\sin(-\theta).
\end{align*}
$$


But $\cos(-\theta) = \cos(\theta)$, whereas $\sin(-\theta) = -\sin(\theta)$. Using this, we add the two formulas above to get:


$$
\cos((n+1)\theta) = 2 \cos(\theta) \cos(n\theta)  - \cos((n-1)\theta).
$$

That is the angle for a multiple of $n+1$ can be expressed in terms of the angle with a multiple of $n$ and $n-1$. This can be used recursively to find expressions for $\cos(n\theta)$ in terms of polynomials in $\cos(\theta)$.


## Inverse trigonometric functions


The  trigonometric functions are all periodic. In particular they are not monotonic over their entire domain. This means there is no *inverse* function applicable. However, by restricting the domain to where the functions are monotonic, inverse functions can be defined:


  * For $\sin(x)$, the restricted domain of $[-\pi/2, \pi/2]$ allows for the arcsine function to be defined. In `Julia` this is implemented with `asin`.
  * For $\cos(x)$, the restricted domain of $[0,\pi]$ allows for the arccosine function to be defined. In `Julia` this is implemented with `acos`.
  * For $\tan(x)$, the restricted domain of $(-\pi/2, \pi/2)$ allows for the arctangent function to be defined. In `Julia` this is implemented with `atan`.


For example, the arcsine function is defined for $-1 \leq x \leq 1$ and has a range of $-\pi/2$ to $\pi/2$:


```{julia}
plot(asin, -1, 1)
```

The arctangent has domain of all real $x$. It has shape given by:


```{julia}
plot(atan, -10, 10)
```

The horizontal asymptotes are $y=\pi/2$ and $y=-\pi/2$.


### Implications of a restricted domain


Notice that $\sin(\arcsin(x)) = x$ for any $x$ in $[-1,1]$, but, of course,  not for all $x$, as the output of the sine function can't be arbitrarily large.


However, $\arcsin(\sin(x))$ is defined for all $x$, but only equals $x$ when $x$ is in $[-\pi/2, \pi/2]$. The output, or range, of the $\arcsin$ function is restricted to that interval.


This can be limiting at times. A common case is to find the angle in $[0, 2\pi)$ corresponding to a point $(x,y)$. In the simplest case (the first and fourth quadrants) this is just given by $\arctan(y/x)$. But with some work, the correct angle can be found for any pair $(x,y)$. As this is a common desire, the `atan` function with two arguments, `atan(y,x)`, is available. This function returns a value in $(-\pi, \pi]$.


For example, this will not give back $\theta$ without more work to identify the quadrant:


```{julia}
theta = 3pi/4                     # 2.35619...
x,y = (cos(theta), sin(theta))    # -0.7071..., 0.7071...
atan(y/x)
```

But,


```{julia}
atan(y, x)
```

##### Example


A (white) light shining through a [prism](http://tinyurl.com/y8sczg4t) will be deflected depending on the material of the prism and the angles involved (refer to the link for a figure). The relationship can be analyzed by tracing a ray through the figure and utilizing Snell's law. If the prism has index of refraction $n$ then the ray will deflect by an amount $\delta$ that depends on the angle, $\alpha$ of the prism and the initial angle ($\theta_0$) according to:


$$
\delta = \theta_0 - \alpha  + \arcsin(n \sin(\alpha - \arcsin(\frac{1}{n}\sin(\theta_0)))).
$$

If $n=1.5$ (glass), $\alpha = \pi/3$ and $\theta_0=\pi/6$, find the deflection (in radians).


We have:


```{julia}
#| hold: true
n, alpha, theta0 = 1.5, pi/3, pi/6
delta = theta0 - alpha + asin(n * sin(alpha - asin(sin(theta0)/n)))
```

For small $\theta_0$ and $\alpha$ the deviation is approximated by $(n-1)\alpha$. Compare this approximation to the actual value when $\theta_0 = \pi/10$ and $\alpha=\pi/15$.


We have:


```{julia}
#| hold: true
n, alpha, theta0 = 1.5, pi/15, pi/10
delta = theta0 - alpha + asin(n * sin(alpha - asin(sin(theta0)/n)))
delta, (n-1)*alpha
```

The approximation error is about $2.7$ percent.


##### Example


The AMS has an interesting column on [rainbows](http://www.ams.org/publicoutreach/feature-column/fcarc-rainbows) the start of which uses some formulas from the previous example. Click through to see a ray of light passing through a spherical drop of water, as analyzed by Descartes. The deflection of the ray occurs when the incident light hits the drop of water, then there is an *internal* deflection of the light, and finally when the light leaves, there is another deflection. The total deflection (in radians) is $d = (i-r) + (\pi - 2r) + (i-r) = \pi + 2i - 4r$. However, the incident angle $i$ and the refracted angle $r$ are related by Snell's law: $\sin(i) = n \sin(r)$. The value $n$ is the index of refraction and is $4/3$ for water. (It was $3/2$ for glass in the previous example.) This gives


$$
d = \pi + 2i - 4 \arcsin(\frac{1}{n} \sin(i)).
$$

Graphing this for incident angles between $0$ and $\pi/2$ we have:


```{julia}
#| hold: true
n = 4/3
d(i) = pi + 2i - 4 * asin(sin(i)/n)
plot(d, 0, pi/2)
```

Descartes was interested in the minimum value of this graph, as it relates to where the light concentrates. This is roughly at $1$ radian or about $57$ degrees:


```{julia}
rad2deg(1.0)
```

(Using calculus it can be seen to be $\arccos(((n^2-1)/3)^{1/2})$.)


##### Example: The Chebyshev Polynomials


Consider again this equation derived with the sum-and-difference formula:


$$
\cos((n+1)\theta) = 2 \cos(\theta) \cos(n\theta)  - \cos((n-1)\theta).
$$

Let $T_n(x) = \cos(n \arccos(x))$. Note $T_1(x) = x$. By identifying $\theta$ with $\arccos(x)$ for $-1 \leq x \leq 1$, we get a relation between these functions:


$$
T_{n+1}(x) = 2x T_n(x) - T_{n-1}(x).
$$

We can simplify a few of the above : For example, when $n=0$ we see immediately that $T_0(x) = 1$, the constant function. We used above that for $n=1$ we get $T_1(x) = \cos(\arccos(x)) = x$. Things get more interesting as we get bigger $n$, for example using the equation above we get $T_2(x) = 2xT_1(x) - T_0(x) = 2x\cdot x - 1 = 2x^2 - 1$. Continuing, we'd get $T_3(x) = 2 x T_2(x) - T_1(x) = 2x(2x^2 - 1) - x = 4x^3 -3x$.


A few things become clear from the above two representations:


  * Starting from $T_0(x) = 1$ and $T_1(x)=x$ and using the recursive definition of $T_{n+1}$ we get a family of polynomials where $T_n(x)$ is a degree $n$ polynomial. These are defined for all $x$, not just $-1 \leq x \leq 1$.
  * Using the initial definition, we see that the zeros of $T_n(x)$ all occur within $[-1,1]$ and happen when $n\arccos(x) = k\pi + \pi/2$, or $x=\cos((2k+1)/n \cdot \pi/2)$ for $k=0, 1, \dots, n-1$.


Other properties of this polynomial family are not at all obvious. One is that amongst all polynomials of degree $n$ with roots in $[-1,1]$, $T_n(x)$ will be the smallest in magnitude (after we divide by the leading coefficient to make all polynomials considered to be monic). We check this for one case. Take $n=4$, then we have: $T_4(x) = 8x^4 - 8x^2 + 1$. Compare this with $q(x) = (x+3/5)(x+1/5)(x-1/5)(x-3/5)$ (evenly spaced zeros):


```{julia}
T4(x) = (8x^4 - 8x^2 + 1) / 8
q(x) = (x+3/5)*(x+1/5)*(x-1/5)*(x-3/5)
plot(abs ∘ T4, -1,1, label="|T₄|")
plot!(abs ∘ q, -1,1, label="|q|")
```

## Hyperbolic trigonometric functions


Related to the trigonometric functions are the hyperbolic trigonometric functions. Instead of associating a point $(x,y)$ on the unit circle with an angle $\theta,$ we associate a point $(x,y)$ on the unit *hyperbola* ($x^2 - y^2 = 1$). We define the hyperbolic sine ($\sinh$) and hyperbolic cosine ($\cosh$) through $(\cosh(\theta), \sinh(\theta)) = (x,y)$.


```{julia}
#| echo: false
let
    ## inspired by https://en.wikipedia.org/wiki/Hyperbolic_function
    # y^2 = x^2 - 1
    top(x) = sqrt(x^2 - 1)

    p = plot(; legend=false, aspect_ratio=:equal)

    x₀ = 2
    xs = range(1, x₀, length=100)
    ys = top.(xs)
    plot!(p, xs,  ys, color=:red)
    plot!(p, xs, -ys, color=:red)

    xs = -reverse(xs)
    ys = top.(xs)
    plot!(p, xs,  ys, color=:red)
    plot!(p, xs, -ys, color=:red)

    xs = range(-x₀, x₀, length=3)
    plot!(p, xs, xs, linestyle=:dash, color=:blue)
    plot!(p, xs, -xs, linestyle=:dash, color=:blue)

    a = 1.2
    plot!(p, [0,cosh(a)], [sinh(a), sinh(a)])
    annotate!(p, [(sinh(a)/2, sinh(a)+0.25,"cosh(a)")])
    plot!(p, [cosh(a),cosh(a)], [sinh(a), 0])
    annotate!(p, [(sinh(a) + 1, cosh(a)/2,"sinh(a)")])
    scatter!(p, [cosh(a)], [sinh(a)], markersize=5)


    ts = range(0, a, length=100)
    xs′ = cosh.(ts)
    ys′ = sinh.(ts)

    xs = [0, 1, xs′..., 0]
    ys = [0, 0, ys′..., 0]
    plot!(p, xs, ys, fillcolor=:red, fill=true, alpha=.3)

    p
end
```

These values are more commonly expressed using the exponential function as:


$$
\begin{align*}
\sinh(x) &= \frac{e^x - e^{-x}}{2}\\
\cosh(x) &= \frac{e^x + e^{-x}}{2}.
\end{align*}
$$


The hyperbolic tangent is then the ratio of $\sinh$ and $\cosh$. As well, three inverse hyperbolic functions can be defined.


The `Julia` functions to compute these values are named `sinh`, `cosh`, and `tanh`.


## Questions


###### Question


What is bigger $\sin(1.23456)$ or $\cos(6.54321)$?


```{julia}
#| hold: true
#| echo: false
a = sin(1.23456) > cos(6.54321)
choices = [raw"``\sin(1.23456)``", raw"``\cos(6.54321)``"]
answ = a ? 1 : 2
radioq(choices, answ, keep_order=true)
```

###### Question


Let $x=\pi/4$. What is bigger $\cos(x)$ or $x$?


```{julia}
#| hold: true
#| echo: false
x = pi/4
a = cos(x) > x
choices = [raw"``\cos(x)``", "``x``"]
answ = a ? 1 : 2
radioq(choices, answ, keep_order=true)
```

###### Question


The cosine function is a simple transformation of the sine function. Which one?


```{julia}
#| hold: true
#| echo: false
choices = [
raw"``\cos(x) = \sin(x - \pi/2)``",
raw"``\cos(x) = \sin(x + \pi/2)``",
raw"``\cos(x) = \pi/2 \cdot \sin(x)``"]
answ = 2
radioq(choices, answ)
```

###### Question


Graph the secant function. The vertical asymptotes are at?


```{julia}
#| hold: true
#| echo: false
choices = [
L"The values $k\pi$ for $k$ in $\dots, -2, -1, 0, 1, 2, \dots$",
L"The values $\pi/2 + k\pi$ for $k$ in $\dots, -2, -1, 0, 1, 2, \dots$",
L"The values $2k\pi$ for $k$ in $\dots, -2, -1, 0, 1, 2, \dots$"]
answ = 2
radioq(choices, answ, keep_order=true)
```

###### Question


A formula due to [Bhaskara I](http://tinyurl.com/k89ux5q) dates to around 650AD and gives a rational function approximation to the sine function. In degrees, we have


$$
\sin(x^\circ) \approx \frac{4x(180-x)}{40500 - x(180-x)}, \quad 0 \leq x \leq 180.
$$

Plot both functions over $[0, 180]$. What is the maximum difference between the two to two decimal points? (You may need to plot the difference of the functions to read off an approximate answer.)


```{julia}
#| hold: true
#| echo: false
numericq(.0015, .01)
```

###### Question


Solve the following equation for a value of $x$ using `acos`:


$$
\cos(x/3) = 1/3.
$$

```{julia}
#| hold: true
#| echo: false
val = 3*acos(1/3)
numericq(val)
```

###### Question


For any positive integer $n$ the equation $\cos(x) - nx = 0$ has a solution in $[0, \pi/2].$ Graphically estimate the value when $n=10.$


```{julia}
#| hold: true
#| echo: false
val = 0.1
numericq(val)
```

###### Question


The sine function is an *odd* function.


  * The hyperbolic sine is:


```{julia}
#| hold: true
#| echo: false
choices = ["odd", "even", "neither"]
answ = 1
radioq(choices, answ, keep_order=true)
```

  * The hyperbolic cosine is:


```{julia}
#| hold: true
#| echo: false
choices = ["odd", "even", "neither"]
answ = 2
radioq(choices, answ, keep_order=true)
```

  * The hyperbolic tangent is:


```{julia}
#| hold: true
#| echo: false
choices = ["odd", "even", "neither"]
answ = 1
radioq(choices, answ, keep_order=true)
```

###### Question

The function `f(x) = x * tanh(exp(x))` has a shape akin to `max(0,x)` but is smoooth. Graphically finds its smallest $y$ value.

```{julia}
#| echo: false
f(x) = x * tanh(exp(x))
val = -0.35328577784821125
numericq(val, 1e-1)
```


###### Question


The hyperbolic sine satisfies this formula:


$$
\sinh(\theta + \beta) = \sinh(\theta)\cosh(\beta) + \sinh(\beta)\cosh(\theta).
$$

Is this identical to the pattern for the regular sine function?


```{julia}
#| hold: true
#| echo: false
yesnoq(true)
```

The hyperbolic cosine satisfies this formula:


$$
\cosh(\theta + \beta) = \cosh(\theta)\cosh(\beta) + \sinh(\beta)\sinh(\theta).
$$

Is this identical to the pattern for the regular sine function?


```{julia}
#| hold: true
#| echo: false
yesnoq(false)
```

