MATLAB: Fitnlm either runs perfectly or crashes with nearly identical data

curve fittingfitnlm

I am using fitnlm to do a simple curve fit. Most of the time it runs perfectly, but every now and then nearly identical data will cause it to crash, and I am not sure why. Here is an example that runs perfectly:
Here is an example where it doesn't work. Both example codes are attached, along with the function "weibull.m" which defines the fit.
Thank you very much for your help with this. It really has be baffled.

Best Answer

Hi Clay,
The roots returned in the latter case (fitnlm_broken.m) are imaginary, hence, you receive the error "JW must be scalar or real matrix". To know this, you can put a breakpoint on the 23rd line (x50 = ...) and replace fitnlm by nlinfit, and check the variable mdl (returns the model parameters) which are not real in your case.
Since the parameters are not full rank matrix, the model can not fit the x- and y- parameters.
A solution to this could be that you can try making your data full rank using PCA.