I need to make a Gaussian fit, problem is i dont know how to type the parameters using lsqcurvefit
16 views (last 30 days)
Show older comments
Equation of Gaussian distribution
if it's not gonna work with lsqcurvefit then please suggest another Function
[name,path]=uigetfile({'*.xlsx'});
filename = [path name];
opts = detectImportOptions(filename);
preview(filename,opts);
newtable = readtable(filename,'Range','A1:D15');
newtable(:,1)=[]; % Discard A Column
newtable(:,2)=[]; % Discard C Column
figure;
x = newtable{:,1};
y = newtable{:,2};
plot(x,y)
title('Energy in Relation of Beam Insulation');
xlabel('Movement on y Axis [mm]');
ylabel('Energy [mW]');
dydx = gradient(y(:))./gradient(x(:));
mitt=mean(dydx); % Average
sig = std(dydx); % Standard Deviation
fun = @(z,x)z(1)*exp(; % Here is the Problem !!!!
x0 = [1 1];
z = lsqcurvefit(fun,x0,x,dydx)
times = linspace(x(1),x(end));
plot(x,dydx,'ko',times,fun(z,times),'b-')
set(gca, 'YDir','reverse')
legend('Data','Fitted exponential')
title('Data and Fitted Curve')
0 Comments
Accepted Answer
Torsten
on 2 Sep 2022
Edited: Torsten
on 2 Sep 2022
M = [
24.6 1.518572825
24.7 1.5083088954
24.8 1.5210166178
24.9 1.4203323558
24.95 1.1490713587
24.97 0.9853372434
24.99 0.811827957
25.01 0.6217008798
25.03 0.4384164223
25.05 0.2776148583
25.07 0.1495601173
25.09 0.0654936461
25.11 0.0151515152
25.16 0
25.26 0 ];
x = M(:,1);
y = M(:,2);
dydx = -gradient(y(:))./gradient(x(:));
mitt=mean(dydx); % Average
sig = std(dydx); % Standard Deviation
fun1 = @(z)1/(z(2)*sqrt(2*pi))*exp(-(x-z(1)).^2/(2*z(2)^2));
fun= @(z)fun1(z)-dydx;
z0 = [mitt sig];
z = lsqnonlin(fun,z0)
plot(x,dydx)
hold on
plot(x,fun1(z))
2 Comments
Torsten
on 2 Sep 2022
lsqnonlin tries to determine parameters such that res_i(p1,p2,...,pn) = 0.
So in contrast to lsqcurvefit where you define
f_i = 1/(z(2)*sqrt(2*pi))*exp(-(x_i-z(1)).^2/(2*z(2)^2))
and the solver subtracts dydx(i) internally from f_i, you must provide the residual
res_i = f_i - dydx(i)
for lsqnonlin directly.
More Answers (0)
See Also
Categories
Find more on Interpolation 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!