Function Calls Given the following functions test 1 and test

Function Calls Given the following functions test 1 and test2, function res = test1(x, y) res = squareroot (x.^2 + y.^2); end function res = test2(x, y) narginchk(1, 2) if nargin == 2 res = squareroot (x.^2 + y.^2); else res = x; end end determine whether the function calls below are correct or not. If they are correct, indicate the value of the variable out. If they are in error, specify what is wrong with them You must \"desk-check\" your answers and write them out below by hand. You should not use MATLAB for this problem, unless it is to check your hand-work after you\'ve finished. out = test 1 (); out = test 1 (6); out = test 1(3, 4) out = test 2 (12); out = test 2 (6, 8); out = test 2 (8, 10, 12);

Solution

Please follow the data and description :

CODE :

function res = test1(x,y)
res = sqrt(x.^2+y.^2);
end
function res = test2(x,y)
narginchk(1,2)
  
if nargin == 2
res = sqrt(x.^2+y.^2);
else
res = x;
end
end

a) out = test1();

In this case the code rises a error as the sufficient variables are not passed to the function ofr the code to run the output. The error raises that the variable x could not be defined or undefined.

b) out = test1(6);

For the above line of code even the it rises a error as the sufficient variables are not passed to the function ofr the code to run the output. The error raises that the variable y could not be defined or undefined.

c) out = test1(3,4); :

For this the code gives the output for the variables that get the output desired. The value generated would be 5 as root of squares of the values passed is 5.

d) out = test2(12); :

This gives the value passed as the output as the condition that needs to be checked is not satisfied and thus the code goes to the else condition which outputs the value that is passed, which results the output as 12.

e) out = test2(6,8); :

This prints the value 10 as the output as the values are satisfied for the given condition and thus results the sqrt of the values which is 10.

f) out = test2(8,10,12); :

This generates the error as the arguments that need to be passed are two and the passed values are 3 so results in an error as the too many arguments for the given function call.


Hope this is helpful.

 Function Calls Given the following functions test 1 and test2, function res = test1(x, y) res = squareroot (x.^2 + y.^2); end function res = test2(x, y) nargin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site