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

matlab help ; The only built in function you can use for this problem is length.

ID: 3936639 • Letter: M

Question

matlab help ;

The only built in function you can use for this problem is length.

The geometric mean of a set of numbers x1 through xn is defined as the nth root of the product of numbers:

Geometric mean = (x1*x2...xn)1/n

Write a function file that will accept a vector of any length as an input argument and calculate the average and the geometric mean of the numbers in the vector. The function should return the two calculated values as output arguments.

Test your code with the following vector: [5 -10 15 2.5 30]

Explanation / Answer

function [average, geometric_mean ] = func( vector )
    len = length( vector );
    average = 0;
    geometric_mean = 1;
    for i=1:len
        average = average + vector(i);
        geometric_mean = geometric_mean*vector(i);
    end
    average = average/len;
    geometric_mean = geometric_mean/len;
end