Question 1a
Hist_data = [17, 18, 33, 38, 48, 23, 52, 27, 29, 24, 44, 28, 18, 45, 18, 38,
28, 22, 32, 21, 29, 54, 38, 33, 30, 20, 32, 36, 43, 54];
min_value = min(data);
max_value = max(data);
numClasses = 5
width = ceil ((max_value - min_value)/numClasses)
edges = min_value:width:max_value+width;
edges(end) = max_value + 2
frequencies = histcounts(data, edges);
fprintf('Class Interval\tFrequency\n');
for i = 1:length(frequencies)
if i < length(frequencies)
fprintf('%d-%d\t\t%d\n', edges(i), edges(i+1)-1, frequencies(i));
else
fprintf('%d-%d\t\t%d\n', edges(i), edges(i+1), frequencies(i));
end
end
Question 1b
%get the data, i.e: the histogram data and the number of classes
hist_data =
[17,23,44,38,29,20,18,52,28,28,54,32,33,27,18,22,38,36,38,29,45,32,33,43,48,2
4,18,21,30,54];
classes = 5
% sort the data and calculate the class_width
hist_data = sort(hist_data)
class_width = ceil((hist_data(end) - hist_data(1))/ classes)
edges = [];
counter = hist_data(1);
i=1
while counter<= hist_data(length(hist_data))
edges(i) = counter
counter = counter + class_width
i = i+1
if counter > hist_data(length(hist_data))
edges(i) = hist_data(length(hist_data))
end
end
%get the frequency of the histogram using the boundary
frequency = histcounts(hist_data,edges)
%display the histogram
binCenters = (edges(1:end-1) + edges(2:end)) / 2;
% Create a bar plot using the bar function
bar(binCenters, frequency, 'hist');
xlabel('Students');
ylabel('Frequency');
title('Histogram');
Question 1c
%get the data, i.e: the histogram data and the number of classes
hist_data =
[17,23,44,38,29,20,18,52,28,28,54,32,33,27,18,22,38,36,38,29,45,32,33,43,48,2
4,18,21,30,54];
classes = 5
% sort the data and calculate the class_width
hist_data = sort(hist_data)
class_width = ceil((hist_data(end) - hist_data(1))/ classes)
edges = [];
counter = hist_data(1);
i=1
while counter<= hist_data(length(hist_data))
edges(i) = counter
counter = counter + class_width
i = i+1
if counter > hist_data(length(hist_data))
edges(i) = hist_data(length(hist_data))
end
end
%get the frequency of the histogram using the boundary
frequency = histcounts(hist_data,edges)
%%construct a relative frequency historgram
for i=1 : length(frequency)
relative_freq(i) =frequency(i)/ sum(frequency)
end
binCenters = (edges(1:end-1) + edges(2:end)) / 2;
% Create a bar plot using the bar function
bar(binCenters, relative_freq, 'hist');
xlabel('Students');
ylabel('Frequency');
title('Histogram');
grid on
Question 1d
%get the data, i.e: the histogram data and the number of classes
hist_data =
[17,23,44,38,29,20,18,52,28,28,54,32,33,27,18,22,38,36,38,29,45,32,33,43,48,2
4,18,21,30,54];
classes = 5
% sort the data and calculate the class_width
hist_data = sort(hist_data)
class_width = ceil((hist_data(end) - hist_data(1))/ classes)
edges = [];
counter = hist_data(1);
i=1
while counter<= hist_data(length(hist_data))
edges(i) = counter
counter = counter + class_width
i = i+1
if counter > hist_data(length(hist_data))
edges(i) = hist_data(length(hist_data))
end
end
%get the frequency of the histogram using the boundary
frequency = histcounts(hist_data,edges)
%display the histogram
binCenters = (edges(1:end-1) + edges(2:end)) / 2;
% Create a bar plot using the bar function
xlabel('Ages in Years');
ylabel('Frequency');
title('Histogram');
hold on
plot(binCenters, frequency, '-o', 'LineWidth', 2);
grid on
Question 1e
Data =
[17,18,33,38,48,23,52,27,29,24,44,28,18,45,18,38,28,22,32,21,29,54,38,33,30,2
0,32,36,43,54];
edges = [16.5 24.5 32.5 40.5 48.5 56.5];
[counts, edges] = histcounts(Data,edges);
cumulative_counts = cumsum(counts);
midpoints = edges(1:end-1) + diff(edges)/2;
plot(midpoints,cumulative_counts, '-o');
xlabel('Ages (in years)');
ylabel('Cumulative Frequency');
title('Cumulative Frequency Graph');
grid on;