Custom Fit - Error Functions

조회 수: 10 (최근 30일)
Darin
Darin 2014년 5월 7일
댓글: Erik Schiferle 2020년 8월 10일
Hi All,
So I am trying to fit a series of error functions, and it is not coming up with viable answers. I am using simulated data that I know fits error functions, but the answers it is coming up with are worse fits than linear.
The code I have for the fit is, where N is the number of error functions, and X and Y are the data (Y is normalized). It's not throwing any errors, just the fit is terrible, and it does not seem to be going about it correctly (my initial guesses are better than its result):
%construct equation string
expression = '';
coefficients = {};
for index = 1:N
expression = [expression, '(1/2)*(1+erf( (x-mu', num2str(index) ,').^2 / sqrt(2*sigma', num2str(index), '.^2) )) +'];
coefficients = [coefficients {['mu', num2str(index)]} {['sigma', num2str(index)]}];
end
%cut off the last + sign
expression(end) = [];
%construct reasonable initial values by dividing the span up evenly
span = max(X) - min(X);
mu0 = min(X) + (span/N * (1/2):1:(N-1/2));
sigma0 = span/(2*N);
startpoint = zeros(1,2*N); %initial guess
LB = zeros(1,2*N); %lower bounds
UB = zeros(1,2*N); %upper bounds
for index = 1:N
startpoint(2*index-1) = mu0(index);
startpoint(2*index) = sigma0;
LB(2*index-1) = min(X);
LB(2*index) = span*10^-5;
UB(2*index-1) = max(X);
UB(2*index) = 2*span;
end
%fit
f = fittype(expression, 'coefficients', coefficients, 'independent', {'x'}, 'dependent', {'y'});
[fitobj, gof] = fit(X,Y,f, 'StartPoint', startpoint, 'Lower', LB, 'Upper', UB);
rmserror = gof.rmse;
rsquared = gof.rsquare;
%to debug
figure;
plot(X,Y, 'o');
hold on;
plot(X,fitobj(cdfx), 'Color', 'red' , 'LineWidth', 1.5)
hold off
When I use the curve fit toolbox, it also has difficulty with this fit. I have attached some data you can use for X and Y (X is first column, Y is second).
Thanks for any help!
  댓글 수: 1
Erik Schiferle
Erik Schiferle 2020년 8월 10일
Hi,
Any chance you could send your example data to me. And both exponents were supposed to be taken out? I'm trying to do something very similar

댓글을 달려면 로그인하십시오.

답변 (1개)

Darin
Darin 2014년 5월 7일
Nevermind - I have squared in my expression that shouldn't be there, woops! Sorry! At least this code is helpful if anyone wants to do the same.

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by