Download presentation

Presentation is loading. Please wait.

Published byMaryam Hargis Modified over 2 years ago

1
CMPS 1371 Introduction 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)e x 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 10 308 When you divide by 0, the computer returns Inf

18
Function specification function = ( ) % 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
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 h r rh vol cyl

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… 1. First checks current workspace 2. is it a built-in Matlab function? 3. is it a sub-function in the current function? 4. is it a private function? 5. is it in the current directory? 6. 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;

Similar presentations

OK

Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.

Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on dressing etiquettes Ppt on union parliament of india Ppt on human chromosomes vs primates Class 7 science ppt on light Ppt on world environment day logo Company brochure ppt on architectural firm Download ppt on biomass energy Ppt on world war ii Ppt on hotel industry in india Ppt on forest management in india