Consider the following set of data Write an mfile that uses
Consider the following set of data. Write an m-file that uses this data to calculate each of the following statistics using \'FOR\' loops: DATA: 92.3 93.2 91.9 93.5 92.7 93.1 93.8 92.4 a. Harmonic mean: H_M=\\frac{n}{1/x_1+1/x_2+...+1/x_n} b. Geometric mean: G_M=\\sqrt[n]{x_1x_2...x_n} c. RMS average: RMS_A=\\sqrt{\\frac{x^2_1+x^2_2...x^2_n}{n}}
Solution
Here is the code for you:
dataSet = [92.3, 93.2, 91.9, 93.5, 92.7, 93.1, 93.8, 92.4];
fprintf(\'Given data set: \');
for i = 1 : length(dataSet)
fprintf(\'%.1f \', dataSet(i));
end
fprintf(\'\ \');
GeometricMean = 1;
for i = 1: length(dataSet)
GeometricMean = GeometricMean * dataSet(i);
end
GeometricMean = GeometricMean ^ (1.0 / length(dataSet));
HarmonicMean = 0.0;
for i = 1 : length(dataSet)
HarmonicMean = HarmonicMean + 1.0 / dataSet(i);
end
HarmonicMean = length(dataSet) / HarmonicMean;
RMS = 0.0;
for i = 1 : length(dataSet)
RMS = RMS + dataSet(i) * dataSet(i);
end
RMS = RMS / length(dataSet);
RMS = sqrt(RMS);
fprintf(\'The harmonic mean of the given data set is: %.1f\ \', HarmonicMean);
fprintf(\'The geometric mean of the given data set is: %.1f\ \', GeometricMean);
fprintf(\'The root mean square of the given data set is: %.1f\ \', RMS);
If you need any refinements, just get back to me.
