3.
Matlab code and explanation
a. Using manual
1. Gas Input and Plotting
You can enter gases with:
o A name
o Mass (in kg)
o Temperature (in Kelvin)
o A checkbox to show or hide the gas curve
The program will draw the speed distribution curve for each gas.
Each gas is shown in a different color.
You can add or delete gases.
You can click a Refresh button to update everything.
2. f(v) Table – Value at One Point
You can choose a gas line from a dropdown list.
Enter a velocity (v), and it will calculate the corresponding f(v) value.
You can add or delete rows in this table.
3. Interval Table – Area Between Two Speeds
Choose a gas line from a dropdown.
Enter a starting speed (v1) and an ending speed (v2).
The program will:
o Calculate the probability that a gas particle has speed between
v1 and v2.
o Show the area under the curve for that interval using a shaded
color (same as the gas curve color).
You can also add or delete rows in this table.
4. Automatic Updating
When you edit gas info or change tables, the plot and data update
automatically.
The dropdown lists for gas names also update automatically.
Turning a gas on/off will also turn its shaded area on/off
b. code explanation
Creating the main window of the app using uifigure.
fig = uifigure('Name', 'Maxwell-Boltzmann Distribution
Tool');
fig.Position = [100 100 1200 600];
Creating axes and properties of the graph
ax = uiaxes(fig, 'Position', [50 250 500 300]);
title(ax, 'Maxwell-Boltzmann Distribution');
xlabel(ax, 'Velocity v (m/s)');
ylabel(ax, 'Distribution f(v) (s/m)');
grid(ax, 'on');
box(ax, 'on');
hold(ax, 'on');
Make multiple color to color each line on the graph differently
colorList = lines(10);
colorMap = {}; % Will store color for each gas row
plotHandles = {}; % Will store plot handles per gas
areaHandles = {}; % Will store area fill handles per gas
(cell of array of handles)
Create Gas Table, input the data (m, T) and the function will draw the line
on the graph.
gasTable = uitable(fig, 'Data', cell(0,4), 'ColumnName',
{'Name', 'm (kg)', 'T (K)', 'Show'},...
'Position', [600 350 550 200], 'ColumnEditable', [true
true true true]);
Create Add, Delete, and Refresh buttons. Add and delete for making rows
in Gas table, refresh for making the program self-update at the moment
addBtn = uibutton(fig, 'push', 'Text', 'Add Gas', 'Position',
[1050 310 100 30],...
'ButtonPushedFcn', @(btn, event) addGas());
delBtn = uibutton(fig, 'push', 'Text', 'Delete Gas',
'Position', [1050 270 100 30],...
'ButtonPushedFcn', @(btn, event) deleteGas());
refreshBtn = uibutton(fig, 'push', 'Text', 'Refresh',
'Position', [1050 230 100 30],...
'ButtonPushedFcn', @(btn, event) updateAll());
Create f(v) Table, input speed v to calculate the corresponding f(v) value.
fvTable = uitable(fig, 'Data', cell(0,3), 'ColumnName',
{'Line', 'v (m/s)', 'f(v)'},...
'Position', [50 50 350 150], 'ColumnEditable', [true true
false]);
fvAddBtn = uibutton(fig, 'push', 'Text', 'Add f(v) Row',
'Position', [50 210 100 30],...
'ButtonPushedFcn', @(btn, event) addFvRow());
fvDelBtn = uibutton(fig, 'push', 'Text', 'Delete f(v) Row',
'Position', [160 210 100 30],...
'ButtonPushedFcn', @(btn, event) deleteFvRow());
Create Interval Table, input initial speed v1 and final speed v2 to calculate
the probability that a gas particle exist and have the speed between v1 and
v2.
intTable = uitable(fig, 'Data', cell(0,4), 'ColumnName',
{'Line', 'v1 (m/s)', 'v2 (m/s)', 'Probability'},...
'Position', [450 50 400 150], 'ColumnEditable', [true
true true false]);
intAddBtn = uibutton(fig, 'push', 'Text', 'Add Interval Row',
'Position', [450 210 100 30],...
'ButtonPushedFcn', @(btn, event) addIntRow());
intDelBtn = uibutton(fig, 'push', 'Text', 'Delete Interval
Row', 'Position', [560 210 100 30],...
'ButtonPushedFcn', @(btn, event) deleteIntRow());
Make every time the data is change by user, the program will auto update
itself.
gasTable.CellEditCallback = @(src, event) updateAll();
fvTable.CellEditCallback = @(src, event) updateFvTable();
intTable.CellEditCallback = @(src, event)
updateIntervalTable();
Call command, make a dropdown list when you choose for line at f(v) table
and interval table.
updateDropdownOptions();
Add, delete gas in the gas table. Then, auto update the program.
function addGas()
gasTable.Data = [gasTable.Data; {'' 0 0 false}];
colorIdx = mod(numel(colorMap), size(colorList, 1)) + 1;
colorMap{end+1} = colorList(colorIdx, :);
plotHandles{end+1} = [];
areaHandles{end+1} = [];
updateAll();
updateDropdownOptions();
end
function deleteGas()
if ~isempty(gasTable.Selection)
idx = gasTable.Selection(1);
gasTable.Data(idx, :) = [];
colorMap(idx) = [];
if ~isempty(plotHandles{idx}) &&
isvalid(plotHandles{idx})
delete(plotHandles{idx});
end
if ~isempty(areaHandles{idx})
delete(areaHandles{idx});
end
plotHandles(idx) = [];
areaHandles(idx) = [];
updateAll();
updateDropdownOptions();
end
end
add and delete f(v) table row and update the data
function addFvRow()
fvTable.Data = [fvTable.Data; {'' [] []}];
end
function deleteFvRow()
if ~isempty(fvTable.Selection)
idx = fvTable.Selection(1);
fvTable.Data(idx, :) = [];
end
end
add and delete interval table row and update the data
function addIntRow()
intTable.Data = [intTable.Data; {'' [] [] []}];
end
function deleteIntRow()
if ~isempty(intTable.Selection)
idx = intTable.Selection(1);
intTable.Data(idx, :) = [];
end
end
This function redraws the entire graph. It clears the axes, then loops through
each gas entry. If the gas is marked "Show" and has valid mass and
temperature, it calculates and plots the Maxwell-Boltzmann distribution
curve using that gas's data and color.
function updateAll()
cla(ax);
hold(ax, 'on');
legend(ax, 'off');
for i = 1:size(gasTable.Data, 1)
row = gasTable.Data(i, :);
color = colorMap{i};
if row{4} && row{2} > 0 && row{3} > 0
m = row{2}; T = row{3};
v = 0.0001;
v_list = [];
d_list = [];
k = 1.38e-23;
while v <= 5000
d = 4 * pi * (m/(2*pi*k*T))^(3/2) * v^2 *
exp(-(m*v^2)/(2*k*T));
v_list(end+1) = v;
d_list(end+1) = d;
v = v + 0.5;
end
plotHandles{i} = plot(ax, v_list, d_list,
'LineWidth', 2, 'DisplayName', sprintf('%s, %gK', row{1}, T),
'Color', color);
else
plotHandles{i} = [];
end
end
axis(ax, [0 1000 0 0.001]);
legend(ax, 'show');
updateIntervalTable();
updateDropdownOptions();
end
This function calculates the value of the Maxwell-Boltzmann distribution for
each row in the f(v) table. It finds the corresponding gas from the dropdown,
checks if the data is valid, and then computes using the mass, temperature,
and velocity. The result is stored in the third column of the table.
function updateFvTable()
for i = 1:size(fvTable.Data, 1)
selectedLine = fvTable.Data{i, 1};
v = fvTable.Data{i, 2};
gasIdx = findLineIndex(selectedLine);
if gasIdx > 0 && ~isempty(v)
row = gasTable.Data(gasIdx, :);
if row{2} > 0 && row{3} > 0
m = row{2}; T = row{3};
k = 1.38e-23;
f = 4 * pi * (m/(2*pi*k*T))^(3/2) * v^2 *
exp(-(m*v^2)/(2*k*T));
fvTable.Data{i, 3} = f;
else
fvTable.Data{i, 3} = [];
end
else
fvTable.Data{i, 3} = [];
end
end
end
This function clears old, shaded areas and recalculates the probability for
each selected speed interval. It shades the area under the curve and updates
the table if the input is valid.
function updateIntervalTable()
for i = 1:size(areaHandles, 2)
if ~isempty(areaHandles{i})
delete(areaHandles{i});
areaHandles{i} = [];
end
end
for i = 1:size(intTable.Data, 1)
selectedLine = intTable.Data{i, 1};
v1 = intTable.Data{i, 2};
v2 = intTable.Data{i, 3};
gasIdx = findLineIndex(selectedLine);
if gasIdx > 0 && ~isempty(v1) && ~isempty(v2) && v2 >
v1
row = gasTable.Data(gasIdx, :);
if row{2} > 0 && row{3} > 0 && row{4}
m = row{2}; T = row{3};
k = 1.38e-23;
f = @(v) 4 * pi * (m/(2*pi*k*T))^(3/2) .*
v.^2 .* exp(-(m.*v.^2)./(2.*k.*T));
p = integral(f, v1, v2);
intTable.Data{i, 4} = p;
v_fill = v1:1:v2;
y_fill = f(v_fill);
areaHandles{gasIdx}(end+1) = fill(ax, [v_fill
fliplr(v_fill)], [y_fill zeros(size(y_fill))], colorMap{gasIdx},
'FaceAlpha', 0.2, 'EdgeColor', 'none');
else
intTable.Data{i, 4} = [];
end
else
intTable.Data{i, 4} = [];
end
end
end
function idx = findLineIndex(nameStr)
idx = -1;
for j = 1:size(gasTable.Data, 1)
expectedName = sprintf('%s, %gK', gasTable.Data{j,
1}, gasTable.Data{j, 3});
if strcmp(nameStr, expectedName)
idx = j;
return;
end
end
end
The function updates the dropdown menus in both tables with current gas
names and temperature
function updateDropdownOptions()
options = generateDropdownOptions();
fvTable.ColumnFormat = {'char', 'numeric', 'numeric'};
fvTable.ColumnFormat{1} = options;
intTable.ColumnFormat = {'char', 'numeric', 'numeric',
'numeric'};
intTable.ColumnFormat{1} = options;
end
function options = generateDropdownOptions()
options = {' '};
for i = 1:size(gasTable.Data, 1)
row = gasTable.Data(i, :);
if ~isempty(row{1}) && ~isempty(row{3})
options{end+1} = sprintf('%s, %gK', row{1},
row{3});
end
end
end
end