find the full width half max of a normal distribution in matlab

main.m
% Generate a normal distribution with mean=0 and std deviation=1
mu = 0;
sigma = 1;
x = -5:.01:5;
y = normpdf(x,mu,sigma);

% Find the max value and index
[max_val, max_idx] = max(y);

% Half max value
half_max = max_val / 2;

% Find the lower and upper indexes where the curve crosses the half-max value
lower_idx = find(y(1:max_idx) < half_max, 1, 'last');
upper_idx = find(y(max_idx:end) < half_max, 1) + max_idx - 1;

% Calculate the full width half max (FWHM)
fwhm = x(upper_idx) - x(lower_idx);
501 chars
19 lines

This code generates a normal distribution with mean=0 and std deviation=1 using normpdf function in Matlab. It then finds the maximum value and index of the curve. The half max value is calculated by dividing the max value by 2. Following that, the lower and upper indexes where the curve crosses the half-max value are found using find function. Finally, the full width half max (FWHM) is calculated as the difference between the x-values at the upper and lower indexes.

gistlibby LogSnag