User Defined Functions

Slides:



Advertisements
Similar presentations
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Advertisements

Lecture 5.
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
The Web Warrior Guide to Web Design Technologies
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
MATLAB and Simulinklecture 31 To days Outline  Functions  Strings  Sparse Arrays  Cell Arrays  Structures  Exercises on this days topics.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Functions CS 103 March 3, Review A function is a set of code we can execute on command to perform a specific task A function is a set of code we.
Functions CS 103. Review A function is a set of code we can execute on command to perform a specific task When we call a function, we can pass arguments.
Recap Function M-files Syntax of Function M-Files Comments Multiple Input and Output Functions.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
Flow Control and Functions ● Script files ● If's and For's ● Basics of writing functions ● Checking input arguments ● Variable input arguments ● Output.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room , Chris Hill, Room ,
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Python Let’s get started!.
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
Recap Functions with No input OR No output Determining The Number of Input and Output Arguments Local Variables Global Variables Creating ToolBox of Functions.
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
ENG College of Engineering Engineering Education Innovation Center 1 Functions 1 in MATLAB Topics Covered: 1.Uses of Functions Organizational Tool.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
OOP: Creating a Class Topics More OOP concepts
Whatcha doin'? Aims: To start using Python. To understand loops.
EEE 161 Applied Electromagnetics
Release Numbers MATLAB is updated regularly
Python Let’s get started!.
Introduction to Python
CS005 Introduction to Programming
Formatting Output.
Repetition Structures Chapter 9
Matlab Training Session 4: Control, Flow and Functions
Lecture 25.
Introduction to MATLAB for Engineers, Third Edition
Variables, Expressions, and IO
Other Kinds of Arrays Chapter 11
Other Kinds of Arrays Chapter 11
Arrays and files BIS1523 – Lecture 15.
Basic operations in Matlab
Scripts & Functions Scripts and functions are contained in .m-files
Intro to PHP & Variables
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
MATLAB: Structures and File I/O
Python I/O.
Topics Introduction to File Input and Output
Use of Mathematics using Technology (Maltlab)
Functions In Matlab.
funCTIONs and Data Import/Export
Note on Indexing of Array Elements
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Python Syntax Errors and Exceptions
Using Script Files and Managing Data
Topics Introduction to File Input and Output
Executive Reports, Instructions and Documentation
Presentation transcript:

User Defined Functions Chapter 6

In this chapter we’ll… Create and use MATLAB functions with both single and multiple inputs and outputs Learn how to store and access functions in a user defined toolbox Create anonymous functions

Section 6.1 Creating Function M-files User defined functions are stored as M-files To use them, they must be in the current directory

Syntax All functions have a similar syntax, whether they are built-in functions or user-defined functions Name Input Result A=cos(x)

User-defined functions must start with a function definition line The line contains… The word function A variable that defines the function output A function name A variable used for the input argument function output = poly(x)

A simple function The function name must be the same as the file name

The function is available from the command window or from other M-file programs

Hint While you are creating a function it may be useful to allow intermediate calculations to print to the command window. However, once you complete your “debugging” make sure that all your output is suppressed. If you don’t, you’ll see extraneous information in the command window.

Comments You should comment functions liberally, just as you would any computer code The comment lines immediately after the first line are returned when you query the help function

Functions can accept… numeric values variables scalars arrays

Functions with Multiple Inputs and Outputs Recall the remainder function This function has two inputs

A user defined function with multiple inputs

Functions with Multiple Outputs Recall the max function It returns two results

This function return 3 output values If you don’t ask for all three results, the program just returns the first value

Recall the size function At first this function looks like it returns two values – but it really only returns a single array with two elements

Functions with no input or no output It isn’t always necessary to define an output

No output is defined Just because a function does not return an output value doesn’t mean it doesn’t do anything. This function draws a star When you try to set the star function equal to a variable, an error statement is returned

Determining the number of input and output arguments nargin determines the number of input arguments nargout determines the number of output arguments

The input to these functions is represented using a string

You can use these functions in your programming to make your functions more versatile For example the surf function accepts a variable number of arguments surf(z) plots the 2-D matrix z against the index numbers surf(x,y,z) plots the 2-D matrix z against the x and y coordinates

When a variable number of arguments is allowed… nargin returns -1

Checks to see how many output values were requested If star1 is not set equal to a variable the star is drawn but no output is returned If star1 is set equal to a variable, the rhyme is returned

Local Variables Variables defined in an M-file function, only have meaning inside that program if I set x=1 in the command window, it is not equal to 1 in the function If I set y=2 in a function, it is not equal to 2 in the workspace window The only way to communicate between functions and the workspace, is through the function input and output arguments

ans is the only variable created x, y, a, and output are local variables to the g function When the g function is executed, the only variable created is determined in the command window (or script M-file used to execute a program)

g must be defined in the function file

Even though g is defined in the workspace, the function can’t access it If you don’t define g in this function, it won’t work!!

Global Variables Although it is possible to define global variables It is a bad idea!!

Accessing M-file Code functions provided with MATLAB consist of two types The first is built in, and the code is not accessible to us The second type consists of groups of function M-files – just like the ones we’ve been writing Use the type function to see the code in function M-files

The sphere function is stored as a function M-file, but is provided by MATLAB. Studying these functions may help you understand how to program better functions yourself

We just wrote this function, and saved it into the current directory.

Section 6.2 Creating Your Own Toolbox of Functions When you call a function MATLAB searches for it along a predetermined path First it looks in the current directory Then it follows a search path determined by your installation of the program

To find the search path, select File->setpath or type pathtool in the command window

Create your own toolbox Once you’ve created a set of functions you’d like to be able to access regularly, group them into a directory (folder) and add them to the search path using the pathtool

Browse for your folder and add it to the search path

Section 6.3 Anonymous Functions Normally if you go to the trouble of creating a function, you want to store it for future use. Anonymous functions are defined inside a script M-file, and are only available to that program

Define anonymous functions in a script M-file Suppose you’d like to define a function for natural log called ln ln=@(x)log(x) The @ symbol alerts MATLAB that ln is a function The function input is next, inside parentheses Finally the function is defined

function definition The name of the function is called a function handle – in this case it is ln Notice that function handles are represented with the box symbol in the workspace window

Section 4.4 Function Functions Some functions accept other functions as input An example is the fplot function described in chapter 5 or the nargin and nargout functions described in this chapter

Either syntax gives the same result fplot requires a function in the first input field

Function functions can also accept a function handle in place of the function itself – in this case ln

Summary MATLAB contains a wide variety of built in functions It also allows you to define your own functions

Summary – Function M-Files Function M-files must start with a definition line containing the word function a variable that defines the function output a function name a variable used for the input argument

Summary – Function M-files Function M-files must be stored in the current directory or in a user defined toolbox The function name must also be the file name

Summary - IO Multiple Inputs are allowed Multiple Outputs are allowed Some functions require no input Some functions produce no outputs

Summary - Comments Functions should contain ample comments to document the code The comments directly after the function definition are used by the help feature to describe the function

Summary - Toolboxes Numerous toolboxes are provided by MATLAB Others are available from the user community Individual users can define their own toolboxes The pathtool is used to define the search path so that MATLAB can find the toolboxes

Summary – Anonymous Functions Anonymous functions are defined in a MATLAB session or M-file They only exist during the current session They are useful as input to function functions