In: Computer Science
Please solve the following problem for MATLAB
Write a user-defined function that calculates the average and the standard deviation of a list of numbers. Use the function to calculate the average and the standard deviation of the following list of grades : 80 75 91 60 79 89 65 80 95 50 81
The following is the MATLAB code for above question. It has met all the requirements stated in the question.
grades = [80 75 91 60 79 89 65 80 95 50 81];
function [m,sd] = meanandstd(x) %create a user defined function to calculate mean and standard deviation without using inbuilt functions
n = length(x); %store the length of grades in k
s = 0; %declaring sum as s, since sum is an inbuilt function
for i = 1: n
s=s+x(i);
end
m = s/n; %calculate mean
for i = 1: n
sd = ((x(i)-m).^2); %calculate standard deviation
sd = sqrt((sd)/n);
end
end
[Mean,Standard_Deviation] = meanandstd(grades)
I am also attaching the output and code screenshot for your reference. It will help you to cross check if any error.
Output and code screenshot:
#Please dont forget to upvote if you find the solution helpful. Feel free to ask doubts if any, in the comments section. Thank you.