MATLAB: Calculating covariance matrix from Jacobian using lsqcurvefit

covarianceCurve Fitting ToolboxfittingjacobianlsqcurvefitMATLABOptimization Toolboxresiduals

I am trying to calculate the covariance matrix from the residuals vector and the Jacobian matrix, which are optional outputs of the lsqcurvefit function. I keep getting negative values for the diagonal (variance) values, but they should be strictly positive. Does this have something to do with the method that lsqcurvefit uses? I have attached sample data and a sample extPar structure (this usually gets generated other peices of code that call this function). The part of the code that I think is germane is here:
[par,resnorm,R,~,~,~,J] = lsqcurvefit(FID,par0,fitTime,fitData);
pCov = inv(J'*J)*((R'*R)./(numel(fitData) - numel(par)));
The fit function converges to a nice looking solution, am I doing something drastically wrong here?
I am fully aware that the statistics toolbox solves this easily, but unless you give my prof the cash to buy it for me, I'm stuck with this.

Best Answer

This old documentation example might be of some use.
Alan Weiss
MATLAB mathematical toolbox documentation