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
  • Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int...

    Programming language C Please go through this carefully. Needs function void add(int *a1, int n, int *a2) Write a program addition.c that reads in an array (a1) of numbers, and creates a new array (a2) of numbers such that the first and last numbers of a1 are added and stored as the first number, the second and second-to-last numbers are added and stored as the second number, and so on. You need to check for even and odd length of...

  • 3. Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested, to return the three elements of the vector as three scalar output-argumen...

    3. Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested, to return the three elements of the vector as three scalar output-arguments in nondecreasino-roer , i.e., the first output argument equals the smallest element of the input vector and the last output argument equals the largest element. NOTE: Your function may NOT use any built-in functions, e.g., sort, min, max, median, etc. (5 points) (bonus question). Write a function called...

  • What would the function look like in MATLAB? Write a function called mysort that takes a...

    What would the function look like in MATLAB? Write a function called mysort that takes a 3-element vector as its sole arguments. It uses if statements, possibly nested, to return a 3-element vector with its elements in non-decreasing order, i.e., the first element in the returned vector equals the smallest element of the input vector and the last element equals the largest element in the input vector. NOTE: Your function should not use any built-in functions, e.g., sort, min, max,...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • 2. A. Write a MATLAB code to plot the function ?(?) = 4 − ? ^2...

    2. A. Write a MATLAB code to plot the function ?(?) = 4 − ? ^2 * ? ^−3? for the time range 0 ≤ ? ≤ 3 seconds. Plot the function using the following two methods: I. Use MATLAB’s colon operator and vector operations. II. Use iterations (for-loop) and scalar operations. B. Let ? = −3, 1, 5, … ,21. I. Find the summation of the elements of ? that are greater than the 4 ?ℎelement and less than...

  • programing C,already write part of code (a) Write a function print.array that receives an array of...

    programing C,already write part of code (a) Write a function print.array that receives an array of real numbers and the number of el stored in the array. This function must display the elements of the array in order with each one on a line by itself in a field width of 7 with two decimal places. (See sample output. Recall the format specifier would be "%7.21f"). (b) Sometimes we want to treat an array as a mathematical vector and compute...

  • a) Write a function called print_doubles that prints the first n elements of an array of...

    a) Write a function called print_doubles that prints the first n elements of an array of doubles. Assume the array is long enough. Example: double v[5] = {1,2,3,4,5}; print_doubles(v, 5); // prints [1,2,3,4,5] b) Write a function with the following signature: void get_min_max(const double values[], int n, double *pmin, double *pmax); that returns in output parameters *pmin the value of the minimum element in array values and in *pmax the maximum element. Parameter n represents the size of array values....

  • MATLAB: write a function that retuns logical true of vector or matrics or scaler is empty...

    MATLAB: write a function that retuns logical true of vector or matrics or scaler is empty Write a function that retuns logical true of vector or matrics or scaler is empty %% P2: Check for an empty matrix % Write a function myIsEmpty which takes one input (scalar, vector, matrix, ...) % and returns logical true if the input is empty and false otherwise. % DO NOT USE MATLAB's isempty FUNCTION! % % Example: An input of [] should result...

  • Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as...

    Matlab code 4) Write a function leadzero(v) which takes as input a vector v and as output, c, returns the number of zeros at the beginning of v (number of zero elements before any non-zero element). For example, for input (-5, 5, 11] the output would be 0. If the input is [0, 0, 3, 0, 0, 0], the output would be 2. If the input is [0, 0, 0, 0, 7, 4) the output would be 4. 5) Write...

  • Problem Summary This is a basic problem that uses a for-loop. Write a function called vector_multiply...

    Problem Summary This is a basic problem that uses a for-loop. Write a function called vector_multiply that takes two vectors of the same length as input arguments and returns one row vector as output Each component of the output vector is the product of the corresponding components in the two input vectors. However, this function must not use an array operation it must instead use a for-loop. For example, >>product - vector_multiply([1, 2, 3, 6], [9, 6, 4, 0]) product...

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