Hello, jahichuanna!

This is a classic . . . The "shortest ladder" problem.

A wall is being supported by a beam passing over a dumpster 5 ft high and 4 ft from the wall.

Find the length of the shortest possible beam. It is easier using Trigonometry . . . Code:

* C
* |
* |
B * θ |
* - - - * D
* | 4 |
* | |
* |5 |
* θ | |
A * - - - - * - - - * E
F 4

In right triangle $\displaystyle BFA\!:\;\;\sin\theta \:=\:\frac{5}{AB} \quad\Rightarrow\quad AB \:=\:5\csc\theta$

In right triangle $\displaystyle CDB\!:\;\;\cos\theta \:=\:\frac{4}{BC} \quad\Rightarrow\quad BC \:=\:4\sec\theta $

So we have: .$\displaystyle AC \:=\:AB + BC \quad\Rightarrow\quad L \;=\;5\csc\theta + 4\sec\theta $

And __that__ is the function we must minimize . . .