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

The command error(„message) terminates function or program execution and returns

ID: 3675150 • Letter: T

Question

The command error(„message) terminates function or program execution and returns control to the keyboard. The return command returns control from a function back to the invoking program or function. The keyboard command inside a function or program returns control to the keyboard at the point where the keyboard command occurs. Within the command window the prompt becomes k>> to show that the keyboard command has executed. Any valid MATLAB command can then be executed from within the command window. Program or function execution can be made to continue just after the place where the keyboard command occurs by entering the return command from within the command window.
Enter the following function, and save it in the Current Folder. Write a program to use the function, and describe what happened when your program executes a statement like b=SquareRoot(a) for a=2, a=0 and a= -1.
function y = SquareRoot(x); y=0; if x < 0 error('the function argument cannot be negative') end if x == 0 return else y = x^0.5; end

Explanation / Answer

function y = SquareRoot(x)
y=0;
if(x < 0 )
error('the function argument cannot be negative')
end
if (x == 0 )   
return
else   
y = x.^0.5;
end
end
a= -1,0,1
b=SquareRoot(a)