Question #45547

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.
The definitions of means are given as follows.

Expert's answer

`% Input`

xlow **=** input**(**'xlow: '**);**

xhigh **=** input**(**'xhigh: '**);**

% Random array

array **=** randi**([**xlow xhigh**],** 1**,** 10000**);**

% Arithmetic mean

AM **=** mean**(**array**);**

% Quadratic mean

QM **=** sqrt**(**mean**(**array**.^**2**));**

% Geometric mean

GM **=** geomean**(**array**);**

% Harmonic mean

HM **=** harmmean**(**array**);**

% Output

fprintf**(**'Arithmetic mean: %.3f\n'**,** AM**);**

fprintf**(**'Quadratic mean: %.3f\n'**,** QM**);**

fprintf**(**'Geometric mean: %.3f\n'**,** GM**);**

fprintf**(**'Harmonic mean: %.3f\n'**,** HM**);**

## Comments

## Leave a comment