How do I get the error of a fitting function?

24 views (last 30 days)
I've written a function that does a Lorentzian fit on a set of data. I want to calculate an error bar for the accuray of this fit with respect to the x coordinates (the parameter Wavelength). Is this a built in command or is it more difficult?
function [params, Wavelength, Intensity, wavpeak]=SpectrumPeakFitCSV(filename)
SpectrumData=csvread(filename);
Wavelength=SpectrumData(2:end-1,1);
Intensity=SpectrumData(2:end-1,2);
Max = max(Intensity);
peak = find(Intensity==Max(1));
wavpeak = Wavelength(peak(1));
fun=@(x,Wav)Lorentzian(x(1),x(2),x(3),Wav)+x(4);
dark=Intensity(1);
opts = optimset('Display','off');
x=lsqcurvefit(fun,[Max(1),1,wavpeak,dark],Wavelength,Intensity,[0.5*Max(1) 0 wavpeak-0.2 0.9*dark],[2*Max(1) 3 wavpeak+0.2 1.5*dark],opts);
params=x; %scale, width, centre, dark
end

Accepted Answer

Star Strider
Star Strider on 14 Jan 2019
If I understand correctly what you want to do, the Statistics and Machine Learning Toolbox function nlpredci (link) is likely what you are looking for. Use the ‘jacobian’ output (last output) from lsqcurvefit with it.
  5 Comments
Janna Hinchliff
Janna Hinchliff on 16 Jan 2019
Thanks, nlinfit seems to be a bit easier!
Star Strider
Star Strider on 16 Jan 2019
@Janna Hinchliff — Please see my Comment to your Question.

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!