matlab help The only built in function you can use for this

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]

Solution

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

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site