In: Computer Science
Using Matlab, write a function that takes numeric data as its input argument and prints a message to the Command Window stating if the number is positive, or negative, or 0. This function should transfer an output value to the Workspace ONLY when the input value is negative.
Please include a copiable code and the associated screenshots.
Let us step by step implement funciton following given points in question,
Step 1. Check if numeric data entered: To check for numeric input we can use isnumeric function.
if(isnumeric(input_to_function))
% numeric entered now proceed to next step
else
disp('please enter numeric input');
end
Step 2. prints a message to the Command Window stating if the number is positive, or negative, or 0.
function output_value = check_number(x)
if(isnumeric(x))
% numeric entered now proceed to next step
if x>0
% number greater than 0 display positve
disp('Positive');
elseif x<0
% check if number less than 0, that is negative
% transfer output to workspace
disp('Negative')
else
disp('Zero');
end
%end of if statement
else
% number not numeric
disp('please enter numeric input');
end
end
Step 3: This function should transfer an output value to the Workspace ONLY when the input value is negative. we simply pass output_value with input_parameter in our case x.
Final Solution:
function output_value = check_number(x)
if(isnumeric(x))
% numeric entered now proceed to next step
if x>0
% number greater than 0 display positve
disp('Positive');
elseif x<0
% check if number less than 0, that is negative
% transfer output to workspace
disp('Negative')
output_value = x;
else
disp('Zero');
end
%end of if statement
else
% number not numeric
disp('please enter numeric input');
end
end
Output: