In class we wrote some pseudocode stuff that looks like code

In class, we wrote some pseudocode (stuff that looks like code, but isn\'t a specific language) to compute the mean and the median of a data set x. Write the pseudocode for the function stdDeviation(x) that receives an input vector x and returns the standard deviation of the dataset. Your function must include a call to the function computeMean(x) as described in class. The equation for standard deviation is s_x = sqaureroot sigma (x_i - bar x)^2/n - 1. You may use length (x) to get the number of terms in the data set. Your first line should read function [sD]=stdDeviation(x)

Solution

% matlab code standard deviation of vector

% compute mean
function [mean] = computeMean(x)
sum = 0;
for i=1:length(x)
sum = sum + x;
end
mean = sum/double(length(x));
end

% compute sttandard deviation
function [sD] = stdDeviation(x)
mean = computeMean(x);
n = length(x);
variance = 0;
for i=1:n
t = (x(i)-mean);
variance = variance + power(t,2);
end
sD = sqrt(variance/(n-1));
end

 In class, we wrote some pseudocode (stuff that looks like code, but isn\'t a specific language) to compute the mean and the median of a data set x. Write the p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site