Answer on Question #45470, Programming, Mat LAB | Mathematica | MathCAD | Maple
Problem.
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. Flow Chart also.
Solution.
Code
function test()
%Clears screen
clc();
% Inputs xlow and xhigh
xlow = input('xlow: ');
xhigh = input('xhigh: ');
% Generates 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);
% Outputs result
fprintf('Arithmetic mean: %.3f\n', AM);
fprintf('RMS mean: %.3f\n', RMS);
fprintf('Geometric mean: %.3f\n', GM);
fprintf('Harmonic mean: %.3f\n', HM);
end
% Computes arithmetic mean
function mean = arithmetic_mean(array)
mean = sum(array)/length(array);
end
% Computes geometric mean
function mean = geometric_mean(array)
mean = prod(array.^(1/length(array)));
end
% Computes harmonic mean
function mean = harmonic_mean(array)
mean = length(array)/sum(array.^(-1));
end
% Computes rms mean
function mean = rms_mean(array)
mean = sqrt(sum(array.^2)/length(array));
endResult
Command Window
+□××
xlow: 1001
xhigh: 1019
Arithmetic mean: 1010.004
RMS mean: 1010.019
Geometric mean: 1009.989
Harmonic mean: 1009.974
fx >>

Flowchart
http://www.AssignmentExpert.com/
Comments