functions uc berkeley fall 2004, e77 pack/e77 copyright 2005, andy packard. this work is licensed...

Post on 23-Dec-2015

214 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

FunctionsUC Berkeley

Fall 2004, E77http://jagger.me.berkeley.edu/~pack/e77

Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to

Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Script Files

Script files• text files (created with a text editor)

• contain many lines of MATLAB expressions and assignments.

You can execute all of the lines of the file (sequentially) by typing the filename at the MATLAB prompt.

If filename is computetraj.m, then

>> computetraj

causes the instructions (ie., the MATLAB commands) in the file to be run.

Functions (35 minutes)

Basics–Function declaration line (3)– Input and output arguments (3)–Call by value, but with delayed copy (2)–Function workspaces, recursion, towers example (7)–Special features (4)

• nargin, nargout, varargin, varargout, assignin, evalin

Subfunctions (4)

Nested functions (4)

Anonymous functions (4)

Function_handles (4)

Script file example

Suppose a file called runthis.m contains the text

X = linspace(0,4*pi,1000);

Y = sin(3*X) + 2*cos(5*X);

plot(X,Y)

maxy = max(abs(Y));

disp([’Peak of Y is ’ num2str(maxy)]);

Then, typing

>> runthiswill cause the 5 lines to execute, resulting in a plot and a message about the peak value of Y.

Functions (Chapter 11, pg 175-194)

In mathematics, a function is a rule that assigns to each value of the input, a corresponding output value.

Consider the function f defined by the rule

f(x) = x2 for all numbers x.

Here, x is the input value, and f(x) is the output value.

Equivalently, we could have written the rule as

f(y) = y2 for all numbers y.

Functions can have many inputs and produce (through multiple rules) many outputs, f1(a,b,c) = 2a+3b, f2(a,b,c) = bc.

FUNCTIONS in programming

If you have a “complex” task (eg., more than two lines of Matlab program code) that you will reuse in a few (or several places), you will want to write that task as a reusable function file.

Example: trigonometric SIN function

Then, every time you need to execute that task, you will only need to “call” the function.

This modularity helps break down a huge program task into a collection of smaller tasks, which individually are easier to design, write, debug and maintain.

functions are sometimes also called subroutines, methods, etc.

A MATLAB function file

The first line is the function declaration line.

function [dp,cp] = vecop(v,w)

The function name, this function should be saved in a file called vecop.m

The input variables. This function has two. Within the function, the names of the input variables are v and w.

The output variables. This function has two. The function’s purpose is to compute these variables, based on the values of the input variables.

The input and output variables are also called the input and output arguments.

Picture of VECOP function

InputArgument #1

InputArgument #2

OutputArgument #1

OutputArgument #2

VECOP

6-line function in vecop.m

function [dp,cp] = vecop(v,w)

dp = sum(v.*w);

cp = zeros(3,1);

cp(1) = v(2)*w(3) – w(2)*v(3);

cp(2) = v(3)*w(1) – w(3)*v(1);

cp(3) = v(1)*w(2) – w(1)*v(2);

Logically correct expressions and assignments that compute the output variables using the values of the input variables.

Function declaration line

Comments and blank lines add readability

function [dp,cp] = vecop2(v,w)

% VECOP computes dot product and cross

% product of two 3-by-1 vectors.

dp = sum(v.*w);

cp = zeros(3,1); % create 3-by-1

% Fill cp

cp(1) = v(2)*w(3) – w(2)*v(3);

cp(2) = v(3)*w(1) – w(3)*v(1);

cp(3) = v(1)*w(2) – w(1)*v(2);

comments

Calling a function

>>

>> v1 = [1;-2;3];

>> v2 = [0;1;1];

>> [A,B] = vecop(v1,v2);

>>

function [dp,cp] = vecop(v,w)

dp = sum(v.*w);

cp = zeros(3,1);

cp(1) = v(2)*w(3)-w(2)*v(3);

cp(2) = v(3)*w(1)-w(3)*v(1);

cp(3) = v(1)*w(2)-w(1)*v(2);

BASE WORKSPACE

v1 3-by-1

v2 3-by-1

A 1-by-1

B 3-by-1

FUNCTION INSTANCE WS

v 3-by-1

w 3-by-1

dp 1-by-1

cp 3-by-1

Can we see evidence of this (evalin)?

function [dp,cp] = vecop3(v,w)

disp(’Just inside VECOP3 function’)

disp(’ FunctionWS’); whos

disp(’ CallerWS’); evalin(’caller’,’whos’);

dp = sum(v.*w);

disp(’After DP create’)

disp(’ FunctionWS’); whos

disp(’ CallerWS’); evalin(’caller’,’whos’);

cp = zeros(3,1);

disp(’After CP create’)

disp(’ FunctionWS’); whos

disp(’ CallerWS’); evalin(’caller’,’whos’);

cp(1) = v(2)*w(3)-w(2)*v(3);

cp(2) = v(3)*w(1)-w(3)*v(1);

cp(3) = v(1)*w(2)-w(1)*v(2);

Built-in Functions

MATLAB has many functions called built-in functions.–sin, cos, tan, exp–reshape, size, plot

If we could see the programs for these, we would see that they are written in C or assembly language (specific to each processor that MATLAB is available for). These low-level, important functions give MATLAB its basic functionality.

Individuals (like yourself) expand the functionality of MATLAB by writing their own m-file functions, which use the built-in functions, as well as already written m-file functions.

We will write m-file functions, using MATLAB expressions and assignments.

TYPE shows m-files

The command

>> type filename

will either–inform you that the function is a built-in, or–print out (to the command window) all of the lines in the m-

file with that name.

Try

>> type sin

>> type strvcat

>> type vecop3

top related