Look at the last definition of `A`. The function `A` appears on both sides, though on the left side with one argument and on the right with two. These are two "methods" of a *generic* function, `A`. `Julia` allows multiple definitions for the same name as long as the arguments (their number and type) can disambiguate which to use. In this instance, when one argument is passed in then the last defintion is used (`A(b,h(b))`), whereas if two are passed in, then the method that multiplies both arguments is used. The advantage of multiple dispatch is illustrated: the same concept - area - has one function name, though there may be different ways to compute the area, so there is more than one implementation.
The area is the area of the rectangle plus the area of the half circle ($\pi r^2/2$ with $r=x/2$).
```math
A = xy + \pi(x/2)^2/2
```
In `Julia` this is
```julia;
Aᵣ(x, y) = x*y + pi*(x/2)^2 / 2
```
The perimeter consists of ``3`` sides of the rectangle and the perimeter
of half a circle ($\pi r$, with $r=x/2$):
```math
P = 2y + x + \pi(x/2) = 20
```
We solve for $y$ in the first with $y = (20 - x - \pi(x/2))/2$ so that in `Julia` we have:
```julia;
y(x) = (20 - x - pi * x/2) / 2
```
And then we substitute in `y(x)` for `y` in the area formula through:
```julia;
Aᵣ(x) = Aᵣ(x, y(x))
```
Of course both $x$ and $y$ are non-negative. The latter forces $x$ to
be no more than $x=20/(1+\pi/2)$.
This leaves us the calculus problem of finding an absolute maximum of
a continuous function over the closed interval
$[0, 20/(1+\pi/2)]$. Our theorem tells us this maximum must occur, we
now proceed to find it.
We begin by simply graphing and estimating the values of the maximum and
where it occurs.
```julia;
plot(Aᵣ, 0, 20/(1+pi/2))
```
The naked eye sees that maximum value is somewhere around $27$ and
occurs at $x\approx 5.6$. Clearly from the graph, we know the maximum
value happens at the critical point and there is only one such
critical point.
As reading the maximum from the graph is more difficult than reading a
$0$ of a function, we plot the derivative using our approximate
derivative.
```julia;
plot(Aᵣ', 5.5, 5.7)
```
We confirm that the critical point is around $5.6$.
#### Using `find_zero` to locate critical points.
Rather than zoom in graphically, we now use a root-finding algorithm,
to find a more precise value of the zero of ``A'``. We know that the
maximum will occur at a critical point, a zero of the derivative. The
`find_zero` function from the `Roots` package provides a non-linear
root-finding algorithm based on the bisection method. The only thing
to keep track of is that solving $f'(x) = 0$ means we use the
derivative and not the original function.
We see from the graph that $[0, 20/(1+\pi/2)]$ will provide a bracket, as there is only one relative maximum:
```julia;
x′ = find_zero(Aᵣ', (0, 20/(1+pi/2)))
```
This value is the lone critical point, and in this case gives
the position of the value that will maximize the function. The value and maximum area are then given by:
```julia;
(x′, Aᵣ(x′))
```
(Compare this answer to the previous, is the square the figure of
greatest area for a fixed perimeter, or just the figure amongst all
rectangles? See [Isoperimetric inequality](https://en.wikipedia.org/wiki/Isoperimetric_inequality) for an answer.)
### Using `argmax` to identify where a function is maximized
This value that maximizes a function is sometimes referred to as the
*argmax*, or argument which maximizes the function. In `Julia` the
`argmax(f,domain)` function is defined to "Return a value ``x`` in the
domain of ``f`` for which ``f(x)`` is maximized. If there are multiple
maximal values for ``f(x)`` then the first one will be found." The
domain is some iterable collection. In the mathematical world this
would be an interval ``[a,b]``, but on the computer it is an
approximation, such as is returned by `range` below. Without out
having to take a derivative, as above, but sacrificing some accuracy,
the task of identifying `x` for where `A` is maximum, could be done
with
```julia
argmax(Aᵣ, range(0, 20/(1+pi/2), length=10000))
```
#### A symbolic approach
We could also do the above problem symbolically with the aid of `SymPy`. Here are the steps:
```julia;
@syms 𝒘::real 𝒉::real
𝑨₀ = 𝒘 * 𝒉 + pi * (𝒘/2)^2 / 2
𝑷erim = 2*𝒉 + 𝒘 + pi * 𝒘/2
𝒉₀ = solve(𝑷erim - 20, 𝒉)[1]
𝑨₁ = 𝑨₀(𝒉 => 𝒉₀)
𝒘₀ = solve(diff(𝑨₁,𝒘), 𝒘)[1]
```
We know that `𝒘₀` is the maximum in this example from our previous
work. We shall see soon, that just knowing that the second derivative
is negative at `𝒘₀` would suffice to know this. Here we check that
condition:
```julia;
diff(𝑨₁, 𝒘, 𝒘)(𝒘 => 𝒘₀)
```
As an aside, compare the steps involved above for a symbolic solution to those of previous work for a numeric solution:
```julia; hold=true
Aᵣ(w, h) = w*h + pi*(w/2)^2 / 2
h(w) = (20 - w - pi * w/2) / 2
Aᵣ(w) = A(w, h(w))
find_zero(A', (0, 20/(1+pi/2))) # 40 / (pi + 4)
```
They are similar, except we solved for `h0` symbolically, rather than by hand, when we solved for `h(w)`.
##### Example
```julia; hold=true; echo=false
caption = """
The figure shows a trapezoid inscribed in a circle. By adjusting the point ``P_3 = (x,y)`` on the upper-half circle, the area of the trapezoid changes. What value of ``x`` will produce the maximum area for a given ``r`` (from ``P_4``, which can also be adjusted)? By playing around with different values of ``P_3`` and ``P_4`` the answer can be guessed.
A trapezoid is *inscribed* in the upper-half circle of radius ``r``. The trapezoid is found be connecting the points ``(x,y)`` (in the first quadrant) with ``(r, 0)``, ``(-r,0)``, and ``(-x, y)``. Find the maximum area. (The above figure has ``x=0.75`` and ``r=1``.)
Here the constraint is simply ``r^2 = x^2 + y^2`` with ``x`` and ``y`` being non-negative. The area is then found through the average of the two lengths times the height. Using `height` for `y`, we have:
The answer will occur at a critical point or an endpoint, either $x=0$ or $x=L$.
The structure of `dt` is too complicated for simply calling `solve` to find the critical points. Instead we help `SymPy` out a bit. We are solving an equation of the form $a/b + c/d = 0$. These solutions will also be solutions of $(a/b)^2 - (c/d)^2=0$ or even $a^2d^2 - c^2b^2 = 0$. This follows as solutions to $u+v=0$, also solve $(u+v)\cdot(u-v)=0$, or $u^2 - v^2=0$. Setting $u=a/b$ and $v=c/d$ completes the comparison.
We can get these terms - $a$, $b$, $c$, and $d$ - as follows:
```julia;
t1, t2 = dt.args # the `args` property returns the arguments to the outer function (+ in this case)
```
The equivalent of $a^2d^2 - c^2 b^2$ is found using the generic functions `numerator` and `denominator` to access the numerator and denominator of the fractions:
```julia;
ex = numerator(t1^2)*denominator(t2^2) - denominator(t1^2)*numerator(t2^2)
```
This is a polynomial in the `x` variable of degree $4$, as seen here where the `sympy.Poly` function is used to identify the symbols of the polynomial from the parameters:
[article](http://www.ams.org/samplings/feature-column/fc-2016-05) -- from
which the figure came -- states that "In today's textbooks the problem,
usually involving a river, involves walking along one bank and then
swimming across; this corresponds to setting $g=0$ in l'Hospital's
example, and leads to a quadratic equation." Let's see that case,
which we can get in our notation by taking $b=0$:
```julia;
q = ex(b=>0)
factor(q)
```
We see two terms: one with $x=L$ and another quadratic. For the simple
case $r_0=r_1$, a straight line is the best solution, and this
corresponds to $x=L$, which is clear from the formula above, as we
only have one solution to the following:
```julia;
solve(q(r1=>r0), x)
```
Well, not so fast. We need to check the other endpoint, $x=0$:
```julia;
ta = t(b=>0, r1=>r0)
ta(x=>0), ta(x=>L)
```
The value at $x=L$ is smaller, as $L^2 + a^2 \leq (L+a)^2$. (Well, that was a bit pedantic. The travel rates being identical means the fastest path will also be the shortest path and that is clearly ``x=L`` and not ``x=0``.)
Now, if, say, travel above the line is half as slow as travel along, then $2r_0 = r_1$, and the critical points will be:
```julia;
out = solve(q(r1 => 2r0), x)
```
It is hard to tell which would minimize time without more work. To check a case ($a=1, L=2, r_0=1$) we might have
a critical point, not an endpoint. Then let $M$ go to infinity. In general, for an optimization problem of a continuous function on the interval ``(a,b)`` if the right limit at ``a`` and left limit at ``b`` can be ruled out as candidates, the optimal value must occur at a critical point.)
Find a value in degrees that gives the maximum. (The first task is to
write the area in terms of $\theta$.
```julia; hold=true; echo=false
function Ar(t)
opp = 10 * sin(t)
adj = 10 * cos(t)
2 * opp * adj/2 + opp * 10
end
t = find_zero(Ar', pi/4); ## Has issues with order=8 algorithm, tol > 1e-14 is needed
val = t * 180/pi;
numericq(val, 1e-3)
```
###### Question Non-Norman windows
Suppose our new "Norman" window has half circular tops at the top and bottom? If the perimeter is fixed at $20$ and the dimensions of the rectangle are $x$ for the width and $y$ for the height.
Image number $40$ from l'Hospital's calculus book (the first calculus book). Among all the cones that can be inscribed in a sphere, determine which one has the largest lateral area. (From http://www.ams.org/samplings/feature-column/fc-2016-05)
"""
ImageFile(:derivatives, imgfile, caption)
```
The figure above poses a problem about cones in spheres, which can be reduced to a two-dimensional problem. Take a sphere of radius $r=1$, and imagine a secant line of length $l$ connecting $(-r, 0)$ to another point $(x,y)$ with $y>0$. Rotating that line around the $x$ axis produces a cone and its lateral surface is given by $SA=\pi \cdot y \cdot l$. Write $SA$ as a function of $x$ and solve.
The largest lateral surface area is:
```julia; hold=true; echo=false
r = 1
SA(r,l) = pi * r * l
y(x) = sqrt(1 - x^2)
l(x) = sqrt((x-(-1))^2 + y(x)^2)
SA(x) = SA(y(x), l(x))
cp = find_zero(SA', (-1/2, 1/2))
val = SA(cp)
numericq(val)
```
The surface area of a sphere of radius $1$ is $4\pi r^2 = 4 \pi$. This is how many times greater than that of the largest cone?
In the examples the functions `argmax(f, itr)` and `findmin(collection)` were used. These have mathematical analogs. What is `argmax(f,itr)` in terms of math notation, where ``vs`` is the iterable collection of values:
```julia; hold=true; echo=false
choices = [
raw"``\{v \mid v \text{ in } vs, f(v) = \max(f(vs))\}``",
raw"``\{f(v) \mid v \text{ in } vs, f(v) = \max(f(vs))\}``",
raw"``\{i \mid v_i \text{ in } vs, f(v_i) = \max(f(vs))\}``"
]
radioq(choices, 1)
```
The functions are related: `findmax` returns the maximum value and an index in the collection for which the value will be largest; `argmax` returns an element of the set for which the function is largest, so `argmax(identify, itr)` should correspond to the index found by `findmax` (through `itr[findmax(itr)[2]`)
###### Question
Let ``f(x) = (a/x)^x`` for ``a,x > 0``. When is this maximized? The following might be helpful
In [Hall](https://www.maa.org/sites/default/files/hall03010308158.pdf) we can find a dozen optimization problems related to the following figure of the parabola ``y=x^2`` a point ``P=(a,a^2)``, ``a > 0``, and its normal line. We will do two.
```julia; hold=true, echo=false
p = plot(; legend=false, aspect_ratio=:equal, axis=nothing, border=:none)