Question

[MATLAB] Write a function called myMultProd.m that computes the cumulative product of the elements in a...

[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 it’s a 1, use the procedure in part (a). If it’s 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find required MATLAB code along with necessary details in comments below:

--------------------------------------------------------------------------------------------------------- main_script.m

clear all; clc; close all;

x = [2 3 4 2];

myMultProd1_product=myMultProd(x,1) % actual product computed using myMultProd arg=1

myMultProd2_product=myMultProd(x,2) % actual product computed using myMultProd arg=1

actual_product=cumprod(x) % actual product computed using comprod

------------------------------------------------------------------------------------------------------------------------------ myMultProd.m

function y=myMultProd(x,arg)

if ~isvector(x) % ensure that x is a vector
if isempty(x) % check if x is empty
y=[];
end
error('Not a vector input');
return;
end

if ~isreal(x) % ensure that x has no imaginary values
error('Inputs not real');
return;
end

if ~(arg==1 || arg==2) % ensure that arg is either 1 or 2
error('Second argument invalid');
return;
end

if arg==1 % if arg is 1, use the first method
for i=1:length(x)
prodv=x(i);
for j=1:i-1
prodv=prodv*x(j); % multiply all previous values
end
y(i)=prodv;
end
elseif arg==2 % if arg is 1, use the second method
  
for i=1:length(x)
prodv=x(i);
prodv=prodv*prod(x(1:i-1)); % multiply all previous values
y(i)=prodv;
end
  
end
end

=================================== SCREENSHOT OF CODE

================================ SAMPLE OUTPUT

Add a comment
Know the answer?
Add Answer to:
[MATLAB] Write a function called myMultProd.m that computes the cumulative product of the elements in a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT