Answer on Question #45469, Programming, Mat LAB | Mathematica | MathCAD | Maple
Problem.
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.
Solution.
Code
%Clear screen
clc();
% Input
xlow = input('xlow: ');
xhigh = input('xhigh:');
% Random array
array = randi([xlow xhigh], 1, 10000);
% Arithmetic mean
AM = sum(array)/length(array);
% RMS mean
RMS = sqrt(sum(array.^2)/length(array));
% Geometric mean
GM = prod(array).^(1/length(array));
% Harmonic mean
HM = length(array)/sum(1./array);
% Output
fprintf('Arithmetic mean: %f\n', AM);
fprintf('RMS mean: %f\n', RMS);
fprintf('Geometric mean: %f\n', GM);
fprintf('Harmonic mean: %f\n', HM);Result
Command Window
+1 0 P X
xlow: 13
xhigh: 15
Arithmetic mean: 14.009800
RMS mean: 14.033688
Geometric mean: 13.985834
Harmonic mean: 13.961852
PX >>
Comments