EGR 106 Lecture 9 Function Files - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

EGR 106 Lecture 9 Function Files

Description:

EGR 106 Lecture 9 Function Files – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 27
Provided by: gatew318
Category:
Tags: egr | assm | files | function | lecture

less

Transcript and Presenter's Notes

Title: EGR 106 Lecture 9 Function Files


1
EGR 106 Lecture 9Function Files
  • Functions
  • Concept
  • Basic Structure
  • Examples and Applications
  • Textbook 6.1 - 6.7

2
Function Concept
  • So far
  • Have used Matlabs built-in functions for
    example sin(x), exp(x), abs(x), . . .
  • Have started writing scripts
  • Function
  • Reusable script
  • Sometimes called a subprogram
  • Useful as a building block for larger programs
  • Computes an output from an input

3
Building Block Concept
function 1 ------ ------ ------
Main Program ------ ------ ------ ------ functio
n 1 ------ ------ ------ function
2 ------ ------ ------ ------ ------ function
3 ------ ------ ------ ------ ------ ------ -----
-
function 2 ------ ------ ------
function 3 ------ ------ ------
4
Function File Format
First line of the file must be of the form
5
Example Convert Degrees to RadiansFunction
deg2rad
6
Usage
  • Syntax is just like a built-in functions
  • Application is independent of the variable names
    within the function (x,y)
  • Executed by typing name (input)

7
Rules for Functions
  • Again first line of the file must be of the form
  • function outputs name(inputs)

List of any variables that the function needs
Identifies a function file
List of function result variables
Name of the function and the file (name.m)
8
  • inputs
  • Used to transfer data into the function from the
    workspace
  • Workspace variables are unavailable within the
    function
  • Any necessary variables must be brought in
  • For multiple inputs
  • Separate them by commas
  • Order is important
  • Examples of built-in functions with inputs
  • sum(x) plot(x,y)

9
  • outputs
  • Used to transfer results back into the workspace
    from the function
  • For multiple outputs
  • Separate them by commas in brackets
  • Order is important
  • Output variables must be assigned
  • Examples of built-in functions with outputs
  • y sum(x)
  • value,location max(x)

10
  • Note brackets on the left only make sense for
    functions
  • value,location max(x) is okay
  • value,location 1, 2 is not
  • Default output is the first one
  • value,location max(x)
  • value max(x)

11
  • Comments are suggested in lines 2, 3
  • Words in line 2 are searched when lookfor is
    employed
  • Comments in lines 2, 3, are returned when help
    name is executed

12
Variables Local vs Global
  • Usually, once created, variables are available in
    the workspace until cleared
  • Functions create their own workspace with their
    own local variables distinct from those in the
    original workspace
  • Functions cannot modify variables within the
    original workspace except through outputs
  • Exception variables can be declared global and
    will then span both workspaces and be
    manipulated in both.
  • Format global variable_name

13
Variables Local vs Global
function zfct2(x) Calcuate fct
2 z5x.2x - - -
function yfct1(x) Calcualte fct
1 y6x.310 - - -
Local variables x are not in general the same
Each workspace is different with different local
variables
14
Example Functions
  • Consider a script to compute the area of a
    triangle
  • First as a script

15
(No Transcript)
16
Now as a function
17
  • Usage doesnt depend on knowing parameter names
    internal to the program

18
  • Could add the perimeter calculation as a second,
    optional output

19
  • My plotting function

20
(No Transcript)
21
  • Can stack functions in one file (all must be
    functions)
  • Useful for mailing and testing
  • Must start
  • m-file with
  • function . . .that could be a main program
    used to call other functions

22
Typical Errors
Too few inputs
23

Too many inputs
Too many outputs
Wrong input type funny result
24
Application f(x) 2e-xsin(x)e-x/2sin(2x)
  • Find its minimum and the first root beyond zero

x ?
f(x) ?
25
Solution using Matlab
  • Construct a function m-file for f(x)

26
  • To find the root fzero('fun',a) (a nearby
    value)
  • To find the minimum fminbnd('fun',a,b)
  • (a,b range)
Write a Comment
User Comments (0)
About PowerShow.com