Fitting a modified gaussian
조회 수: 6 (최근 30일)
이전 댓글 표시
Hello Experts,
I am new to data fitting. I would be very gratefull for a detailed response. I have a data set that looks like a streched gaussian distribution, or a reverse log normal distribution please see attached plot. Please let me know what distribution will be the best to get a good fit for this type of data. If you could please provide me with an example that would be very helpful. I have attached x and y data for your reference.
Thanks very much!
load x
load y
plot(x, y)
댓글 수: 0
채택된 답변
Mathieu NOE
2022년 3월 21일
hello
I am by no mean a curve fitting or stats expert but this is what I could achieve :
this is a reverse log normal distribution - so basically a gaussian fit realized not on x but on exp(x/constant)
f = @(a,b,c,d,x) a.*exp(-(exp(x/d)-b).^2 / c.^2);
plot
code
load('x.mat');
load('y.mat');
% curve fit using fminsearch
f = @(a,b,c,d,x) a.*exp(-(exp(x/d)-b).^2 / c.^2);
obj_fun = @(params) norm(f(params(1), params(2), params(3), params(4),x)-y);
d_init = 1000;
[a_init,ind] = max(y);
b_init = exp(x(ind)/d_init);
sol = fminsearch(obj_fun, [a_init,b_init,b_init/2,d_init]);
a_sol = sol(1)
b_sol = sol(2)
c_sol = sol(3)
d_sol = sol(4)
xx = linspace(min(x),max(x),200);
y_fit = f(a_sol, b_sol,c_sol,d_sol, xx);
yy = interp1(x,y, xx);
Rsquared = my_Rsquared_coeff(yy,y_fit); % correlation coefficient
plot(xx, y_fit, '-',x,y, 'r .', 'MarkerSize', 15)
title(['Gaussian Fit / R² = ' num2str(Rsquared) ], 'FontSize', 15)
ylabel('Amplitude', 'FontSize', 14)
xlabel('x', 'FontSize', 14)
eqn = " y = "+a_sol+ " * exp(-(exp(x / " +d_sol+" )- " +b_sol+")² / (" +c_sol+ ")²";
legend(eqn)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function Rsquared = my_Rsquared_coeff(data,data_fit)
% R2 correlation coefficient computation
% The total sum of squares
sum_of_squares = sum((data-mean(data)).^2);
% The sum of squares of residuals, also called the residual sum of squares:
sum_of_squares_of_residuals = sum((data-data_fit).^2);
% definition of the coefficient of correlation is
Rsquared = 1 - sum_of_squares_of_residuals/sum_of_squares;
end
댓글 수: 2
Mathieu NOE
2022년 3월 21일
hello again
for the first question, I would simply recommend to see the fminsearch documentation. Simply follow how the function to minimize is defined (handle);
c_init = b_init/2 : a rough estimate based on how narrow or wide is the experimental peak. You can probably try other ratios , the optimizer will still converge to the optimal value
All the best !
M
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Probability Distributions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!