Date post: | 16-Apr-2017 |
Category: |
Engineering |
Upload: | umer-azeem |
View: | 29 times |
Download: | 0 times |
Matlab Functions
Matlab Functions13033386-018 Umar Azeem
University of Gujrat
Introduction A function is a MATLAB program that can accept inputs and produce outputsA function is a group of statements that together perform a task. Functions can accept more than one input arguments and may return more than one output arguments
Why use functions?
Functions provide reusable code
Use same code in more than one place in program without rewriting code
Reuse code by calling in different programs
Basic types of function
Built in FunctionUser define Function
Built in Function
Operators and Special Characters+ Plus; addition operator.- Minus; subtraction operator.* Scalar and matrix multiplication operator..* Array multiplication operator.^ Scalar and matrix exponentiation operator..^ Array exponentiation operator.\ Left-division operator./ Right-division operator..\ Array left-division operator../ Array right-division operator.
Operators and Special Characters : Colon; generates regularly spaced elements and represents an entire row or column.( ) Parentheses; encloses function arguments and array indices; overrides precedence.[ ] Brackets; enclosures array elements.., Comma; separates statements and elements in a row.; Semicolon; separates columns and suppresses display.% Percent sign; designates a comment and specifies formatting.= Assignment (replacement) operator.
Commands for Managing a Sessionclc -Clears Command window.Clear- Removes variables from memory.exist -Checks for existence of file or variable.Global- Declares variables to be global.help -Searches for a help topic.Lookfor- Searches help entries for a keyword.quit -Stops MATLAB.who -Lists current variables.whos -Lists current variables (long display).
System and File CommandsCd-Changes current directory.Date-Displays current date.Delete-Deletes a file.Diary-Switches on/off diary file recording.dir-Lists all files in current directory.Load-Loads workspace variables from a file.Path-Displays search path.Save-Saves workspace variables in a file.
Data Analysismax(x)min(x)mean(x)median(x)sum(x)
Trigonometric Functionssin(x)sinecos(x)cosinetan(x)tangentasin(x)inverse sinesinh(x)hyperbolic sineasinh(x)inverse hyperbolic sinesind(x)sine with degree inputasind(x)inverse sin with degree output
User define Function
Before you start
Identify the functionDecide the function nameDecide the input variablesDecide the output variablesFile name must be the function name.
Function Syntaxfunction[a, b, c]= basicmath(x,y)%BASICMATH Basic Mathematical function% basicmath(x,y) is a sample matlab function to perform% basic mathematical operations on input variables x & y% outputs of the function are sum, difference and product of input arguments.
a = x + y;b = x y;c = x * y;14Output argumentsExecutable code
Comments about the functionName of the function
Input argumentsdeclaration statement
Syntax The declaration statement function is used to define the file as a function.It must be typed in lower case letters.Input argumentsCan be zero or more input arguments.Output argumentsTypes inside the square brackets [ ]Give a meaningful variable name.
Function with one OutputThe function have only one out put you dont need to put it inside the square brackets [ ]
Example:The function in a file namedaverage.mthat accepts an input vector, calculates the average of the values, and returns a single result.function y = average(x) y = sum(x)/length(x); end
Functions with more OutputsWhen there are more than one output arguments put them in a square bracket.Example
function[a, b, c]= basicmath(x,y)a = x + y;b = x y;c = x * y;
Calling a user-defined functionA function can be called from the command window or inside a script or function.To run a function in command window, just type the name of comment with proper input and output argumentsFor example consider the function basicmath>> [a,b,c] = basicmath (2,3)a = 5b = -1c = 6
Points to RememberFunction name and file name must be same.Unlike other programming languages, Function in MATLAB can return more than one value.Syntax: function outputs=function_name(inputs) There is no starting and ending curly braceS to enclose the body of function, instead, there is an end statement which signifies end of function.
Points to Remember
To run a function we type a function name with valid arguments in command window area.If function returns two or more ouputs,to see all the outputs, we must not put semicolon after the output statement. Note: if we put semicolon, we will see only the first output.
SummaryMATLAB contains a wide array of predefined functionsElementary Math FunctionsTrigonometric FunctionsData Analysis FunctionsRandom NumbersComplex Numbers
Thank You