When you maximize or minimize over an area, the critical points (maxima and minima) will arise either where df/dx and df/dy are both zero, or on vertices, or on the boundaries.
We first set df/dx = 0 and dg/dx = 0.
df/dx = 2 - 2x = 0 => x = 1
dg/dx = 2 - 2y = 0 => y = 1
Thus, one critical point is (1,1). (We do not yet know if this is a maxima or a minima, but lies in the region)
Next, we consider the three boundary lines. These are x = 0, y = 0, and x + y =9.
For x = 0, f(x,y) = 2 + 2y - y^2. We can directly find the critical points by taking
df/dy = 2 - 2y = 0 => y = 1. This yields another critical point (0,1)
For y = 0, f(x,y) = 2 + 2x - x^2. We can directly find the critical points by taking
df/dx = 2 - 2x = 0 => x = 1. This yields another critical point (1,0)
For x + y = 9, we can use Langrange multipliers.
Define h(x,y,L) = f(x,y) + L (x + y -9)
The critical points will be obtained by setting dh/dx = dh/dy = dh/dL = 0. We have:
dh/dx = 2 - 2x + L = 0
dh/dy = 2 - 2y + L = 0
dh/dL = x + y - 9 = 0
The first two equations yield x = y. The last equation gives x = y = 9/2.
So the next critical point is (9/2, 9/2).
Finally, all three vertices (0,0), (0,9), (9,0) are also critical points.
Thus, the set of all critical points is {(1,1), (1,0), (0,1), (9/2,9/2), (0,0), (9,0), (0,9)}.
Check the value of f(x,y) at all these points. It is maximum at (1,1) and equals 4.
It is minimum at (9,0) and (0,9) and equals -61.