Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

[MATLAB] Write a function called myMultProd.m that computes the cumulative product of the elements in a vector. The cumulative product, pj, of the jth element

[MATLAB] Write a function called myMultProd.m that computes the cumulative product of the elements in a vector. The cumulative product, pj, of the jth element of the vector x, xj, is defined by pj = (x1)(x2) (xj) for j = 1:length of the vector x. DO NOT USE CUMPROD For example, when you run your function it should look like this:

>> x = [2 3 4 2]; >> myMultProd(x) >> ans = 2 6 24 48 That is, the function returns: 2, 2*3, 2*3*4, 2*3*4*2 where 2 is p1, 2*3 is p2, 2*3*4 is p3, etc.

a) Do this first using two for loops to explicitly carry out the calculation element-byelement. The inner loop should accumulate the product and the other loop should move through the elements of the vector p.

b) Write a line of code to replace (but do not actually remove, see next part of the problem) the entire inner for loop by using the prod function.

c) Add another argument to myMultProd which can either be a 1 or a 2. If its a 1, use the procedure in part (a). If its a 2, instead use the single line of code from part (b). So now your code should work like this: >> myMultProd(x,2) The output should be the same for both methods. You can check to make sure the output is correct by using the cumprod function, which does the same thing as the function you just wrote.

d) Add checks to your function that produce error or warning messages to make sure that:

- the input vector is a vector of numbers and not any other data type

- that the numbers in the vector are real (and not imaginary)

- the input vector is not a matrix - if the input vector is an empty array, then the output should also be an empty array

- the second input is a 1 or 2

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions