MATLAB Answers

fitnlm either runs perfectly or crashes with nearly identical data

4 views (last 30 days)
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.

  1 Comment

Clay Swackhamer
Clay Swackhamer on 7 Oct 2020
Pasting the full error message here:
Error using internal.stats.getscheffeparam>ValidateParameters (line 182)
If non-empty, JW must be a numeric, real matrix.
Error in internal.stats.getscheffeparam (line 110)
[J,VF,VP,JW,Intopt,TolSVD,TolE,VQ,usingJ] = ValidateParameters(J,VF,VP,JW,Intopt,TolSVD,TolE,VQ,allowedIntopt);
Error in nlinfit (line 441)
sch = internal.stats.getscheffeparam('WeightedJacobian',J(~nans,:),'Intopt','observation','VQ',VQ);
Error in NonLinearModel/fitter (line 1127)
nlinfit(X,y,F,b0,opts,wtargs{:},errormodelargs{:});
Error in classreg.regr.FitObject/doFit (line 94)
model = fitter(model);
Error in NonLinearModel.fit (line 1434)
model = doFit(model);
Error in fitnlm (line 99)
model = NonLinearModel.fit(X,varargin{:});
Error in fitnlm_broken (line 20)
mdl = fitnlm(x,y,@weibull,betaNot) %crashes here

Sign in to comment.

Accepted Answer

Gaurav Garg
Gaurav Garg on 13 Oct 2020
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.

  3 Comments

Clay Swackhamer
Clay Swackhamer on 13 Oct 2020
Hello Gaurav,
Thank you for the answer, this explains why my fit failed. In the case of PCA, could I make the data full rank using PCA, and then fit using the same curve fitting equation? Or alternatively, do you mean that I could make the model parameter matrix full rank? Thank you very much for your insight, I am not too familiar with PCA.
Is there any example of this on the file exchange, perhaps?
Thanks,
Clay
Gaurav Garg
Gaurav Garg on 14 Oct 2020
Hey Clay,
There are many ways to put constraints on rank, and PCA is one of them; and SVD being another.
You can take the eigen values which are more relevant and compress your data with the help of SVD and PCA taking help from their documentations.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!