Begin by loading our packages for plotting and zero finding:
using Plots
using Root
using ForwardDiff
D(f, k=1) = k > 1 ? D(D(f),k-1) : x -> ForwardDiff.derivative(f, float(x))
using QuadGK
Parametric plots are ones where the points plotted are parametrized by $t$: $(g(t), f(t))$. The plot
function will produce parametric plots when called with two function objects, plot(g,f, a, b)
.
x(theta) = sin(theta)
y(theta) = cos(3theta)
plot(x, y, 0, 2pi)
This can be used to plot functions in polar representation by converting to Cartesian coordinates:
but it is easier to let the graphing function do that work. Here is a specialized function:
function polar(r, a, b)
plot(t -> r(t)*cos(t), t->r(t)*sin(t), a, b)
end
polar (generic function with 1 method)
Which is used as follows:
r(theta) = 1 - cos(theta) # a cardiod
polar(r, 0, 2pi)
If $c(t) = (x(t), y(t))$ parametrically describes a curve with differentiable functions, then the tangent line $t$ is given by: $dy/dx = y'(t)/x'(t)$. Using D
we can compute this quickly.
For example, the curve $x(t) = \cos^3(t)$, $y(t) = \sin^3(t)$ is an astroid. Its tangent line at $\theta = \pi/3$ is found with
x(t) = cos(t)^3
y(t) = sin(t)^3
t0 = pi/3
m = D(y)(t0) / D(x)(t0)
(x(t0), y(t0)), m # return point and slope for y = y0 + m (x-x0)
((0.12500000000000008, 0.6495190528383289), -1.7320508075688767)
The area bounded by a curve in polar form given by $r(\theta)$ between the rays $\theta=a$ and $\theta=b$ is
We can check this works for a known figure. When $r=1$, $a=0$, and $b=2\pi$ the area is that of a circle of radius 1, or $\pi$. Here we integrate to compare:
r(theta) = 1
a, b = 0, 2pi
a, err = quadgk(t -> (1/2)*r(t)^2, a, b)
a - pi
0.0
A bullet fired from a gun follows a trajectory given by
Let $a=500$ and $b=300$. Show graphically or analytically that the bullet leaves the gun at an angle $\theta=\tan^{-1}(b/a)$ and lands at a distance $ab/16$ from the origin.
A cycloid is the shape made by tracking a fixed point along a wheel as it moves away in a direction. If the wheel has radius $R$ and the point is at the bottom of the wheel at $t=0$ then these functions parameterize the curve:
Verify the following property algebraically. The tangent line at time $t$ wil go through the top point of the circle of radius $R$ with center at $(R\cdot t, R)$, that is, it contains the point $(R\cdot t, 2R)$.
Assuming $R=1$, prove this analytically.
A Bezier Curve is a parametric curve which has been found useful in computer graphics. The cubic case is described on Wikipedia by
Four points P0, P1, P2 and P3 in the plane or in higher-dimensional
space define a cubic Bézier curve. The curve starts at P0 going toward P1 and arrives at P3 coming from the direction of P2. Usually, it will not pass through P1 or P2; these points are only there to provide directional information. The distance between P0 and P1 determines "how long" the curve moves into direction P2 before turning towards P3.
If we write $p_i = (a_i, b_i)$ for $i = 0, 1, 2, 3$ then the parametric curve is given by:
where $t$ is in $[0,1]$.
If we input the points as pairs like (a,b)
into julia
we can construct and plot the functions via:
function bezier(ps...)
(t -> sum([ps[i+1][1] * t^i*(1-t)^(3-i) for i in 0:3]),
t -> sum([ps[i+1][2] * t^i*(1-t)^(3-i) for i in 0:3]))
end
x2, y2 =bezier((0,0),(1,-1),(2,-2),(3,0))
plot(x2, y2, 0, 1)
Find a group of four points where the graph is convex (always concave up or down). Find a group of four points where the graph is not convex (it has part of it concave up and some concave down).
The tractrix with parameter $l > 0$ is described parametrically by
At a given value $t$, the tangent line to the curve intersects the $x$-axis. Show that the distance of this line segment is $l$ for t = rand()
(that is a random value of $t$ in $(0,1)$).
Plot the polar curve described by
for $a=1$, $b=2$. What is the shape of the curve?
Plot the polar curve given by $r = \sin(\theta) + \cos(3\theta)$. What more closely describes the shape: a circle, an ellipse, or something odder.
The polar curve given by $r(\theta) = \theta^2 + 4\theta$ intersects the $y$-axis when $\theta = 0$ and When $\theta = \pi/2$. Find the bounded area between the curve and the $y$-axis.
The cardiod $r(\theta) = 1 - \cos(\theta)$ has some of its area to the right of the $y$ axis. The total area is given by:
r(t) = 1 - cos(t)
quadgk(t -> 1/2 * r(t)^2, 0, 2pi)[1]
4.712388980384691
What percent of the area is to the right of the $y$ axis?
Find the area of one loop of the lemniscate with equation $r = \cos(2\theta)$. (Finding the right limits is all the battle.)
Plotting the polar curve $r(\theta) = \theta \cdot\sin(\theta)$ over $[0, k\pi]$ produces $k$ nested shapes ($k$ is a positive integer). Find the area of the smallest one.