Question #45476

Matlab: write separate functions arithmetic_mean(), rms_average(), harmonic_mean(), geometric_mean() which takes the data array as the argument and compute the respective quantities. Write a script averages which take two values xlow and xhigh and generate 10000 random numbers in
the range [xlow…xhigh], and calls the appropriate functions to compute arithmetic mean (average), rms average, geometric mean and harmonic mean.

Expert's answer

Answer on Question #45476, Programming, Mat LAB | Mathematica | MathCAD | Maple

Problem.

Matlab: write separate functions arithmetic_mean(), rms_average(), harmonic_mean(), geometric_mean() which takes the data array as the argument and compute the respective quantities. Write a script averages which take two values xlow and xhigh and generate 10000 random numbers in the range [xlow...xhigh], and calls the appropriate functions to compute arithmetic mean (average), rms average, geometric mean and harmonic mean.

Solution.

Code (arithmetic_mean.m)


function mean = arithmetic_mean(array)
mean = sum(array)/length(array);
end


Code (geometric_mean.m)


function mean = geometric_mean(array)
mean = prod(array.^(1/length(array)));
end


Code (harmonic_mean.m)


function mean = harmonic_mean(array)
mean = length(array)/sum(array.^(-1));
end


Code (rms_mean.m)


function mean = rms_mean(array)
mean = sqrt(sum(array.^2)/length(array));
end


Code (test.m)


%Clear screen
clc();
% Input
xlow = input('xlow: ');
xhigh = input('xhigh: ');
% Random array
array = randi([xlow xhigh], 1, 10000);
% Arithmetic mean
AM = arithmetic_mean(array);
% RMS mean
RMS = rms_mean(array);
% Geometric mean
GM = geometric_mean(array);
% Harmonic mean
HM = harmonic_mean(array);
% Output
fprintf('Arithmetic mean: %.3f\n', AM);
fprintf('RMS mean: %.3f\n', RMS);
fprintf('Geometric mean: %.3f\n', GM);
fprintf('Harmonic mean: %.3f\n', HM);
Result
Command Window
+□>

txt

xlow: 10

xhigh: 13

Arithmetic mean: 11.499

RMS mean: 11.553

Geometric mean: 11.445

Harmonic mean: 11.390

```


fs >>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS