The code looks generally alright.
Which criterion are you using and what is the last non-linearity in your model?
PS: Tagging certain people might discourage others to answer in your thread and as you can see I’m quite clueless here.