Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem In this problem, you are asked to write two versions of a MATLAB user-de

ID: 3216169 • Letter: P

Question

Problem In this problem, you are asked to write two versions of a MATLAB user-defined function, named calpro.m, that has three input arguments and one output argument The input arguments are to be (in this order): column vectors x, y, and z, all of the same length. The output argument is to be: column vector w, whose elements are calculated using the formula shown below, where n is the length of the input vectors, and i denotes the ith element of each input vector. . Part I: Write a version of the user-defined function that satisfies all the following requirements 1. If the function is called with only two input arguments, then the third input argument must be set equal to y. 2. If the function is called with only one input argument, then the following error message must be generated: 'Not enough input arguments. Output cannot be calculated." 3. The calculation of w must use for-loops 4. If either x or y contains one or more elements equal to zero, then the following warning message must be generated: "Possible division by zero. Part II: Write another version of the user-defined function that satisfies all the following requirements: 1. The calculation of w must not use any type of loops. 2. If the input vectors x and y are identical, then the following warning message must be generated: 'First input vectors are identical.

Explanation / Answer

%%%%%%VERSION 1

function w= calpro( x,y,z )
%Function that calculates vector w
% We specify the cases
switch nargin
case 2
z=y;
case 1
msg = 'Not enough input arguments.Output cannot be calculated';
error(msg)
end

n=length(x);
%% We warn if there's a possible division by zero
for i=1:n
if x(i)==0 || y(i)==0
msg = 'Possible division by 0';
warning(msg)
end
end

%% Then we calculate the sum of the absolute value of the product of x(i)y(i)
sumxy=0;

for i=1:n
sumxy=sumxy +abs(x(i)*y(i));
end

%%Now, we calculate w
w=ones(n,1);
for i=1:n
w(i)= (x(i)-y(i))^3 + (x(i)^4)*z(i)/sumxy;
end


end

%%%%%%%%%%%%%VERSION 2

function w= calpro2( x,y,z )
%Version 2 of Function that calculates vector w without loops

% We warn if x=y

if x==y
msg = 'First input vectors are identical';
warning(msg)
end

% Then we calculate the sum of the absolute value of the product of x(i)y(i)
% For doing that
% we create new vectors with absolute values and take the dot product
x2=abs(x);
y2=abs(y);
sumxy=dot(x2,y2);
%Now, we calculate w
%Using the operations .^ .* and ./ because we are
%operating on vectors, not in scalars
  
w= (x-y).^3 + ((x.^4).*z)./sumxy;


end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote