1 ME1 Homework #5 Matlab- Functions Last Updated February 9 2012. Assignment: Read and complete the suggested commands. After completing the exercise, copy the contents of the command window into Word or Adobe. Turn in a hardcopy of the file, and any M-files that you generate. Native functions in Matlab: You may not know it, but you have already used functions in Matlab. Consider the computation of the sine of an angle. At the command prompt, type x=0.25, and y=sin(x). The output should look like >> x=0.25 x = 0.2500 >> y=sin(x) 0.27 If you check with your calculator (and make sure that your calculator uses units of radians for degrees), you should get the same answer. Since the function sin is part of Matlab, it is known as a native function. In general, a function has three parts, visualized as sin From left to right, the three parts consist of an input (in this case x), a function name (in this case named sin), and an output (in this case y). The intent of a function is to compute outputs from inputs using a procedure that is black box in nature. For example, when the function sin was used above, the user does not need to know how the computation was made. This allows for development of modular code. More on this later. The function sin is more powerful that the simple use above. For example, type x=[0.2 0. -0.5], and then y=sin(x). You should obtain the following output >> x=[0.2 0. -0.5] x = 0.2000 0.000-0.5000 >> y=sin(x) 0.1987 0.2955-0.79 x y
2 It is apparent that the input x can be a row vector, and the function sin will respond with a row vector with elements corresponding to the sine of the same elements in the input. Making your own elementary function: A valuable capability of programming languages (and Matlab is a programming language), is the ability to write your own functions. Lets consider a very simple example, which will illustrate some of the rules for using functions. We will write a function that will compute the square of a scalar. Functions are stored in separate M-files. Open the M-file editor, and type in the following lines function y=sq(x) y=x^2 Then save the file as sq.m. There are several element of syntax, even in this simple function. For a function, the first line should always take the form function output=name(input). The purpose of function is to inform Matlab that this is a special type of M-file, i.e., a function instead of a script. output is the name of a variable that will be computed in the function. In this case, the output variable has been named y. name is the name of the function. In parenthesis, input is the name of a variable that will be the input. In this example, the input variable is named x. Later, we will see that there can be multiple inputs and multiple outputs. The job of the function is to compute an output given the input. In this example, this is accomplished by the line y=x^2. In general, there can be many lines in a function, their sole purpose being the computation of output variable(s) given the input variable(s). A second important factoid is that the name of the M-file should be the same as the name of the function. Hence, the function named sq must be stored in an M-file named sq.m. Now toggle to the command window, and type the lines x=2, y=sq(x). You should obtain the following output >> y=sq(x) The first occurrence of y= in the command window is caused by the line y=x^2 in the function sq. The second occurrence of y= in the command window takes
place after the assignment to y in the line y=sq(x) typed into the command window. Input and output variable names in functions are local: The variable names x for input and y for output in the function sq.m are local to the function, i.e., it is not required to use these names at the command window. At the command window, type the lines t=5, z=sq(t). You should obtain the following output >> z=sq(t) 25 z = 25 The function does not care that the input, named x in the function, it called t in the command window. It also does not mind that the output y in the function, computed from x in the function, is assigned to z in the command window. The input(s) and output(s) to functions are called dummy variables, in that they are defined by the placement in the function statement, not by the name. This will be clearer in later examples. All variables in a function are local to the function: Not only are input and output variables local to a function, all variables used in a function are local to the function. Suppose that we make the change to the function sq that it will add 2 to the square of a number. This can be accomplished by the following modification to sq.m function y=sq(x) a=2; y=x^2+a; Now there are two computational lines in the function square. It is evident that the output y will be computed by squaring the input x, and adding a value of 2. In adding a value of 2, an intermediate variable a was created. Furthermore, the command window output for the computational lines has been suppressed. Now type clear all in the command window. This will enable a fresh start. Type the commands t=, z=sq(t). Your output should be >> clear all >> t= t = >> z=sq(t) z = 18
The function responds as expected, the input and output variables x and y are local. However, at the command prompt, type a, followed by whos. You will obtain the following ouput in the command window >> a??? Undefined function or variable 'a'. t 1x1 8 double z 1x1 8 double It is apparent that the variable a does not exist. It only existed locally within the function sq. This is a very important feature of a programming language. It allows for modularity. If variables were not local, then assignments in one module of code would affect the values of variables of the same name in another module. Multiple inputs and outputs: As you might expect, it is possible for a function to have multiple inputs and multiple outputs. Open the M-file editor, and type in the following code for a function named sq2 function [z1,z2]=sq2(x,y) a=2; z1=x^2+a; z2=y^2-a; Make sure and save the function in the file named sq2.m. Multiple inputs and outputs have been implemented in the first line function [z1,z2]=sq2(x,y) There are two inputs, x and y. They are included in the parenthesis, separated by a comma. This is often called the input list, or argument list. Since variable names in the function are local, the interpretation of the meaning of the inputs and outputs are determined by their order in each list, not by their name. The outputs are placed in square brackets, separated by a comma. In the code statements, note that the outputs z1 and z2 are computed from the inputs and the variable a defined in the code.
5 Now, go to the command window, and type the commands clear all, w=5, v=6, and [q1,q2]=sq2(w,v), followed by whos. The output at the command window should look like >> w=5 w = 5 >> v=6 v = 6 >> [q1,q2]=sq2(w,v) q1 = 27 q2 = q1 1x1 8 double q2 1x1 8 double v 1x1 8 double w 1x1 8 double At the command window, the computation performed by the function sq2 were assigned to the variables q1 and q2. The variables x,y,a,z1,z2 do not exist in the command window, they existed locally in the function sq2. Input and output variables may be arrays: As you may further expect, input and output variables may be scalars and arrays. In fact the inputs and outputs can also be other kinds of data structures, which will be discussed in class. Using the M-file editor, open a new M-file and type in the following code function [z1,z2]=arrayexample(x,y) a=[1 ; 2 ; ] ; z1=x*a; z2=*y; Make sure and save the function in an M-file named arrayexample.m. Now go to the command window, and type clear all, w=[ 2 1;-9 8 7;0 2 ], v=, [q1,q2]=arrayexample(w,v), and then whos. You should get the following output at the command window
6 >> clear all >> w=[ 2 1;-9 8 7;0 2 ] w = 2 1-9 8 7 0 2 >> v= v = >> [q1,q2]=arrayexample(w,v) q1 = 10 28 16 q2 = 9 q1 x1 2 double q2 1x1 8 double v 1x1 8 double w x 72 double The function computes the outputs q1 and q2 as defined by the code in the function arrayexample. Again, the variables x,y,a,z1,z2 are local to the function, and do not exist in the command window. Note that the intention of the function is to return an output z1 that is a x1 column vector, and an output z2 that is a scalar. This is exactly what happened, as observed by the dimension of the output variables q1 and q2 in the command window. You must be aware of required variable types for inputs and outputs: On must pay careful attention to the requirements of the input and output lists for functions. In the previous example, the dimensions of the first and second arguments x and y were x and 1x1 (scalar) respectively. If you do not follow the intended requirements, you may get incorrect results or an error in the execution of the function. For example, suppose you repeat the command window sequence before, only you mix up the order of the input list for the function arrayexample. To do this, type clear all, w=[ 2 1;-9 8 7;0 2 ], v=, [q1,q2]=arrayexample(v,w), and then whos (only difference is v,w instead of intended w,v in the function call). You should obtain the following result in the command window
7 >> clear all >> w=[ 2 1;-9 8 7;0 2 ] w = 2 1-9 8 7 0 2 >> v= v = >> [q1,q2]=arrayexample(v,w) q1 = 6 9 q2 = 9 6-27 2 21 0 6 12 q1 x1 2 double q2 x 72 double v 1x1 8 double w x 72 double Now the results of the computation in the function are not what were intended. The output q1 is a scalar x1, and the output q2 is a x matrix. In this case, only the output of the code was incorrect. Many times, the code in the function assumes particular dimensions for the input variables for a valid matrix operation. If an input does not have the assumed dimension, and error will occur and execution will halt.