Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Organizing numbers is of great importance in todays world and calculating the me

ID: 3839294 • Letter: O

Question

Organizing numbers is of great importance in todays world and calculating the mean of a set of data/numbers is often necessary. Mathematicians are often interested in the algebraic mean (the mathematical averaging of all numbers). Statisticians are often interested in the middle number of the data (median) once or the average of the two middle numbers once sorted if the data set has an even number of elements) and Engineers are often interested in the Geometric mean which is the nth root of the product of all n numbers.

Write a MATLAB program to accept a non-empty set of numbers from a user (entered as a single
input) and then calculate and display the above means in an organized fashion.

Explanation / Answer

vec=input('Enter the values enclosed in square bracket'); %take input from user
vec=sort(vec); % sort the vector so that we can find the median

%calculate mean
sum=0;
for i=1:length(vec)
  
sum=sum+vec(i);

end
mean=sum/length(vec);
fprintf('mean= %f ',mean);

%calculate median
if(mod(length(vec),2) ==0)
median=(vec(length(vec)/2)+vec((length(vec)/2)+1))/2;
fprintf('median= %f ',median);
else
median=vec(length(vec)/2);
fprintf('median= %f ',median);
end

%calculate geometric mean
product=1;
for i=1:length(vec)
  
product=product*vec(i);

end
g_mean=nthroot(product,length(vec));
fprintf('geometric mean= %f ',g_mean);
  
  
  
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote