% Unit 2 (MATLAB Course @ UAM)
% (c) Alexander Knebe

%================================================
% Maxwell-Boltzmann distribution of hydrogen gas
%================================================
clear all
close all
clc

% physical constants
kB = 1.38E-23;
m  = 1.67E-27;

% temperature of the gas
T    = 350;

% range and number of points
N    = 1000;
vmin = 0;
vmax = 10000.;

% calculate Maxwell-Boltzmann distribution
v = linspace(vmin,vmax,N);
f = 4*pi*(m/(2*pi*kB*T))^1.5 * v.^2 .* exp(-m*v.^2/(2*kB*T));

% plot distribution
plot(v,f), xlabel('v [m/s]'), ylabel('f')

% determine maximum of distribution
[fmax,imax] = vecmax(f);
vmax        = v(imax);

% we now add some lines to the plot indicating the maximum
hold on
plot([vmax vmax],[min(f) max(f)], 'r')
plot([min(v) vmax],[fmax fmax], 'r')
hold off