Task: Create a function (save it to a file) to calculate the square root of an input value, but return NaN if the input is negative or not a number. Write a script to test this function by passing several different values.
Modern ideas of computer programming encourage breaking programs into small, isolated parts (called subprograms) that can be checked independently. An additional feature of these programming parts, is that they can use only certain variables and can change only certain variables. They provide a level of isolation for variables.
These subprograms are called functions in MATLAB. Input and output variables are specified explicitly. Any variable defined or changed inside a function is unknown outside of the function unless it is explicitly returned to the calling program. Outside variables are unknown inside.
Each function is usually specified in a separate file, but recent versions of MATLAB allow functions to be included at the end of a script. However, these functions specified within a script file are accessable only by the script. To make functions generally available, they need to be in their own file.
An example function follows. These lines are in a file called MyDouble.m. By convention, the function name and file name must be the same. The file needs to be in your current working folder so it can be found. The first line must start with the keyword function.
function D = MyDouble(A) % this function doubles the input value if it is a number % calling sequence: D = MyDouble(A) if ~isnumeric(a) % if the input is not a number, send a warning % but don't stop the calling script warning('MyDouble: input is not a number') D=NaN; else D = 2*A; t = 3*A; %this result is not available outside of the function. A = pi; %this does not affect the value of A outside the function. end end % this last end is not always required if the function is in a separate file.Note that this function only knows the value A and will return only the value D. Changing A in the function only affects calculations inside the function.
A new test function is introduced (isnumeric()) which returns the value true if a variable is a number (real or integer). There are many other functions of this sort. We have already seen isnan().
You can use this function in the following way:
% test script for MyDouble x=1; xd=MyDouble(x); % should return 2 y=0; yd=MyDouble(y); % should return 0 z=-1; zd=MyDouble(z); % should return -2 c='X'; cd=MyDouble(c); % should print a warning and return NaNNotice that the input variable does not need to be called A nor the output variable D. These variables are called "dummy variables" because they take the name of the variables in the calling statement.
Only one function can be defined in each file. It is possible to define other functions within a function file, but these subfunctions are only known to the primary (first) function that is defined.
It is also possible to add function code to a general script. However, the functions will be available only to the main script in the file. Older versions of MATLAB do not allow this.
The command warning('text') can be use used anywhere in MATLAB scripts or functions. It will print "Warning: " followed by whatever "text" you pass as an argument.
Every function should have comments after the first line. These comments are printed by the command help. For the function defined above, help MyDouble would have the following response.
>> help MyDouble % this function doubles the input value if it is a number % calling sequence: D = MyDouble(A)
Flow chart to accomplish the task is
%%% test commands: test all possibilities in the function %%% define function %%% add documentation to the function %%% test for numeric value %%% test for negative input value %%% return NaN %%% otherwise %%% return the square root %%% close the function definitionThe script to accomplish the task. Note that the function definition is inside the test script for convenience. The function should be moved to its own file once it is completely tested.
%%% test commands: test all possibilities in the function x=2;y=F81(x);disp([' the square root of ' num2str(x) ' is ' num2str(y)]) x=0;y=F81(x);disp([' the square root of ' num2str(x) ' is ' num2str(y)]) x=-2;y=F81(x);disp([' the square root of ' num2str(x) ' is ' num2str(y)]) %%% define function function OUT = F81(IN) %%% add documentation to the function % calling sequence: % OUT = F81(IN) % calculate the square root of the input if IN >=0 % return NaN if the input is negative or non-numeric %%% test for numeric value if isnumeric(IN) %%% test for negative input value if IN < 0 %%% return NaN OUT = NaN; %%% otherwise else %%% return the square root OUT=sqrt(IN); %%% close the function definition end else warning('Square root only works on numeric values') OUT = NaN; end % end of negative test end % end of isnumeric test end % end of function