1 / 27

CMPS 1371 Introduction to Computing for Engineers

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

abrittney
Download Presentation

CMPS 1371 Introduction to Computing for Engineers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMPS 1371Introduction to Computing for Engineers FUNCTIONS

  2. Functions • MATLAB uses function names consistent with most major programming languages For example • sqrt • sin • cos • log

  3. Function Input can be either scalars or matrices

  4. Using Predefined Functions • Functions consist of • Name • Input argument(s) • Output sqrt (x) = result • In MATLAB sqrt(4) ans = 2

  5. Some functions require multiple inputs • Remainder function returns the remainder in a division problem • For example the remainder of 10/3, is 1

  6. Some functions return multiple results • size function determines the number of rows and columns

  7. You can assign names to the output

  8. Nesting Functions

  9. 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

  10. Elementary Math Functions • abs(x) absolute value • sign(x) plus or minus • exp(x) ex • log(x) natural log • log10(x) log base 10

  11. Rounding Functions • round(x) • fix(x) • floor(x) • ceil(x)

  12. 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)

  13. 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

  14. Data Analysis • max(x) • min(x) • mean(x) • median(x) • sum(x) • prod(x) • sort(x)

  15. Variance and Standard Deviation • std(x) • var(x)

  16. 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

  17. Computational Limits • MATLAB’s computational range on most computers is: • 10-308 • 10308 • When you divide by 0, the computer returns Inf

  18. 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!)

  19. 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

  20. 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

  21. Results of Cylinder

  22. 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)

  23. 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.

  24. 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.

  25. 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

  26. 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

  27. 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;

More Related