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

Please solve using MATLAB. Will rate . Construction of user-defined functions (n

ID: 3874454 • Letter: P

Question

Please solve using MATLAB.
Will rate

. Construction of user-defined functions (numerical solvers) Write an user-defined function PunTestl with input variables A and k and output variable y The variable A is a two-dimensional data array (matrix). The function will first divide, element by element, the k-th row of A by the (k+2)-th row of A, then sum all the elements of the resulting vector and return this value to the output variable y. Note that an error message should be displayed if k+2 is larger than the number of rows of A. Cut and paste the completed m-file below a) Benchmark testing is an important tool learned in BE 1500. Benchmark test your function on A=ones (20, 50) and k=13. The expected answer for the output is y=50; b) Apply your function to a 8x6 matrix a constructed by multiplying a column vector [1 ; 8] , ' to a row vector of [ 1 : 6] , and k=S

Explanation / Answer

Solution to given problem is mentioned below. Sample execution output is also provided for reference.

File: FunTest1.m

function y = FunTest1(A, k)
% This function will first divide, element by element, the
% k-th row of A by the (k+2)-th row of A, then sum all the
% elements of the resulting vector and return this value to
% the output variable y. An error message should be displayed
% if k+2 is larger than the number of rows of A.

# Get size (number of rows, number of columns) of input matrix A
[nRows nCols] = size(A);

# Display error message in case k + 2 > number of rows of A
if(k + 2 > nRows)
    disp("Please provide a value of k such that k + 2 <= number of rows in A.")
    return
end

# Compute output result (y)
y = 0;
for col = 1 : nCols
    y = y + (A(k, col) / A(k+2, col));
end

Sample Execution Output:

(a)

(b)