A finite element scheme to study the nonlinear optical response of a finite grating without and with defect