To recover the Cartesian coordinates from the pair $(r,\theta)$, we have these formulas from [right](http://en.wikipedia.org/wiki/Polar_coordinate_system#Converting_between_polar_and_Cartesian_coordinates) triangle geometry:
```math
x = r \cos(\theta),~ y = r \sin(\theta).
```
Each point $(x,y)$ corresponds to several possible values of
$(r,\theta)$, as any integer multiple of $2\pi$ added to $\theta$ will
describe the same point. Except for the origin, there is only one pair
when we restrict to $r > 0$ and $0 \leq \theta < 2\pi$.
For values in the first and fourth quadrants (the range of
$\tan^{-1}(x)$), we have:
```math
r = \sqrt{x^2 + y^2},~ \theta=\tan^{-1}(y/x).
```
For the other two quadrants, the signs of $y$ and $x$ must be
considered. This is done with the function `atan` when two arguments are used.
For example, $(-3, 4)$ would have polar coordinates:
```julia;
x,y = -3, 4
rad, theta = sqrt(x^2 + y^2), atan(y, x)
```
And reversing
```julia;
rad*cos(theta), rad*sin(theta)
```
This figure illustrates:
```julia; hold=true; echo=false
p = plot([-5,5], [0,0], color=:blue, legend=false)
plot!([0,0], [-5,5], color=:blue)
plot!([-3,0], [4,0])
scatter!([-3], [4])
title!("(-3,4) Cartesian or (5, 2.21...) polar")
p
```
The case where $r < 0$ is handled by going ``180`` degrees in the opposite direction, in other
words the point $(r, \theta)$ can be described as well by $(-r,\theta+\pi)$.
## Parameterizing curves using polar coordinates
If $r=r(\theta)$, then the parameterized curve $(r(\theta), \theta)$
is just the set of points generated as $\theta$ ranges over some set
of values. There are many examples of parameterized curves that
simplify what might be a complicated presentation in Cartesian coordinates.
For example, a circle has the form $x^2 + y^2 = R^2$. Whereas
parameterized by polar coordinates it is just $r(\theta) = R$, or a
constant function.
The circle centered at $(r_0, \gamma)$ (in polar coordinates) with
radius $R$ has a more involved description in polar coordinates:
The case where $r_0 > R$ will not be defined for all values of $\theta$, only when $|\sin(\theta-\gamma)| \leq R/r_0$.
#### Examples
The `Plots.jl` package provides a means to visualize polar plots through `plot(thetas, rs, proj=:polar)`. For example, to plot a circe with $r_0=1/2$ and $\gamma=\pi/6$ we would have:
To avoid having to create values for $\theta$ and values for $r$, the `CalculusWithJulia` package provides a helper function, `plot_polar`. To distinguish it from other functions provided by `Plots`, the calling pattern is different. It specifies an interval to plot over by `a..b` and puts that first (this notation for closed intervals is from `IntervalSets`), followed by `r`. Other keyword arguments are passed onto a `plot` call.
We will use this in the following, as the graphs are a bit more familiar and the calling pattern similar to how we have plotted functions.
As `Plots` will make a parametric plot when called as `plot(function, function, a,b)`, the above
function creates two such functions using the relationship $x=r\cos(\theta)$ and $y=r\sin(\theta)$.
Using `plot_polar`, we can plot circles with the following. We have to be a bit careful for the general circle, as when the center is farther away from the origin that the radius ($R$), then not all angles will be acceptable and there are two functions needed to describe the radius, as this comes from a quadratic equation and both the "plus" and "minus" terms are used.
```julia; hold=true
R=4; r(t) = R;
function plot_general_circle!(r0, gamma, R)
# law of cosines has if gamma=0, |theta| <= asin(R/r0)
There are many interesting examples of curves described by polar coordinates. An interesting [compilation](http://www-history.mcs.st-and.ac.uk/Curves/Curves.html) of famous curves is found at the MacTutor History of Mathematics archive, many of which have formulas in polar coordinates.
##### Example
The [rhodenea](http://www-history.mcs.st-and.ac.uk/Curves/Rhodonea.html) curve has
```math
r(\theta) = a \sin(k\theta)
```
```julia; hold=true
a, k = 4, 5
r(theta) = a * sin(k * theta)
plot_polar(0..pi, r)
```
This graph has radius $0$ whenever $\sin(k\theta) = 0$ or $k\theta
=n\pi$. Solving means that it is $0$ at integer multiples of
$\pi/k$. In the above, with $k=5$, there will $5$ zeroes in
$[0,\pi]$. The entire curve is traced out over this interval, the
values from $\pi$ to $2\pi$ yield negative value of $r$, so are
related to values within $0$ to $\pi$ via the relation $(r,\pi
+\theta) = (-r, \theta)$.
##### Example
The [folium](http://www-history.mcs.st-and.ac.uk/Curves/Folium.html)
is a somewhat similar looking curve, but has this description:
The folium has radial part $0$ when $\cos(\theta) = 0$ or
$\sin(2\theta) = b/4a$. This could be used to find out what values
correspond to which loop. For our choice of $a$ and $b$ this gives $\pi/2$, $3\pi/2$ or, as
$b/4a = 1/8$, when $\sin(2\theta) = 1/8$ which happens at
$a_0=\sin^{-1}(1/8)/2=0.0626...$ and $\pi/2 - a_0$, $\pi+a_0$ and $3\pi/2 - a_0$. The first folium can be plotted with:
```julia;
𝒂0 = (1/2) * asin(1/8)
plot_polar(𝒂0..(pi/2-𝒂0), 𝒓)
```
The second - which is too small to appear in the initial plot without zooming in - with
```julia;
plot_polar((pi/2 - 𝒂0)..(pi/2), 𝒓)
```
The third with
```julia;
plot_polar((pi/2)..(pi + 𝒂0), 𝒓)
```
The plot repeats from there, so the initial plot could have been made over $[0, \pi + a_0]$.
##### Example
The [Limacon of Pascal](http://www-history.mcs.st-and.ac.uk/Curves/Limacon.html) has
```math
r(\theta) = b + 2a\cos(\theta)
```
```julia; hold=true
a,b = 4, 2
r(theta) = b + 2a*cos(theta)
plot_polar(0..2pi, r)
```
##### Example
Some curves require a longer parameterization, such as this where we
plot over $[0, 8\pi]$ so that the cosine term can range over an entire
half period:
```julia; hold=true
r(theta) = sqrt(abs(cos(theta/8)))
plot_polar(0..8pi, r)
```
## Area of polar graphs
Consider the [cardioid](http://www-history.mcs.st-and.ac.uk/Curves/Cardioid.html) described by $r(\theta) = 2(1 + \cos(\theta))$:
```julia; hold=true
r(theta) = 2(1 + cos(theta))
plot_polar(0..2pi, r)
```
How much area is contained in the graph?
In some cases it might be possible to translate back into Cartesian
coordinates and compute from there. In practice, this is not usually the best
solution.
The area can be approximated by wedges (not rectangles). For example, here we see that the area over a given angle is well approximated by the wedge for each of the sectors:
```julia; hold=true; echo=false
r(theta) = 1/(1 + (1/3)cos(theta))
p = plot_polar(0..pi/2, r, legend=false, linewidth=3, aspect_ratio=:equal)
t0, t1, t2, t3 = collect(range(pi/12, pi/2 - pi/12, length=4))
Imagine we have $a < b$ and a partition $a=t_0 < t_1 < \cdots < t_n =
b$. Let $\phi_i = (1/2)(t_{i-1} + t_{i})$ be the midpoint.
Then the wedge of radius $r(\phi_i)$ with angle between $t_{i-1}$ and $t_i$ will have area $\pi r(\phi_i)^2 (t_i-t_{i-1}) / (2\pi) = (1/2) r(\phi_i)(t_i-t_{i-1})$, the ratio $(t_i-t_{i-1}) / (2\pi)$ being the angle to the total angle of a circle.
Summing the area of these wedges
over the partition gives a Riemann sum approximation for the integral $(1/2)\int_a^b
r(\theta)^2 d\theta$. This limit of this sum defines the area in polar coordinates.
> *Area of polar regions*. Let $R$ denote the region bounded by the curve $r(\theta)$ and bounded by the rays
> $\theta=a$ and $\theta=b$ with $b-a \leq 2\pi$, then the area of $R$ is given by:
The outer area (including the inner loop) is the integral from $0$ to $\pi/2 + \pi/6$ plus that from $3\pi/2 - \pi/6$ to $2\pi$. These areas are equal, so we double the first:
We test this out on a circle with $r(\theta) = R$, a constant. The
integrand simplifies to just $\sqrt{R^2}$ and the integral is from $0$
to $2\pi$, so the arc length is $2\pi R$, precisely the formula for
the circumference.
##### Example
A cardioid is described by $r(\theta) = 2(1 + \cos(\theta))$. What is the arc length from $0$ to $2\pi$?
The integrand is integrable with antiderivative $4\sqrt{2\cos(\theta) + 2} \cdot \tan(\theta/2)$,
but `SymPy` isn't able to find the integral. Instead we give a numeric answer:
```julia; hold=true
r(theta) = 2*(1 + cos(theta))
quadgk(t -> sqrt(r'(t)^2 + r(t)^2), 0, 2pi)[1]
```
##### Example
The [equiangular](http://www-history.mcs.st-and.ac.uk/Curves/Equiangular.html) spiral has polar representation
```math
r(\theta) = a e^{\theta \cot(b)}
```
With $a=1$ and $b=\pi/4$, find the arc length traced out from $\theta=0$ to $\theta=1$.
```julia; hold=true
a, b = 1, PI/4
@syms θ
r(theta) = a * exp(theta * cot(b))
ds = sqrt(diff(r(θ), θ)^2 + r(θ)^2)
integrate(ds, (θ, 0, 1))
```
##### Example
An Archimedean [spiral](http://en.wikipedia.org/wiki/Archimedean_spiral) is defined in polar form by
```math
r(\theta) = a + b \theta
```
That is, the radius increases linearly. The crossings of the positive $x$ axis occur at $a + b n 2\pi$, so are evenly spaced out by $2\pi b$. These could be a model for such things as coils of materials of uniform thickness.
For example, a roll of toilet paper promises ``1000`` sheets with the
The value `b` gives a value in inches, the latter in millimeters.
## Questions
###### Question
Let $r=3$ and $\theta=\pi/8$. In Cartesian coordinates what is $x$?
```julia; hold=true; echo=false
x,y = 3 * [cos(pi/8), sin(pi/8)]
numericq(x)
```
What is $y$?
```julia; hold=true; echo=false
numericq(y)
```
###### Question
A point in Cartesian coordinates is given by $(-12, -5)$. In has a polar coordinate representation with an angle $\theta$ in $[0,2\pi]$ and $r > 0$. What is $r$?
```julia; hold=true; echo=false
x,y = -12, -5
r1, theta1 = sqrt(x^2 + y^2), atan(y,x)
numericq(r1)
```
What is $\theta$?
```julia; hold=true; echo=false
x,y = -12, -5
r1, theta1 = sqrt(x^2 + y^2), atan(y,x)
numericq(theta1)
```
###### Question
Does $r(\theta) = a \sec(\theta - \gamma)$ describe a line for $0$ when $a=3$ and $\gamma=\pi/4$?
```julia; hold=true; echo=false
yesnoq("yes")
```
If yes, what is the $y$ intercept
```julia; hold=true; echo=false
r(theta) = 3 * sec(theta -pi/4)
val = r(pi/2)
numericq(val)
```
What is slope of the line?
```julia; hold=true; echo=false
r(theta) = 3 * sec(theta -pi/4)
val = (r(pi/2)*sin(pi/2) - r(pi/4)*sin(pi/4)) / (r(pi/2)*cos(pi/2) - r(pi/4)*cos(pi/4))
numericq(val)
```
Does this seem likely: the slope is $-1/\tan(\gamma)$?
```julia; hold=true; echo=false
yesnoq("yes")
```
###### Question
The polar curve $r(\theta) = 2\cos(\theta)$ has tangent lines at most points. This differential representation of the chain rule
Find the area of a lobe of the [eight](http://www-history.mcs.st-and.ac.uk/Curves/Eight.html) curve traced out by $r(\theta) = \cos(2\theta)\sec(\theta)^4$ from $-\pi/4$ to $\pi/4$. Do this numerically.
Find the arc length of a lobe of the [eight](http://www-history.mcs.st-and.ac.uk/Curves/Eight.html) curve traced out by $r(\theta) = \cos(2\theta)\sec(\theta)^4$ from $-\pi/4$ to $\pi/4$. Do this numerically.