Question #45470

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.
1

Expert's answer

2014-09-16T06:36:23-0400

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));
end


Result

Command Window

+□××


xlow: 1001
xhigh: 1019
Arithmetic mean: 1010.004
RMS mean: 1010.019
Geometric mean: 1009.989
Harmonic mean: 1009.974
fx >>
![img-0.jpeg](img-0.jpeg)
Flowchart
http://www.AssignmentExpert.com/

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!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS