I don't have a lot of time to reinstall matlab to try out your code, but just looking at it, i think you can probably improve the code by specifying "valid ranges" of values, and also max numbers of iterations etc.
What I mean is, the matlab code you have now might, for example, use some...