I need to make a Gaussian fit, problem is i dont know how to type the parameters using lsqcurvefit

15 visualizaciones (últimos 30 días)
Equation of Gaussian distribution
if it's not gonna work with lsqcurvefit then please suggest another Function
[name,path]=uigetfile({'*.xlsx'});
Error using matlab.internal.lang.capability.Capability.require
Support for Java user interfaces is required, which is not available on this platform.

Error in uigetfile (line 117)
Capability.require(Capability.Swing);
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')

Respuesta aceptada

Torsten
Torsten el 2 de Sept. de 2022
Editada: Torsten el 2 de Sept. de 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)
Local minimum possible. lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
z = 1×2
25.0069 0.0457
plot(x,dydx)
hold on
plot(x,fun1(z))
  2 comentarios
Hussam Saddour
Hussam Saddour el 2 de Sept. de 2022
Editada: Hussam Saddour el 2 de Sept. de 2022
thank you very much! although i didnt quite understand whats the idea of substracting dydx from fun1, and is there a method to make the curve smoother?
Torsten
Torsten el 2 de Sept. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interpolation en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by