Custom Fit - Error Functions
37 views (last 30 days)
Show older comments
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 Comment
Erik Schiferle
on 10 Aug 2020
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
Answers (1)
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!