Clear Filters
Clear Filters

Fitting a modified gaussian

14 views (last 30 days)
Sanchit Sharma
Sanchit Sharma on 18 Mar 2022
Commented: Mathieu NOE on 21 Mar 2022
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)

Accepted Answer

Mathieu NOE
Mathieu NOE on 21 Mar 2022
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 Comments
Sanchit Sharma
Sanchit Sharma on 21 Mar 2022
Thanks very much! This was very helpful. Could you please elaborate the meaning of line:
obj_fun = @(params) norm(f(params(1), params(2), params(3), params(4),x)-y);
Also, why did you assume c_init = b_init/2?
Best,
S
Mathieu NOE
Mathieu NOE on 21 Mar 2022
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

Sign in to comment.

More Answers (0)

Categories

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!