270 likes | 391 Views
CMPS 1371 Introduction to Computing for Engineers. FUNCTIONS. Functions. MATLAB uses function names consistent with most major programming languages. For example sqrt sin cos log. Function Input can be either scalars or matrices. Using Predefined Functions. Functions consist of Name
E N D
Functions • MATLAB uses function names consistent with most major programming languages For example • sqrt • sin • cos • log
Using Predefined Functions • Functions consist of • Name • Input argument(s) • Output sqrt (x) = result • In MATLAB sqrt(4) ans = 2
Some functions require multiple inputs • Remainder function returns the remainder in a division problem • For example the remainder of 10/3, is 1
Some functions return multiple results • size function determines the number of rows and columns
Functions • There are functions for almost anything you want to do • Use the help feature to find out what they are and how to use them • From the command window • From the help selection on the menu bar
Elementary Math Functions • abs(x) absolute value • sign(x) plus or minus • exp(x) ex • log(x) natural log • log10(x) log base 10
Rounding Functions • round(x) • fix(x) • floor(x) • ceil(x)
Discrete Mathematics • factor(x) • gcd(x,y) greatest common denominator • lcm(x) lowest common multiple • rats(x) represent x as a fraction • factorial(x) • primes(x) • isprime(x)
Trigonometric Functions • sin(x) sine • cos(x) cosine • tan(x) tangent • asin(x) inverse sine • sinh(x) hyperbolic sine • asinh(x) inverse hyperbolic sine • sind(x) sine with degree input • asind(x) inverse sin with degree output
Data Analysis • max(x) • min(x) • mean(x) • median(x) • sum(x) • prod(x) • sort(x)
Variance and Standard Deviation • std(x) • var(x)
Random Numbers • rand(x) • Returns an x by x matrix of random numbers between 0 and 1 • rand(n,m) • Returns an n by m matrix of random numbers
Computational Limits • MATLAB’s computational range on most computers is: • 10-308 • 10308 • When you divide by 0, the computer returns Inf
Function specification function <results> = <name> ( <parameter-list> ) % <Help comments – used with help, lookfor > % <usage, theory, alg> <code> • The names of variables in functions are only visible in the function. • Scripts are used for the top level call to the functions. • Functions must be saved in an M-file of the same name as the function. (For now in the working directory.) (Actually filename is the name of the function!)
r h r h vol cyl A simple example • Suppose I have a cylinder, and I want the volume: • Going to write a function, called cyl, which will input the radius and the height of the cylinder and produce the desired volume
Function cylinder • Write function function volume = cyl(radius, height) % CYLINDER computes volume of circular cylinder % given radius and height % Use: % vol=cylinder(radius, height) % volume=pi.*radius^2.*height; % why the dots? • Save as m-file with function name (cyl.m) • Type help cyl
M-function Workspace • Each time an m-function is executed, a new workspace is created just for that instance • All variables except arguments and returned variable are defined only in the function workspace • Function variables are not defined in Base workspace, nor are Base variables defined in function workspace • Function workspace is destroyed when function completes • Function variables are “hidden” from world (and from accidental misuse or alteration)
Restrictions, style • Functions do not use INPUT or DISP or any real input output unless specified in the assignment or for debugging • Semi colons – use semi colons unless you need to see step by step results when debugging • Can’t save and run a function. Why not? • Because doesn’t know the calling environment or context.
Test Cylinder Function • Create a script: test_cyl Rad = Input(‘Radius:’) Disp(rad) % note different names Ht = Input(‘Height:’) Disp(ht) Cyl(rad,ht) • Try with vectors.
How are M-Functions Located? • Sometimes it is important to know how Matlab will look for an m-function, especially if two have the same name… • First checks current workspace • is it a built-in Matlab function? • is it a sub-function in the current function? • is it a private function? • is it in the current directory? • is it in the Matlab path, searching from the current directory down? • The first instance is used and any others are ignored • This can cause problems if you don’t understand… • It can also let you replace functions with different versions
Multiple Values Functions • M-functions Can Return Multiple Values • The returned variable from an M-function can be a scalar or an array (even a cell array) • M-functions can use the [ ] constructor to return an array formed from multiple scalar values
Multiple Value Return • Lets update our cylinder function to return the area and the volume: function [area volume] = cylAV(radius, height) % CYLINDER computes volume of circular cylinder % given radius and height % Use: % [ar, vol] =cylinder(radius, height) % volume=pi.*radius^2.*height; area = 2 .* pi .* height + 2.* pi .*r .*r;