I am having come troubles starting my matlab assignment...any help would be much
ID: 3630663 • Letter: I
Question
I am having come troubles starting my matlab assignment...any help would be much appreciated & will be rated FAST.
Background
There are various ways to take the mean, or average, of a set of numbers. In this assignment we will calculate 3 different means for a set of numbers: arithmetic mean, geometric mean and harmonic mean. You are already well aware of the arithmetic mean (other called the average) and might already be familiar with the geometric mean and harmonic mean.
Arithmetic Mean:
The arithmetic mean refers to the sum of all values divided by the total number of values. This can be formally represented by:
Geometric Mean:
The geometric mean is similar to the arithmetic mean except that the numbers are multipled and then the nth root (where n is the count of numbers in the set) of the resulting product is taken. This can be formally represented by:
Harmonic Mean:
The harmonic mean is yet another way of calculating the mean for a set of numbers. You can think of the harmonic mean as the reciprocal of the arithmetic mean of the reciprocals. This can be formally represented by:
Code Requirements
You are required to use a while loop and a for loop in this assignment, one for prompting the user to enter a number and another for doing your calculations. You will lose marks if you don't use both of these in your solution.
Do not use any built-in MATLAB functions in this assignment except for the following which are allowed: clear, clc, if, input, for, while, fprintf
this is my expected output which must be identical. thank you in advance
Explanation / Answer
The code below does what you asked for. It should work if you copy and paste it.
function means
A=[];
a=input('Enter number (0 or negative to end)');
n=1;
A(n,:)=a;
if A(n,:)>0
while A(n,:)>0
n=n+1;
A(n,:)=input('Enter number (0 or negative to end)');
g=0;
p=1;
h=0;
for i=1:n-1
g=g+A(i);
p=p*A(i);
h=h+(1/A(i));
end
end
fprintf('Arithmetic mean is:%d ',g/(n-1))
fprintf('Geometric mean is:%d ',p^(1/(n-1)))
fprintf('Harmonic mean is:%d ',(n-1)/h)
elseif A(n,:)<0
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.