Show that the line through $P$ and $Q$ is perpendicular to the surface at $P$. Consider a smooth surface given by the function $z = f(x,y)$, such that the partial
derivatives of $f(x,y)$ exist. Suppose $Q$ is a point that does not lie on the surface,
and $P$ is the nearest point on the surface to $Q$. Show that the line through $P$ and
$Q$ is perpendicular to the surface at $P$.

Let $Q=(\alpha,\beta,\gamma)$. For any point $S=(x,y,f(x,y))$ on the surface define the function
$$g(x,y)=(x-\alpha)^2+(y-\beta)^2+(f(x,y)-\gamma)^2$$
The function $g(x,y)$ represents the square of the distance between $S$ and $Q$.
Let $P=(x_0,y_0,f(x_0,y_0)$ denote the nearest point the surface $Q$.
Then $g(x_0,y_0)$ is the minimum value of $g(x,y)$.
Now $$g_x(x,y)=2(x-\alpha)+2(f(x,y)-\gamma)f_x(x,y)$$
$$g_y(x,y)=2(y-\beta)+2(f(x,y)-\gamma)f_y(x,y)$$
so $$0=2(x_0-\alpha)+2(f(x_0,y_0)-\gamma)f_x(x_0,y_0) \tag{1}$$
$$0=2(y_0-\beta)+2(f(x_0,y_0)-\gamma)f_y(x_0,y_0) \tag{2}$$
The equation of the tangent plane at $(x_0,y_0)$ is given by
$$z=f(x_0,y_0)+f_x(x_0,y_0)(x-x_0)+f_y(x_0,y_0)(y-y_0)$$
which is standard scalar form $ax+by+cz=d$, is expressed as
$$f_x(x_0,y_0)x+f_y(x_0,y_0)y-z=f(x_0,y_0)+f_x(x_0,y_0)(x-x_0)+f_y(x_0,y_0)(y-y_0)$$
From the above equation a normal vector for the plane is read off as being
$$n=(f_x(x_0,y_0), f_y(x_0,y_0), -1)$$
Now, $$\overrightarrow{QP}=(x_0-\alpha,y_0-\beta,f(x_0,y_0)-\gamma)$$
$$\overrightarrow{QP}=(\gamma-f(x_0,y_0))(f_x(x_0,y_0), f_y(x_0,y_0), -1)$$
$$\overrightarrow{QP}=(\gamma-f(x_0,y_0))n$$
Where $equation (1)(2)$ have been used in going from the first line to the second. Sice $Q$ does not lie on the surface, $(\gamma-f(x_0,y_0))\neq0$. It follows that $n$ and $\overrightarrow{QP}$ are parallel, so the line through $P$ and $Q$ is perpendicular to the surface at $P$. 
