Line 20 LG=NG;
This guarantees that the test on line 18 NG-LG>0.00001 will terminate after the first iteration, as NG-LG is zero, which is smaller than the limit of 0.00001
Change the order of the lines, move line 20 so that it is just before line 19 rather than just after.
You could also change line 22 to return NG; which will give a more accurate result.