Chapter 1 - Part 2 Matlab Built-in Functions.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

BUILT-IN FUNCTIONS Built-in Functions One of MATLAB’s benefits is the large number of built -in functions Built-in functions allow the user to complete a range of tasks quickly and easily There are functions that perform simple calculations such as the absolute value There are fun...

BUILT-IN FUNCTIONS Built-in Functions One of MATLAB’s benefits is the large number of built -in functions Built-in functions allow the user to complete a range of tasks quickly and easily There are functions that perform simple calculations such as the absolute value There are functions that perform complex calculations such as a numerical integration or curve fitting How to use Built-in Functions Terminology: Call a function by giving the function name followed by passing its argument(s) in parenthesis. If the function calculates a value, it returns the value If an assignment statement is used the returned value gets stored in the variable named on the left side Function Input can be either scalars or matrices Functions that require multiple inputs Remainder function returns the remainder in a division problem For example: The remainder of 10/3, is 1 Functions that return multiple results size function determines the number of rows and columns No. of rows No. of columns Assign names to the output The variable names are arbitrary – choose something that makes sense in the context of your problem Nesting Functions *Nesting = using one function as the input to another Getting Help  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 Access Help from Command Window To see a list of help topics, use the command help >> help To find the functions in a help topic, type help with function group name: >> help demos To find out about a particular function like square root: >> help sqrt function group name Access Help from Menu The windowed help function can also be accessed using the doc command Elementary Math Functions I) Common Computations abs(x) absolute value sign(x) plus or minus exp(x) ex log(x) natural log log10(x) log base 10 As in most computer languages, log(x) is the syntax for the natural log – there is no ln function defined in MATLAB Rounding Functions Discrete Mathematics Trigonometric Functions Built-in function Meaning 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 Functions max(x) – largest element in array min(x) – smallest element in array mean(x) – average or mean value in array median(x) – median value of array sum(x) – sum of elements prod(x) – product of elements sort(x) - Sort in ascending or descending order sortrows(x) - sort rows in ascending order std(x) – standard deviation var(x) - variance MAX & MIN When the max function is used with a vector (either a row or a column), it returns the maximum value in the vector When x is a matrix, the max is found for each column The max function can also be used to determine where the maximum occurs max value index number where the max value occurs Vector of maximums Vector of row numbers SORTING VALUES It’s easy to sort data in MATLAB, using the sort function The default is to sort in ascending order To sort in descending order, just add the word ‘descend’ in the second input field Sorting in a Matrix MATLAB is column dominant, so when sort is used with a 2-D matrix, each column is sorted in ascending order The sortrows function allows you to sort entire rows, based on the value in a specified column. The default sorting column is #1 In this example the matrix is sorted in ascending order, based on the second column Notice that this is a different strategy than that used by the sort function! To sort based on descending order, place a negative sign in front of the column number Determining Matrix Size  size(x) - number of rows and columns  length(x) - biggest dimension  numel(x) total number of elements Variance and Standard Deviation  N std(x)  (xk −  ) 2 var(x)  2  2 = k =1 N −1

Use Quizgecko on...
Browser
Browser