Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Matlab: The geometric mean GM of a set of n positive numbers x1, x2, ..., xn

ID: 3713267 • Letter: I

Question

In Matlab:

The geometric mean GM of a set of n positive numbers x1, x2, ..., xn is defined by: GM = (x1 × x2 × m )1/n Write a user-defined function that calculates the geometric mean of a set of numbers. The geometric mean is useful for calculating the average of rates. The following table gives the inflation rates in the US from 1978 to 1987 (inflation of 7.6% means 1.076). Use your user-defined function to calculate the average inflation for the ten-year period. Year 197819791980198 19821983 1984 1985 1986 1987 Inflation 076 1131.135 1.1031.062 .03 .043 1.036 1.019 1.036 rate

Explanation / Answer


#GM.m
#The matlab function that takes vector of inflation
#and then find the mean of geometric prouct
function mean=GM(inflation)
  
#set product=0
product=0;
#get size of the vector
size=length(inflation);
#calculate the product of vector elements
#x1.x2.x3....xn
product=prod(inflation);
#raise product to 1/size
mean=product^(1/size);
  
end

Sample Output:

>> inflation=[1.076 1.113 1.135 1.103 1.062 1.03 1.043 1.036 1.019 1.036]
inflation =

1.0760 1.1130 1.1350 1.1030 1.0620 1.0300 1.0430 1.0360 1.0190 1.0360

>> mean=GM(inflation)
mean = 1.0646

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote