Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
×

# Matlab Functions

994 views

Published on

Matlab Functions and types

Published in: Engineering
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

### Matlab Functions

1. 1. 13033386-018 Umar Azeem University of Gujrat
2. 2. A function is a MATLAB program that can accept inputs and produce outputs A 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
3. 3. Functions provide reusable code Use same code in more than one place in program without rewriting code Reuse code by calling in different programs
4. 4. Built in Function User define Function
5. 5. Built in Function
6. 6. + 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.
7. 7.  : 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.
8. 8. clc -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).
9. 9. Cd-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.
10. 10. max(x) min(x) mean(x) median(x) sum(x)
11. 11. 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
12. 12. User define Function
13. 13. Identify the function Decide the function name Decide the input variables Decide the output variables File name must be the function name.
14. 14. function[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; 14 Output arguments Executable code Comments about the function Name of the function Input arguments declaration statement
15. 15. The declaration statement function is used to define the file as a function. It must be typed in lower case letters. Input arguments • Can be zero or more input arguments. Output arguments • Types inside the square brackets [ ] Give a meaningful variable name.
16. 16. • The function have only one out put you don’t need to put it inside the square brackets [ ]  Example:  The function in a file named average.m that 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
17. 17. When 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;
18. 18.  A 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 arguments • For example consider the function basicmath >> [a,b,c] = basicmath (2,3) a = 5 b = -1 c = 6
19. 19. Function 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.
20. 20. 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.
21. 21. MATLAB contains a wide array of predefined functions •Elementary Math Functions •Trigonometric Functions •Data Analysis Functions •Random Numbers •Complex Numbers
22. 22. Thank You