As Mubsi says, the equations as written are correct. I answered on the other thread where you also asked this question. Here’s an older thread that also discusses this point.
For the specific chunk of code you show, you are not supplying the factor of \frac {1}{m}, so presumably you did it “your way” and included that in the dAL calculation at the outer layer. You may be able to get that to work if you make the adjustments throughout, but I think the better strategy is just to follow the way Prof Ng does it. That will just be simpler than trying to rewrite everything your way.