Part 1 Create a MATLAB function named sumprod that meets the

Part 1:
Create a MATLAB function named sumprod that meets the following specifications:

• It accepts three input arguments and returns two output arguments.

• The first output argument is the sum of the three inputs. The second output is the product of the three inputs.

• The function itself should be silent, i.e., it should not prompt the user for input nor should it print anything.

Part 2:
Write a MATLAB script named testps that performs the following tasks:

• Prompts the user to enter the number of times N to repeat a loop.

• Calls your sumprod function within a for loop with the following properties:

o The loop executes for N iterations, starting at 1 with an increment of 1.

o If the looping variable’s value is even, sumprod is called with these arguments:

1) Input argument 1 = looping variable

2) Input argument 2 = looping variable * 3

3) Input argument 3 = looping variable ^ 2

o If the looping variable’s value is a multiple of three, sumprod is called with these arguments:

1) Input argument 1 = looping variable + 5

2) Input argument 2 = looping variable / 2

3) Input argument 3 = looping variable o If the loop variable’s value is neither of the above cases, then the input arguments are all zero.

o Display the output at each iteration using the fprintf function

Solution

sumprod function

function [sum,prod] = sumprod(a, b, c)

sum=a+b+c;
prod=a*b*c;

end

testps script

N=input(\'Enter value of N\'); %takes input N

for n1=1:N
fprintf(\'Iteration %d values\ \',n1);
if(rem(n1,2)==0) % if n1 is even
[sum,prod]=sumprod(n1,n1*3,n1*n1);
fprintf(\'loop variable is even: sum=%d product=%d\ \',sum,prod); %prints values
end
if(rem(n1,3)==0) %if n1is multiple of 3
[sum,prod]=sumprod(n1+5,n1/2,n1);
fprintf(\'loop variable is multiple of 3: sum=%d product=%d\ \',sum,prod);
end
if(rem(n1,2)~=0 && rem(n1,3) ~= 0) %if n1 is neither
[sum,prod]=sumprod(0,0,0);
fprintf(\'loop variable is neither of the two cases: sum=%d product=%d\ \',sum,prod);
end
end

Part 1: Create a MATLAB function named sumprod that meets the following specifications: • It accepts three input arguments and returns two output arguments. • T
Part 1: Create a MATLAB function named sumprod that meets the following specifications: • It accepts three input arguments and returns two output arguments. • T

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site