For questions 57 create a user defined function that takes a

For questions 5-7 create a user defined function that takes a vector of any size as an input argument. The output should whatever is described in the question Each function file must use a for loop in its calculations (even if that isn\'t the most efficient way to do the calculation). The built-in function length may be helpful to you. Test each function with each of the following vectors:[1 2 3 4 5] [10 20 30 40 50] Add up all the values of the elements and returns the sum. (You can check your answer with the bull-in function sum).

Solution

function total = sum(vec)
   total = 0;
   for i = 1:length(vec)
       total = total + vec(i);
   end
end

a = [1 2 3 4 5];
b = [10 20 30 40 50];
sum(a)
sum(b)

 For questions 5-7 create a user defined function that takes a vector of any size as an input argument. The output should whatever is described in the question

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site