MATLAB Functions n n n Builtin Functions Implemented
MATLAB Functions n n n Built-in Functions Implemented Functions M-files: Scripts, Functions
Built-in Functions n Internal functions; (abs, sqrt, exp, sin, …) n Useful constants: pi : 3. 14159265. . . eps : Floating-point relative precision, 2 -52 realmin: Smallest floating-point number, 2 -1022 realmax: Largest floating-point number, (2)21023 You may overwrite function names with a new variable: [eps = 1. e-6] n
Implemented Functions n n Many functions are in M-files, with visible and modifiable code. zeros, ones, rand, magic, gamma, sinh, and … R = [ 10 * rand(3, 2) , magic(3) ] R= 9. 3181 8. 4622 8. 0000 1. 0000 6. 0000 4. 6599 5. 2515 3. 0000 5. 0000 7. 0000 4. 1865 2. 0265 4. 0000 9. 0000 2. 0000
M-files: user defined functions n Text files containing MATLAB code Must be saved with. m extension n M-files execute with the file-name n … a =3; Function C = file-name (a, b) b =4; C = sqrt((a. ^2)+(b. ^2)) C =file-name(a, b) C= 5
M-files: scripts vs functions Script M-Files Function M-Files No arguments (input or output) Accept input / output arguments Operate on data in the workspace Internal variables (local to the function) Useful for automating any repetitive task Useful for application programming
Function M-Files Keyword Output Arg. Function name Input Arg. function f = fact(n) <<Function syntax definition line << H 1 line % fact: Factorial % fact(N) returns N! << Help text % fact(N) = prod(1: N) f = prod(1: n); << Function body
Sub-Functions function [mean, stdev] = stat(x) n = length(x); mean = avg(x, n); stdev = sqrt(sum((x-avg(x, n)). ^2/n)); function m = avg(x, n) m = sum(x)/n; Must be saved as stat. m n avg, only is visible to the other functions in this file n
Functions Details n If file_name and function_name are different, the internal (function) name is ignored. n A return statement forces early return, before the end of function encountered. n MATLAB upload functions to memory. n The clear statement unloads memory resident functions.
A Simple Function function y = average(x) % AVERAGE Mean of vector elements. % AVERAGE(X), where X is a vector, is the mean X. % Non-vector input results in an error. [m, n] = size(x); if (~((m==1) | (n==1)) | (m==1 & n==1)) error('Input must be a vector') return; end % Actual computation starts here: y = sum(x) / length(x);
Function Definition n Many input & output arguments: function [x, y, z] = sphere(theta, phi, rho) n No output arguments: function printresults(x) function [] = printresults(x)
nargin , and nargout function C = testarg 1(a, b) % Just a test if (nargin == 1), C = a. ^2; elseif (nargin == 2), C = a + b; end n = nargin('fun') n = nargout('fun')
function [x 0, y 0]= myplot(fname, lims, npts, angl, subdiv) % MYPLOT Plot a function. % MYPLOT(fname, lims, npts, angl, subdiv) % The first two input arguments are required. . . if nargin < 5, subdiv = 20; end if nargin < 4, angl = 10; end if nargin < 3, npts = 25; end. . . if (nargout == 0), plot(x, y) else (x 0 = x; y 0 = y; ) end
- Slides: 12