I read this question the other day and it got me thinking: the area of a circle is $\pi r^2$, which differentiates to $2 \pi r$, which is just the perimeter of the circle.
Why doesn’t the same thing happen for squares?
If we start with the area formula for squares, $l^2$, this differentiates to $2l$ which is sort of right but only half the perimeter. I asked my calculus teacher and he couldn’t tell me why. Can anyone explain???
Actually, it is also true for squares (and for regular polygons in general!). The problem you ran into is what the equivalent of “r” is. The side length of a square is actually more comparable to the circle’s diameter.
Instead, the correct analogue of the circle’s radius is the distance from the center of the square to the midpoint of one side, which is only half as long as the square’s side.
Here, we have $A = (2r)^2 = 4 r^2$ and $P = 4 (2r) = 8 r$. The perimeter is the derivative of the area with respect to $r$, just as in the case of a circle.