function locaten(n,i,j) % help for locaten(n,i,j) % copyright 1998, D. Estep % % PURPOSE % % This silly function figures out whether n<=i, i=j. % It shows how to use if/elseif and how to combine conditions % call as >>locaten(number to be located, lower number, upper number) % % FUNCTION STATEMENT % % locaten(n,i,j) % % REQUIRED VARIABLES % % n, i, j % % USAGE % % >>locaten(number to be located, lower bound, upper bound) % Author: Don Estep % Date: 11/24/98 %% %% i <= j is required noargs = nargin; if noargs ~= 3 disp('wrong number of arguments') break end if(i>j) disp('i should be less than j') break % the break ends the function and returns to matlab end %% The if/elseif/else construction: %% if (condition 1) %% statements 1 %% elseif (condition 2) %% statements 2 %% else %% statements 3 %% end %% if condition 1 is true, then the statements 1 will be executed %% and then matlab goes to the first line past the end. If condition %% 1 is false but condition 2 is true, matlab does the statements 2 %% then goes to the first line past the end. If both condition 1 %% and condition 2 are false, then matlab does statements 3 then goes %% to the first line past the end if(n>i&n i AND n < j then do the following statements % to get am OR use !, as if(n>i!ni OR n< j. fprintf('n is strictly between %d and %d\n',i,j) elseif(n>=j) fprintf('n is greater than or equal to %12.5e\n',j) else fprintf('n is less than or equal to %14.6f\n',i) end %% note I used fprintf with various formats for the numbers %output %>>locaten(12,5,15) % n is strictly between 5 and 15 %>>locaten(12,20,21) % n is less than or equal to 20.000000 %>>locaten(12,3,8) % n is greater than or equal to 8.00000e+000