Question #45299

Write a single program that calculates the arithmetic mean (average), rms average, geometric mean
and harmonic mean for a set of n positive numbers. Your program should take two values xlow and
xhigh and generate 10000 random numbers in the range [xlow…xhigh], and should print out arithmetic
mean (average), rms average, geometric mean and harmonic mean.

Expert's answer

close all; clear all; clc;

N = 10000;

xlow = input('Enter Xlow: ');

xhigh = input('Enter Xhigh: ');

if xhigh < xlow

disp('Xhigh must be bigger then Xlow! Try again!')

else

X = xlow + (xhigh - xlow).*rand(1,N);

X_m = sum (X)/N;

X_rms = sqrt(sum (X.^2)/N);

X_gm = prod(X.^(1/N));

X_hm = N/sum(1./X);

disp(['Average of X = ', num2str(X_m)]);

disp(['Root mean square of X = ', num2str(X_rms)]);

disp(['Geometric mean of X = ', num2str(X_gm)]);

disp(['Harmonic mean of X = ', num2str(X_hm)]);

end

N = 10000;

xlow = input('Enter Xlow: ');

xhigh = input('Enter Xhigh: ');

if xhigh < xlow

disp('Xhigh must be bigger then Xlow! Try again!')

else

X = xlow + (xhigh - xlow).*rand(1,N);

X_m = sum (X)/N;

X_rms = sqrt(sum (X.^2)/N);

X_gm = prod(X.^(1/N));

X_hm = N/sum(1./X);

disp(['Average of X = ', num2str(X_m)]);

disp(['Root mean square of X = ', num2str(X_rms)]);

disp(['Geometric mean of X = ', num2str(X_gm)]);

disp(['Harmonic mean of X = ', num2str(X_hm)]);

end

## Comments

## Leave a comment