# Related rates


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

This section uses these add-on packaages:


```{julia}
using CalculusWithJulia
using Plots
using Roots
using SymPy
```


---


Related rates problems involve two (or more) unknown quantities that are related through an equation. As the two variables depend on each other, also so do their rates - change with respect to some variable which is often time, though exactly how remains to be discovered. Hence the name "related rates."


#### Examples


The following is a typical "book" problem:


> A screen saver displays the outline of a $3$ cm by $2$ cm rectangle and then expands the rectangle in such a way that the $2$ cm side is expanding at the rate of $4$ cm/sec and the proportions of the rectangle never change.  How fast is the area of the rectangle increasing when its dimensions are $12$ cm by $8$ cm? [Source.](http://oregonstate.edu/instruct/mth251/cq/Stage9/Practice/ratesProblems.html)



```{julia}
#| hold: true
#| echo: false
#| cache: true
### {{{growing_rects}}}
## Secant line approaches tangent line...
function growing_rects_graph(n)
    w = (t) -> 2 + 4t
    h = (t) -> 3/2 * w(t)
    t = n - 1

    w_2 = w(t)/2
    h_2 = h(t)/2

    w_n = w(5)/2
    h_n = h(5)/2

    plt = plot(w_2 * [-1, -1, 1, 1, -1], h_2 * [-1, 1, 1, -1, -1], xlim=(-17,17), ylim=(-17,17),
               legend=false, size=fig_size)
    annotate!(plt, [(-1.5, 1, "Area = $(round(Int, 4*w_2*h_2))")])
    plt


end
caption = L"""

As $t$ increases, the size of the rectangle grows. The ratio of width to height is fixed. If we know the rate of change in time for the width ($dw/dt$) and the height ($dh/dt$) can we tell the rate of change of *area* with respect to time ($dA/dt$)?

"""
n=6

anim = @animate for i=1:n
    growing_rects_graph(i)
end

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

ImageFile(imgfile, caption)
```

Here we know $A = w \cdot h$ and we know some things about how $w$ and $h$ are related *and* about the rate of how both $w$ and $h$ grow in time $t$. That means that we could express this growth in terms of some functions $w(t)$ and $h(t)$, then we can figure out that the area - as a function of $t$ - will be expressed as:


$$
A(t) = w(t) \cdot h(t).
$$

We would get by the product rule that the *rate of change* of area with respect to time, $A'(t)$ is just:


$$
A'(t) = w'(t) h(t) + w(t) h'(t).
$$

As an aside, it is fairly conventional to suppress the $(t)$ part of the notation $A=wh$ and to use the Leibniz notation for derivatives:


$$
\frac{dA}{dt} = \frac{dw}{dt} h + w \frac{dh}{dt}.
$$

This relationship is true for all $t$, but the problem discusses a certain value of $t$ - when $w(t)=8$ and $h(t) = 12$. At this same value of $t$, we have $w'(t) = 4$ and so $h'(t) = 6$. Substituting these 4 values into the 4 unknowns in the formula for $A'(t)$ gives:


$$
A'(t) = 4 \cdot 12 + 8 \cdot 6 = 96.
$$

Summarizing, from the relationship between $A$, $w$ and $t$, there is a relationship between their rates of growth with respect to $t$, a time variable. Using this and known values, we can compute. In this case,  $A'$ at the specific $t$.


We could also have done this differently. We would recognize the following:


  * The area of a rectangle is just:


```{julia}
A(w,h) = w * h
```

  * The width - expanding at a rate of $4t$ from a starting value of $2$ - must satisfy:


```{julia}
w(t) = 2 + 4*t
```

  * The height is a constant proportion of the width:


```{julia}
h(t) = 3/2 * w(t)
```

This means again that area depends on $t$ through this formula:


```{julia}
A(t) = A(w(t), h(t))
```

This is why the rates of change are related: as $w$ and $h$ change in time, the functional relationship with $A$ means $A$ also changes in time.


Now to answer the question, when the width is 8, we must have that $t$ is:


```{julia}
tstar = find_zero(x -> w(x) - 8, [0, 4])  # or solve by hand to get 3/2
```

The question is to find the rate the area is increasing at the given time $t$, which is $A'(t)$ or $dA/dt$. We get this by performing the differentiation, then substituting in the value.


Here we do so with the aid of `Julia`, though this problem could readily be done "by hand."


We have expressed $A$ as a function of $t$ by composition, so can differentiate that:


```{julia}
A'(tstar)
```

---


Now what? Why is $96$ of any interest? It is if the value at a specific time is needed. But in general, a better question might be to understand if there is some pattern to the numbers in the figure, these being $6, 54, 150, 294, 486, 726$. Their differences are the *average* rate of change:


```{julia}
xs = [6, 54, 150, 294, 486, 726]
ds = diff(xs)
```

Those seem to be increasing by a fixed amount each time, which we can see by one more application of `diff`:


```{julia}
diff(ds)
```

How can this relationship be summarized? Well, let's go back to what we know, though this time using symbolic math:


```{julia}
@syms t
diff(A(t), t)
```

This should be clear: the rate of change, $dA/dt$, is increasing linearly, hence the second derivative, $dA^2/dt^2$ would be constant, just as we saw for the average rate of change.


So, for this problem, a constant rate of change in width and height leads to a linear rate of change in area, put otherwise, linear growth in both width and height leads to quadratic growth in area.


##### Example


A ladder, with length $l$, is leaning against a wall. We parameterize this problem so that the top of the ladder is at $(0,h)$ and the bottom at $(b, 0)$. Then $l^2 = h^2 + b^2$ is a constant.


If the ladder starts to slip away at the base, but remains in contact with the wall, express the rate of change of $h$ with respect to $t$ in terms of $db/dt$.


We have from implicitly differentiating in $t$ the equation $l^2 = h^2 + b^2$,  noting that $l$ is a constant, that:


$$
0 = 2h \frac{dh}{dt} + 2b \frac{db}{dt}.
$$

Solving, yields:


$$
\frac{dh}{dt} = -\frac{b}{h} \cdot \frac{db}{dt}.
$$

  * If when $l = 12$ it is known that $db/dt = 2$ when $b=4$, find $dh/dt$.


We just need to find $h$ for this value of $b$, as the other two quantities in the last equation are known.


But $h = \sqrt{l^2 - b^2}$, so the answer is:


```{julia}
length, bottom, dbdt = 12, 4, 2
height = sqrt(length^2 - bottom^2)
-bottom/height * dbdt
```

  * What happens to the rate as $b$ goes to $l$?


As $b$ goes to $l$, $h$ goes to $0$, so $b/h$ blows up. Unless $db/dt$ goes to $0$, the expression will become $-\infty$.


:::{.callout-note}
## Note
Often, this problem is presented with $db/dt$ having a constant rate. In this case, the ladder problem defies physics, as $dh/dt$ eventually is faster than the speed of light as $h \rightarrow 0+$. In practice, were $db/dt$ kept at a constant, the ladder would necessarily come away from the wall. The trajectory would follow that of a tractrix were there no gravity to account for.

:::

##### Example


```{julia}
#| hold: true
#| echo: false
caption = "A man and woman walk towards the light."

imgfile = "figures/long-shadow-noir.png"
#ImageFile(:der
#| hold: true
#| echo: false
choices = [
"``dy/dt = 2x + 1/x``",
"``dy/dt = 1 - x^2 - \\log(x)``",
"``dy/dt = -2x - 1/x``",
"``dy/dt = 1``"
]
answ=1
radioq(choices, answ)
```
