To obtain an iterative algorithm, equate the derivatives of the objective function w.r.t. to f and g to zero:
The method is stable if is small, and suitable boundary conditions are used.