0% found this document useful (0 votes)
75 views5 pages

MATLAB Code For Questions 1a-E

The document contains code to generate histograms from sample data using different techniques: 1) It calculates the minimum, maximum and class width to generate bin edges, then uses histcounts to calculate frequencies and displays the results. 2) It sorts the data, calculates class width, generates bin edges in a loop, uses histcounts to calculate frequencies and displays a bar plot of the histogram. 3) It generates a relative frequency histogram by normalizing the frequencies. 4) It displays the histogram using bar with plot symbols instead of bars. 5) It calculates the cumulative frequency and plots it.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views5 pages

MATLAB Code For Questions 1a-E

The document contains code to generate histograms from sample data using different techniques: 1) It calculates the minimum, maximum and class width to generate bin edges, then uses histcounts to calculate frequencies and displays the results. 2) It sorts the data, calculates class width, generates bin edges in a loop, uses histcounts to calculate frequencies and displays a bar plot of the histogram. 3) It generates a relative frequency histogram by normalizing the frequencies. 4) It displays the histogram using bar with plot symbols instead of bars. 5) It calculates the cumulative frequency and plots it.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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;

You might also like