How to do Curve Fitting with a Piecewise Gaussian Model
Show older comments
Hi,
I am trying to fit a piecewise Gaussian function to an intensity profile (blue curve in the picture) and I'm running into Problems extracting the best fit parameters. This is my first time trying to do a Curve Fit so I decided to ask you for some advice.
I implemented the piecewise Model:
function [ value ] = PiecewiseGaussianModel( A1,A2,m1,m2,sigma1,sigma2,I1,I2,A,B,x )
value=zeros(size(x));
for i=1:length(x)
if x(i) < A || x(i) > B
value(i)=-A1*exp(-((x(i)-m1)^2)/(2*(sigma1)^2))+I1;
elseif A <= x(i) && x(i) <= B
value(i)= A2*exp(-((x(i)-m2)^2)/(2*(sigma2)^2))+I2;
end
end
end
and the minimization problem:
function [ error ] = GaussianError( parameter,x )
load('profile2.mat'); %y intensity Data
x=1:1:numel(y);
x=x';
[ A,B ] = FindBoundaries( y );
fx=PiecewiseGaussianModel(parameter(1),parameter(2),parameter(3),parameter(4), ...
parameter(5),parameter(6),parameter(7),parameter(8),A,B,x);
fx=fx';
error=fx-y;
end;
function [ A,B ] = FindBoundaries( y )
[~, ind1]=min(y);
copy_y=y;
copy_y(ind1)=inf;
[~, ind2]=min(copy_y);
if ind1 > ind2
A=ind2;
B=ind1;
else
A=ind1;
B=ind2;
end
end
profile2.mat returns y and this is how y' looks like:
y'=
Columns 1 through 13
130 120 106 95 90 84 85 90 94 101 112 122 121
Columns 14 through 26
106 96 90 87 82 81 83 88 97 110 124 136 147
Columns 27 through 28
153 151
I tried solving this problem by using the Matlab functions lsqcurvefit and lsqnonlin, but I don't get any good results. Is this problem solvable with these functions? And if so, is there a good solution getting the start points for the fitting?
Thanks in advance
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!