Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
21 views20 pages

MATLAB Practical STAT-1007 Code

The document outlines a series of MATLAB programming exercises for a B.Sc. (Honours) Part-1, Semester-1 Examination in 2024, focusing on statistical data analysis. It includes tasks such as calculating mean, median, mode, range, midrange, creating frequency distributions, histograms, and frequency polygons, as well as finding variance and standard deviation from given datasets. Each exercise is accompanied by sample MATLAB code to guide students in implementing the required statistical functions.

Uploaded by

dewanshoumik87
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)
21 views20 pages

MATLAB Practical STAT-1007 Code

The document outlines a series of MATLAB programming exercises for a B.Sc. (Honours) Part-1, Semester-1 Examination in 2024, focusing on statistical data analysis. It includes tasks such as calculating mean, median, mode, range, midrange, creating frequency distributions, histograms, and frequency polygons, as well as finding variance and standard deviation from given datasets. Each exercise is accompanied by sample MATLAB code to guide students in implementing the required statistical functions.

Uploaded by

dewanshoumik87
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
You are on page 1/ 20

B.Sc.

(Honours) Part-1, Semester-1 Examination 2024


List of problems for Math Practical (Stat)
1. Write a MATLAB program to organize the data in ascending order and find the (a) mean, (b)
median, (c) range, and (d) midrange.
Exercise: Write a MATLAB program to organize the following data in ascending order and
find the (a) mean, (b) median, (e) mode, (d) range, and (e) midrange.
55, 60, 68, 73, 76, 84, 88, 57, 62, 70, 75, 77, 84, 90, 58, 64, 71, 75, 79, 85, 91, 58, 66, 71, 76,
80, 87, 93, 58, 66, 72, 76, 82, 88, 95, 55, 60, 68, 73, 76, 84, 88, 57, 62, 70, 75, 77, 84, 90, 58.
CODE:
function Mean_median_range_midrange
clc
clear all
% Given data
data = [55, 60, 68, 73, 76, 84, 88, 57, 62, 70, 75, 77, 84, 90, 58, ...
64, 71, 75, 79, 85, 91, 58, 66, 71, 76, 80, 87, 93, 58, 66, ...
72, 76, 82, 88, 95, 55, 60, 68, 73, 76, 84, 88, 57, 62, 70, ...
75, 77, 84, 90, 58];
x=sort(data);
n=length(x);
disp('Sorted Data in ascending order:')
disp([x'])
% (a) Calculate the mean
sum=0.0;
for i=1:n
sum=sum+x(i);
end
mean=sum/n;
% (b) Calculate the median
if (mod(n, 2) == 0)
median = (x(n/2) + x(n/2 + 1)) / 2;
else
median = x((n + 1) / 2);
end

% (c) Calculate the range


range = x(n) - x(1);

% (d) Calculate the midrange


midrange = (x(1) + x(n)) / 2;
fprintf('\n Mean = %.2f, Median = %.2f, Range = %.2f, and Midrange = %.2f.\n', mean, median,
range, midrange);
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
2. Write a MATLAB program to organize the data into a frequency distribution and display the
data in a histogram.
Exercise: Write a MATLAB program to organize the following data into a frequency
distribution:
700, 450, 725, 1125, 675, 1650, 750, 400, 1050, 500, 750, 850, 1250, 725, 475, 925, 1050,
925, 850, 625, 900, 1750, 700, 825, 550, 925, 850, 475, 750, 550, 725, 575, 575, 1450, 700,
450, 700, 1650, 925, 500, 675, 1300, 1125, 775, 850.
Display the data in a histogram.
CODE:
function histogram
clc
clf
clear all
% Given data
data = [700, 450, 725, 1125, 675, 1650, 750, 400, 1050, 500, ...
750, 850, 1250, 725, 475, 925, 1050, 925, 850, 625, ...
900, 1750, 700, 825, 550, 925, 850, 475, 750, 550, ...
725, 575, 575, 1450, 700, 450, 700, 1650, 925, 500, ...
675, 1300, 1125, 775, 850];
x=sort(data);
n=length(x);
r=x(n)-x(1);% range of the given data
nci=10; % number of class intervals
cl=fix(r/nci);

% generation of endpoints of the class intervals


fx(1)=x(1);
sx(1)=x(1)+cl;
for i= 2:nci
fx(i)=fx(i-1)+cl+1; % first values of class intervals
sx(i)=sx(i-1)+cl+1; % second values of class intervals
end
% calculating frequencies of each class intervals
for j=1:nci
count=0;
for i=1:n
if(x(i)>=fx(j) && x(i)<=sx(j))
count=count+1;
end
freq(j)=count;
end
end
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
% Display the frequency distribution
disp(' Class interval Frequency')
disp([fx' sx' freq'])
% Display the histogram
for i= 1:nci
k=1;
if(freq(i)==0)
hx1(k)=fx(i);
hx1(k+1)=sx(i);
hy1(k)=0;
hy1(k+1)=0;
plot(hx1,hy1,'-b');
hold on
else
hx(k)=fx(i);
hx(k+1)=fx(i);
hx(k+2)=sx(i);
hx(k+3)=sx(i);

hy(k)=0;
hy(k+1)=freq(i);
hy(k+2)=freq(i);
hy(k+3)=0;
plot(hx,hy,'-b');
hold on
end
end
title('Histogram');

3. Write a MATLAB program to organize the data into a frequency distribution and display the
data in a frequency polygon.
Exercise: Write a MATLAB program to organize the following data into a frequency
distribution:
700, 450, 725, 1125, 675, 1650, 750, 400, 1050, 500, 750, 850, 1250, 725, 475, 925, 1050,
925, 850, 625, 900, 1750, 700, 825, 550, 925, 850, 475, 750, 550, 725, 575, 575, 1450, 700,
450, 700, 1650, 925, 500, 675, 1300, 1125, 775, 850.
Display the data in a histogram and frequency polygon.
CODE:
function frequency_polygon
clc
clf
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
clear all
% Given data
data = [700, 450, 725, 1125, 675, 1650, 750, 400, 1050, 500, ...
750, 850, 1250, 725, 475, 925, 1050, 925, 850, 625, ...
900, 1750, 700, 825, 550, 925, 850, 475, 750, 550, ...
725, 575, 575, 1450, 700, 450, 700, 1650, 925, 500, ...
675, 1300, 1125, 775, 850];
x=sort(data); %sort the given data in ascending order
n=length(x); % number of data
r=x(n)-x(1);% range of the given data
nci=10; % number of class intervals

% calculating class length


cl=fix(r/nci);

% generation of endpoints of the class intervals


fx(1)=x(1);
sx(1)=x(1)+cl;
for i= 2:nci
fx(i)=fx(i-1)+cl+1; % first values of class intervals
sx(i)=sx(i-1)+cl+1; % second values of class intervals
end

% calculating frequencies of each class intervals


for j=1:nci
count=0;
for i=1:n
if(x(i)>=fx(j) && x(i)<=sx(j))
count=count+1;
end
freq(j)=count;
end
end

% Display the frequency distribution


disp(' Class interval Frequency')
disp([fx' sx' freq'])

% preparing Frequency Polygon


frpx(1)= fx(1)-(sx(1)-fx(1))/2.0; % initial x-coordinate of frequency polygon
frpy(1)= 0; % initial y-cordinate of frequency polygon
for i= 1:nci
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
frpx(i+1)= (fx(i)+sx(i))/2.0; % mid points of each class intervals (x-coordinates of frequency
polygon)
frpy(i+1)= freq(i); % frequencies (y-coordinates of frequency polygon)
end
frpx(nci+2)= sx(nci)+(sx(1)-fx(1))/2.0; % last x-coordinate of frequency polygon
frpy(nci+2)= 0; % last x-coordinate of frequency polygon
% Display the frequency polygon
plot(frpx, frpy, '-b'); %line plot of frequency polygon in red colour
hold on
xlabel('Data Values');
ylabel('Frequency');
grid on
title('Frequency polygon');

4. Write a MATLAB program to organize the data into a frequency distribution and find the (a)
mean, (b) median, (e) mode, (d) range, and (e) midrange.
Exercise: Write a MATLAB program to organize the following data into a frequency
distribution:
55, 60, 68, 73, 76, 84, 88, 57, 62, 70, 75, 77, 84, 90, 58, 64, 71, 75, 79, 85, 91, 58, 66, 71, 76,
80, 87, 93, 58, 66, 72, 76, 82, 88, 95, 55, 60, 68, 73, 76, 84, 88, 57, 62, 70, 75, 77, 84, 90, 58,
56.
Find the (a) mean, (b) median, (e) mode, (d) range, and (e) midrange.
CODE:
function Mean_median_mode_range_midrange
clc
clear all
% Given data
data = [55, 55, 60, 68, 73, 76, 84, 88, 57, 62, 70, 75, 77, 84, 90, 58, ...
64, 71, 75, 79, 85, 91, 58, 66, 71, 76, 80, 87, 93, 58, 66, ...
72, 76, 82, 88, 95, 55, 60, 68, 73, 76, 84, 88, 57, 62, 70, ...
75, 77, 84, 90, 58, 56];
x=sort(data); %sort the given data in ascending order
n=length(x); % number of data
r=x(n)-x(1);% range of the given data
nci=10; % number of class intervals

% calculating class length


cl=fix(r/nci);
% generation of endpoints of the class intervals
fx(1)=x(1);
sx(1)=x(1)+cl;
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
for i= 2:nci
fx(i)=fx(i-1)+cl+1; % first values of class intervals
sx(i)=sx(i-1)+cl+1; % second values of class intervals
end

% calculating frequencies of each class intervals


for j=1:nci
count=0;
for i=1:n
if(x(i)>=fx(j) && x(i)<=sx(j))
count=count+1;
end
freq(j)=count;
end
end

% Create a frequency distribution including cumulative frequency


sum_fi=0.0;
for i=1:nci
sum_fi=sum_fi+freq(i); % calculating cumulative frequency
cf(i)= sum_fi; % storing cumulative frequency
end
% Display the frequency distribution including cumulative frequency
disp('Class interval Freq cf')
disp([fx' sx' freq' cf'])

% (a) Calculating mean from the frequency distribution


sum_xi=0.0;
for i=1:nci
sum_xi= sum_xi + freq(i)*(sx(i)+fx(i))/2;
end
mean=sum_xi/sum_fi;

% (b) Calculating median from the cumulative frequency distribution


% Median is the middle value of the dataset
mcl= ceil(sum_fi/2); % median class
for i=1:nci
if (cf(i)>=mcl)
median= (sx(i)+fx(i))/2;
break;
end
end
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
% (c) Calculating mode from the cumulative frequency distribution
% Mode is the most frequent value in the dataset
mocl=max(freq); % mode class
k=1;
if (mocl==1)
disp('There is no mode')
else
for i=1:nci
if (freq(i)==mocl)
mode(k)= (sx(i)+fx(i))/2;
k=k+1;
end
end
end

% (d) Calculate the range


range = x(n) - x(1);
% (e) Calculate the midrange
midrange = (x(1) + x(n)) / 2;
fprintf('\n Mean = %.2f, Median = %.2f, Range = %.2f, and Midrange = %.2f.\n', mean,
median, range, midrange);
disp('Mode =')
disp([mode])

5. Write a MATLAB program to find the variance and standard deviation from a given set of
data.
Exercise: Write a MATLAB program to find the variance and standard deviation from the
following data:
58, 37, 78, 52, 85, 83, 82, 98, 47, 10, 96, 21, 40, 41, 24, 77, 53, 88, 90, 62, 92, 16, 79, 12, 25,
59, 32, 27, 42, 89, 14, 95, 26, 72, 20, 13, 45, 18, 84, 29, 3, 100, 38, 39, 19, 67, 33, 22, 49, 36.
CODE:
function variance_sd
clc
clear all
% Given data
data = [58, 37, 78, 52, 85, 83, 82, 98, 47, 10, 96, 21, 40, 41, 24, 77, 53, 88, 90, 62, 92, 16, 79,
12, 25, 59, 32, 27, 42, 89, 14, 95, 26, 72, 20, 13, 45, 18, 84, 29, 3, 100, 38, 39, 19, 67, 33, 22,
49, 36];
x=sort(data); %sort the given data in ascending order
n=length(x); % number of data
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
% (a) Calculating mean from ungrouped data
sum_xi=0.0;
for i=1:n
sum_xi= sum_xi + x(i); % calculating sum of xi
end
mean=sum_xi/n;

% calculating sum of (xi-xber)^2


sum_xi_xber_sqr=0.0;
for i=1:n
sum_xi_xber_sqr = sum_xi_xber_sqr + (x(i)-mean)^2;
end
%calculating variance
var=sum_xi_xber_sqr/n;

%calculating standard deviation


sd=sqrt(var);
fprintf('\n Variance = %.2f and Standard Deviation = %.2f.\n', var, sd);

6. Write a MATLAB program to organize the data into a frequency distribution and find the
variance and standard deviation.
Exercise: Write a MATLAB program to organize the following data into a frequency
distribution:
58, 37, 78, 52, 85, 83, 82, 98, 47, 10, 96, 21, 40, 41, 24, 77, 53, 88, 90, 62, 92, 16, 79, 12, 25,
59, 32, 27, 42, 89, 14, 95, 26, 72, 20, 13, 45, 18, 84, 29, 3, 100, 38, 39, 19, 67, 33, 22, 49, 36.
Find the variance and standard deviation.
CODE:
function variance_sd
clc
clear all
% Given data
data = [58, 37, 78, 52, 85, 83, 82, 98, 47, 10, 96, 21, 40, 41, 24, 77, 53, 88, 90, 62, 92, 16, 79,
12, 25, 59, 32, 27, 42, 89, 14, 95, 26, 72, 20, 13, 45, 18, 84, 29, 3, 100, 38, 39, 19, 67, 33, 22,
49, 36];
x=sort(data); %sort the given data in ascending order
n=length(x); % number of data

r=x(n)-x(1);% range of the given data


nci=10; % number of class intervals
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
% calculating class length
cl=fix(r/nci);
% generation of endpoints of the class intervals
fx(1)=x(1);
sx(1)=x(1)+cl;
for i= 2:nci
fx(i)=fx(i-1)+cl+1; % first values of class intervals
sx(i)=sx(i-1)+cl+1; % second values of class intervals
end

% calculating frequencies of each class intervals


for j=1:nci
count=0;
for i=1:n
if(x(i)>=fx(j) && x(i)<=sx(j))
count=count+1;
end
freq(j)=count;
end
end

% Calculating class values


for i=1:nci
xi(i)=(sx(i)+fx(i))/2;
end

% Display the frequency distribution including class values


disp('Class interval xi Freq')
disp([fx' sx' xi' freq'])

% (a) Calculating mean from the frequency distribution


sum_xi=0.0;
sum_fi=0.0;
for i=1:nci
sum_xi= sum_xi + freq(i)*xi(i); % calculating sum of xi
sum_fi=sum_fi+freq(i); % calculating sum of frequencies
end
mean=sum_xi/sum_fi;
% calculating sum of fi(xi-xber)^2
sum_fi_xi_xber_sqr=0.0;
for i=1:nci
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
sum_fi_xi_xber_sqr = sum_fi_xi_xber_sqr + freq(i)*(xi(i)-mean)^2;
end
%calculating variance
var=sum_fi_xi_xber_sqr/sum_fi;

%calculating standard deviation


sd=sqrt(var);
fprintf('\n Variance = %.2f and Standard Deviation = %.2f.\n', var, sd);

7. Write a MATLAB program to find the mean deviation from a given set of data.
Exercise: Write a MATLAB program to find the mean deviation from the following data:
58, 37, 78, 52, 85, 83, 82, 98, 47, 10, 96, 21, 40, 41, 24, 77, 53, 88, 90, 62, 92, 16, 79, 12, 25,
59, 32, 27, 42, 89, 14, 95, 26, 72, 20, 13, 45, 18, 84, 29, 3, 100, 38, 39, 19, 67, 33, 22, 49, 36.
CODE:
function mean_deviation
clc
clear all
% Given data
data = [58, 37, 78, 52, 85, 83, 82, 98, 47, 10, 96, 21, 40, 41, 24, 77, 53, 88, 90, 62, 92, 16, 79,
12, 25, 59, 32, 27, 42, 89, 14, 95, 26, 72, 20, 13, 45, 18, 84, 29, 3, 100, 38, 39, 19, 67, 33, 22,
49, 36];
x=sort(data); %sort the given data in ascending order
n=length(x); % number of data

% (a) Calculating mean from ungrouped data


sum_xi=0.0;
for i=1:n
sum_xi= sum_xi + x(i); % calculating sum of xi
end
mean=sum_xi/n;

% calculating sum of |xi-xber|


sum_abs_xi_xber=0.0;
for i=1:n
sum_abs_xi_xber = sum_abs_xi_xber + abs(x(i)-mean);
end
md=sum_abs_xi_xber/n;
fprintf('\n Mean deviation = %.2f.\n', md);
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
8. Write a MATLAB program to organize the data into a frequency distribution and find the mean
deviation.
Exercise: Write a MATLAB program to organize the following data into a frequency
distribution:
58, 37, 78, 52, 85, 83, 82, 98, 47, 10, 96, 21, 40, 41, 24, 77, 53, 88, 90, 62, 92, 16, 79, 12, 25,
59, 32, 27, 42, 89, 14, 95, 26, 72, 20, 13, 45, 18, 84, 29, 3, 100, 38, 39, 19, 67, 33, 22, 49, 36.
Find the mean deviation.
CODE:
function mean_deviation
clc
clear all
% Given data
data = [58, 37, 78, 52, 85, 83, 82, 98, 47, 10, 96, 21, 40, 41, 24, 77, 53, 88, 90, 62, 92, 16, 79,
12, 25, 59, 32, 27, 42, 89, 14, 95, 26, 72, 20, 13, 45, 18, 84, 29, 3, 100, 38, 39, 19, 67, 33, 22,
49, 36];
x=sort(data); %sort the given data in ascending order
n=length(x); % number of data
r=x(n)-x(1);% range of the given data
nci=10; % number of class intervals
cl=fix(r/nci);
% generation of endpoints of the class intervals
fx(1)=x(1);
sx(1)=x(1)+cl;
for i= 2:nci
fx(i)=fx(i-1)+cl+1; % first values of class intervals
sx(i)=sx(i-1)+cl+1; % second values of class intervals
end

% calculating frequencies of each class intervals


for j=1:nci
count=0;
for i=1:n
if(x(i)>=fx(j) && x(i)<=sx(j))
count=count+1;
end
freq(j)=count;
end
end

% Calculating class values


for i=1:nci
xi(i)=(sx(i)+fx(i))/2;
end
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
% Display the frequency distribution including class values
disp(' Class interval xi Freq')
disp([fx' sx' xi' freq'])

% (a) Calculating mean from the frequency distribution


sum_xi=0.0;
sum_fi=0.0;
for i=1:nci
sum_xi= sum_xi + freq(i)*xi(i); % calculating sum of xi
sum_fi=sum_fi+freq(i); % calculating sum of frequencies
end
mean=sum_xi/sum_fi;

% calculating sum of fi|xi-xber|


sum_fi_abs_xi_xber=0.0;
for i=1:nci
sum_fi_abs_xi_xber = sum_fi_abs_xi_xber + freq(i)*abs(xi(i)-mean);
end
md=sum_fi_abs_xi_xber/sum_fi;
fprintf('Mean deviation = %.2f.\n', md);

9. Write a MATLAB program to find the five number summary of a given set of data.
Exercise: Write a MATLAB program to find the five number summary of the following data:
31, 66, 89, 45, 14, 42, 62, 33, 37, 80, 81, 96, 46, 41, 73, 106, 5, 59, 3, 32, 12, 39, 72, 78, 50,
61, 60, 75, 68, 84, 97, 57, 49, 90, 74, 35, 70, 28, 58, 48, 17, 6, 103, 85, 92, 105, 86, 21, 20,
104, 36, 9, 98, 53, 22, 13, 82, 7, 94, 99.
CODE:
function five_number_summary
clc
clear all
% Given data
data = [31, 32, 66, 89, 45, 14, 42, 62, 33, 37, 80, 81, 96, 46, 41, 73, 106, 5, 59, 3, 32, 12, 39,
72, 78, 50, 61, 60, 75, 68, 84, 97, 57, 49, 90, 74, 35, 70, 28, 58, 48, 17, 6, 103, 85, 92, 105, 86,
21, 20, 104, 36, 9, 98, 53, 22, 13, 82, 7, 94, 99];
x=sort(data); %sort the given data in ascending order
n=length(x); % number of data
% Calculating L
L=x(1);
% Calculating H
H=x(n);
% Calculate Q2 (median)
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
if mod(n, 2) == 0
% If the number of elements is even, average the middle two values
q2 = (x(n/2) + x(n/2 + 1)) / 2;
for i=1:n/2
xq1(i)=x(i);
xq3(i)=x(n/2+i);
end
else
% If the number of elements is odd, take the middle value
q2 = x((n + 1) / 2);
for i=1:((n + 1) / 2)-1
xq1(i)=x(i);
xq3(i)=x((n+1)/2+i);
end
end
disp([xq1' xq3'])
% Calculate Q1 and Q3
n1=length(xq1);
if mod(n1, 2) == 0
% If the number of elements is even, average the middle two values
q1 = (xq1(n1/2) + xq1(n1/2 + 1)) / 2;
q3 = (xq3(n1/2) + xq3(n1/2 + 1)) / 2;
else
% If the number of elements is odd, take the middle value
q1 = xq1((n1 + 1) / 2);
q3 = xq3((n1 + 1) / 2);
end
disp('Five number summary is as follows:')
fprintf('\n L = %d, Q_1 = %.2f, Q_2 = %.2f, Q_3 = %.2f, and H = %d.\n', L, q1, q2, q3, H);

10. Write a MATLAB program to find the quartiles of a given set of data.
Exercise: Write a MATLAB program to find the quartiles of the following data:
41, 34, 27, 84, 11, 65, 54, 60, 96, 64, 12, 76, 91, 15, 67, 42, 10, 49, 80, 59, 83, 82, 99, 63, 38,
21, 46, 71, 78, 86, 40, 16, 97, 31, 57, 85, 73, 13, 47, 72, 23, 14, 58, 68, 45, 20, 55, 95, 48, 75.
CODE:
function quartiles
clc
clear all
% Given data
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
data = [31, 32, 66, 89, 45, 14, 42, 62, 33, 37, 80, 81, 96, 46, 41, 73, 106, 5, 59, 3, 32, 12, 39,
72, 78, 50, 61, 60, 75, 68, 84, 97, 57, 49, 90, 74, 35, 70, 28, 58, 48, 17, 6, 103, 85, 92, 105, 86,
21, 20, 104, 36, 9, 98, 53, 22, 13, 82, 7, 94, 99];
x=sort(data); %sort the given data in ascending order
n=length(x); % number of data

% Calculate Q2 (median)
if mod(n, 2) == 0
% If the number of elements is even, average the middle two values
q2 = (x(n/2) + x(n/2 + 1)) / 2;
for i=1:n/2
xq1(i)=x(i);
xq3(i)=x(n/2+i);
end
else
% If the number of elements is odd, take the middle value
q2 = x((n + 1) / 2);
for i=1:((n + 1) / 2)-1
xq1(i)=x(i);
xq3(i)=x((n+1)/2+i);
end
end
disp([xq1' xq3'])
% Calculate Q1 and Q3
n1=length(xq1);
if mod(n1, 2) == 0
% If the number of elements is even, average the middle two values
q1 = (xq1(n1/2) + xq1(n1/2 + 1)) / 2;
q3 = (xq3(n1/2) + xq3(n1/2 + 1)) / 2;
else
% If the number of elements is odd, take the middle value
q1 = xq1((n1 + 1) / 2);
q3 = xq3((n1 + 1) / 2);
end
disp(' The quartiles are as follows as follows:')
fprintf(' Q_1 = %.2f, Q_2 = %.2f, Q_3 = %.2f.\n', q1, q2, q3);

11. Write a MATLAB program to find the percentiles of a given set of data.
Exercise: Write a MATLAB program to find the percentiles of the following data:
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
41, 34, 27, 84, 11, 65, 54, 60, 96, 64, 12, 76, 91, 15, 67, 42, 10, 49, 80, 59, 83, 82, 99, 63, 38,
21, 46, 71, 78, 86, 40, 16, 97, 31, 57, 85, 73, 13, 47, 72, 23, 14, 58, 68, 45, 20, 55, 95, 48, 75.
CODE:
function percentile
clc
clear all
% Given data
data = [41, 34, 27, 84, 11, 65, 54, 60, 96, 64, 12, 76, 91, 15, 67, 42, 10, 49, 80, 59, 83, 82, 99, 63,
38, 21, 46, 71, 78, 86, 40, 16, 97, 31, 57, 85, 73, 13, 47, 72, 23, 14, 58, 68, 45, 20, 55, 95, 48,
75];
x=sort(data); %sort the given data in ascending order
n=length(x); % number of data

% Calculating percentile
k=input('Enter a positive integer between 1 and 99: ');
I=fix(k*n/100);
if (mod(k*n, 100) == 0)
pk=(x(I)+x(I+1))/2;
else
pk= x(I+1);
end
fprintf('\n The %d th Percentile p_%d = %.2f.\n',k, k, pk);

12. Write a MATLAB program to organize the data into a frequency distribution and find the first
four moments about mean 𝜇1 , 𝜇2 , 𝜇3 , and 𝜇4 . Also find 𝛽1 , 𝛽2 , 𝛾1 , and 𝛾2 .
Exercise: Write a MATLAB program to organize the following data into a frequency
distribution:
28, 30, 39, 16, 31, 81, 56, 97, 44, 18, 91, 100, 15, 12, 47, 27, 48, 71, 92, 108, 107, 61, 99, 87,
72, 13, 11, 52, 14, 94, 58, 65, 54, 66, 102, 74, 55, 95, 90, 70, 105, 25, 32, 50, 23, 24, 22, 33,
76, 20. Find the first four moments about mean 𝜇1 , 𝜇2 , 𝜇3 , and 𝜇4 . Also find 𝛽1 , 𝛽2 , 𝛾1 , and
𝛾2 .
CODE:
function moments_beta_gamma
clc
clear all
% Given data
data = [28, 30, 39, 16, 31, 81, 56, 97, 44, 18, 91, 100, 15, 12, 47, 27, 48, 71, 92, 108, 107, 61, 99,
87, 72, 13, 11, 52, 14, 94, 58, 65, 54, 66, 102, 74, 55, 95, 90, 70, 105, 25, 32, 50, 23, 24, 22,
33, 76, 20];
x=sort(data); %sort the given data in ascending order
n=length(x); % number of data
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
r=x(n)-x(1);% range of the given data
nci=10; % number of class intervals

cl=fix(r/nci);

% generation of endpoints of the class intervals


fx(1)=x(1);
sx(1)=x(1)+cl;
for i= 2:nci
fx(i)=fx(i-1)+cl+1; % first values of class intervals
sx(i)=sx(i-1)+cl+1; % second values of class intervals
end

% calculating frequencies of each class intervals


for j=1:nci
count=0;
for i=1:n
if(x(i)>=fx(j) && x(i)<=sx(j))
count=count+1;
end
freq(j)=count;
end
end

% Calculating class values


for i=1:nci
xi(i)=(sx(i)+fx(i))/2;
end
% Display the frequency distribution including class values
disp(' Class interval xi Freq')
disp([fx' sx' xi' freq'])

% (a) Calculating mean from the frequency distribution


sum_xi=0.0;
sum_fi=0.0;
for i=1:nci
sum_xi= sum_xi + freq(i)*xi(i); % calculating sum of xi
sum_fi=sum_fi+freq(i); % calculating sum of frequencies
end
mean=sum_xi/sum_fi;
% calculating sum of fi(xi-xber)^n, n=1,2,3,4
sum_for_mu_1=0.0;
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
sum_for_mu_2=0.0;
sum_for_mu_3=0.0;
sum_for_mu_4=0.0;
for i=1:nci
sum_for_mu_1 = sum_for_mu_1 + freq(i)*(xi(i)-mean);
sum_for_mu_2 = sum_for_mu_2 + freq(i)*(xi(i)-mean)^2;
sum_for_mu_3 = sum_for_mu_3 + freq(i)*(xi(i)-mean)^3;
sum_for_mu_4 = sum_for_mu_4 + freq(i)*(xi(i)-mean)^4;
end
mu_1= sum_for_mu_1/sum_fi;
mu_2= sum_for_mu_2/sum_fi;
mu_3= sum_for_mu_3/sum_fi;
mu_4= sum_for_mu_4/sum_fi;
beta_1=mu_3^2/mu_2^3;
beta_2=mu_4/mu_2^2;
gamma_1=sqrt(abs(beta_1));
gamma_2=beta_2-3;

fprintf('\n Mu_1 = %.2f, Mu_2 = %.2f, Mu_3 = %.2f, Mu_4 = %.2f.\n', mu_1, mu_2, mu_3,
mu_4);
fprintf('\n Beta_1 = %.2f, Beta_2 = %.2f, Gamma_1 = %.2f, Gamma_2 = %.2f.\n', beta_1, beta_2,
gamma_1, gamma_2);

13. Write a MATLAB program to organize the data into a frequency distribution and find the first
four moments about any point 𝜇1 ′, 𝜇2 ′, 𝜇3 ′, and 𝜇4 ′.
Exercise: Write a MATLAB program to organize the following data into a frequency
distribution:
28, 30, 39, 16, 31, 81, 56, 97, 44, 18, 91, 100, 15, 12, 47, 27, 48, 71, 92, 108, 107, 61, 99, 87,
72, 13, 11, 52, 14, 94, 58, 65, 54, 66, 102, 74, 55, 95, 90, 70, 105, 25, 32, 50, 23, 24, 22, 33,
76, 20. Find the first four moments about any point 𝜇1 ′, 𝜇2 ′, 𝜇3 ′, and 𝜇4 ′.
CODE:
function moments_about_any_point
clc
clear all
% Given data
data = [28, 30, 39, 16, 31, 81, 56, 97, 44, 18, 91, 100, 15, 12, 47, 27, 48, 71, 92, 108, 107, 61, 99,
87, 72, 13, 11, 52, 14, 94, 58, 65, 54, 66, 102, 74, 55, 95, 90, 70, 105, 25, 32, 50, 23, 24, 22,
33, 76, 20];
x=sort(data); %sort the given data in ascending order
n=length(x); % number of data
r=x(n)-x(1);% range of the given data
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
nci=10; % number of class intervals

cl=fix(r/nci);

% generation of endpoints of the class intervals


fx(1)=x(1);
sx(1)=x(1)+cl;
for i= 2:nci
fx(i)=fx(i-1)+cl+1; % first values of class intervals
sx(i)=sx(i-1)+cl+1; % second values of class intervals
end

% calculating frequencies of each class intervals


for j=1:nci
count=0;
for i=1:n
if(x(i)>=fx(j) && x(i)<=sx(j))
count=count+1;
end
freq(j)=count;
end
end

% Calculating class values


for i=1:nci
xi(i)=(sx(i)+fx(i))/2;
end
% Display the frequency distribution including class values
disp(' Class interval xi Freq')
disp([fx' sx' xi' freq'])

% calculating sum of fi(xi-a)^n, n=1,2,3,4


a=input('Enter any number from the given set of data: ');
sum_for_mu_1=0.0;
sum_for_mu_2=0.0;
sum_for_mu_3=0.0;
sum_for_mu_4=0.0;
for i=1:nci
sum_for_mu_1 = sum_for_mu_1 + freq(i)*(xi(i)-a);
sum_for_mu_2 = sum_for_mu_2 + freq(i)*(xi(i)-a)^2;
sum_for_mu_3 = sum_for_mu_3 + freq(i)*(xi(i)-a)^3;
sum_for_mu_4 = sum_for_mu_4 + freq(i)*(xi(i)-a)^4;
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)
end
mu_1= sum_for_mu_1/n;
mu_2= sum_for_mu_2/n;
mu_3= sum_for_mu_3/n;
mu_4= sum_for_mu_4/n;

fprintf('\n Mu_1 = %.2f, Mu_2 = %.2f, Mu_3 = %.2f, Mu_4 = %.2f.\n', mu_1, mu_2, mu_3,
mu_4);

14. Write a MATLAB program to display qualitative data in a circular graph (pie chart).
Exercise: A foreign automobile dealer sells English, French, German, Japanese, and Korean
automobiles. The number of such automobiles sold in a month as follows:
Country English French German Japanese Korean
Number 5 3 12 20 10

Write a MATLAB program to display the data in a circular graph (pie chart).
CODE:
function pie_chart
clc
clear all
clf
% Given data
countries = {'English', 'French', 'German', 'Japanese', 'Korean'};
numbers = [5, 3, 12, 20, 10];
% Create a pie chart
figure;
pie(numbers, countries);
% Add title and labels
title('Number of Automobiles Sold by Country');

15. Write a MATLAB program to display qualitative data in a bar diagram.


Exercise: Suppose 50 students of the department of Mathematics have participated in 5
indoor games Carrom, Chess, Ludo, Cards, and Snakes and Ladders. The following table
shows the names of the games along with the number of participants in each:
Game Carrom Chess Ludo Cards Snakes and Ladders
Number of participants 6 2 12 20 10

Write a MATLAB program to display the data in a bar diagram.


CODE:
function bar_diagram
B.Sc. (Honours) Part-1, Semester-1 Examination 2024
List of problems for Math Practical (Stat)

clc
clear all
clf
% Given data
games = {'Carrom', 'Chess', 'Ludo', 'Cards', 'Snakes and Ladders'};
participants = [6, 2, 12, 20, 10];
% Create a bar diagram
figure;
bar(participants);
% Add labels and title
xlabel('Games');
ylabel('Number of Participants');
title('Number of Participants in Each Game');

You might also like