Let’s write a function file range.m that calculates the difference between the maximum and minimum value in a vector. The file should return a reply. Open an edit window. Create a function file like the one below.
% the m-file is created by MatlabCorner.COM % The m-file calculates the difference between the largest % and smallest element in vector q. function r=range(q) % defines a function file range.m qmax=max(q); % finds the maximum value in the vector q. qmin=min(q); % finds the minimum -value in the vector q. r=qmax-qmin; % gives the value that should be returned.
Save the m-file as range.m. Now please enter a vector q=[ 1 3 5 7 9] in the command window. Run the m-file by simply writing range(q) after the prompt in the command window.
range(q) % do not forget to press enter.