Tutorial IV: Function Files Last updated 5/05/05 by G.G. Botte Department of Chemical Engineering ChE-1800: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial IV Function Files (last updated 5/05/05 by GGB) Objectives: These tutorials are designed to show the introductory elements for any of the topics discussed. In almost all cases there are other ways to accomplish the same objective, or higher level features that can be added to the commands below. Read Chapter 6 of the textbook to learn more about function files Any text below appearing after the double prompt (>>) can be entered in the Command Window directly or in an m-file. ______________________________________________________________________________ The following topics are covered in this tutorial; Introduction Creating Function m_files Executing Function files Solved Problems (guided tour) Proposed Problems ______________________________________________________________________________ Introduction: Function m_files are used to calculate a value of a dependent variable or variables, at specified values of independent variables. You may have noticed that when you use the built_in Matlab functions, such as 'sum' or 'sin', you don't see the specific Matlab commands that are executed and any variables used internally by the function are not recognized by the Command Window. We can take advantage of this feature to perform calculations or sequences of commands that appear often in working a larger problem. We do this by creating our own functions. Functions are created in an m_file using the Matlab Editor, just like a script m_file. The major difference between a script m_file and a function m_file lies in the interaction between the m_file and the Command Window. Remember a script m_files was able to see all variables stored in memory, and all variables created in the m_file were stored in memory. A function m_file interacts with the Command Window only through its input and output. Intermediate variables that are not passed to or from the function cannot be accessed by the Command Window, and the Command Window cannot use variables created in the function file unless they are specifically passed to the Command Window as output. 1 Creating Function m_files A function is going to accept input that you control, and return output values that you request. The structure of a function file must be as follows: (1) The first executable line of the file must begin with the word function. This is followed by the output arguments. If more than one variable is to be returned they must be in an array (square brackets). This is followed by an equal sign, and the name of the function with the input arguments in parentheses, separated by commas. See example: function [output1,output2,output3, etc]=function_name(input1,input2,input3, etc) The word function must be the first word, and must be typed in lower-case letters List of output variables returned by the function. Variables are returned as a line vector, therefore, they can be accessed using the order of the vector The name of the function List of input arguments typed inside parentheses The input and output argument names can be the same or different from those used in the calling statement for the function. In either case they are merely matched up by location in the input argument list and the output array. (2) After the first executable line any comments, variable definitions etc. should be listed using the % sign. These comments will be displayed when you type >> help function_name at the MATLAB prompt. Same guidelines for developing quality programs must be followed in function files. It is a good practice to define all the variables that the function uses. That way, when the user wants to learn more about the function the description of the variables used is shown in the command screen by using the command help function_name. The minimum comments that should be written in a function file are: 1. Description of the function 2. Author of the function, date, and last modified dates 3. Call syntax. This is the syntax needed to run the function 4. Input variables including the units 5. Output variables including the units (3) The commands which perform the calculations. Example 1: To practice, let us work on the following example. Develop a function file that will be able to convert temperature from Celsius to Fahrenheit. We are going to call the function Fahrenheit. That is, our function should be able to transform oC to oF, therefore, the input of the function should be temperature (TC) in oC, and the output of the function should be temperature (TF) in o F. 2 Tutorial IV: Function Files Last updated 5/05/05 by G.G. Botte Step 1: Write the script file of the function Function name Comments that should be written in a function file Step 2: Save the function. The file name should be the function name with the extension “m”, that is, function_name.m. See example File name is the function name Executing a function A function can be executed from the Command Window, from a script file, or from another function. To use a function file, you must be in the directory where the function file was saved. 3 Example 2: Execute the function Fahrenheit from the command window. If you use the command help you can see the description of the function that we just created Execute the function to convert 25 oC to oF: Input variable Example 3: Develop a program that will convert temperature from Celcius to Fahrenheit using the function that was developed. This example allows illustrating how to execute the function from a script file. This is the script file 4 Tutorial IV: Function Files Last updated 5/05/05 by G.G. Botte This is what you will see when you execute the file SOLVED PROBLEMS 1. Write a program in Matlab to convert temperatures from Celsius to Fahrenheit or from Fahrenheit to Celsius. The user must have the choice of leaving the program at anytime. Use the menu option to make selections. Your code must use functions to convert from Celsius to Fahrenheit and from Fahrenheit to Celsius. Solution: 1. Follow the “tips for solving problems” 2. Write a flowchart diagram (see H-2). In the space given below draw your flowchart diagram 5 3. Write the code in Matlab. See the solution given below. 4. Execute the program for different temperatures, example 6 Tutorial IV: Function Files Last updated 5/05/05 by G.G. Botte 2. Write a function in Matlab that calculates the local maximum or minimum of a quadratic function of the form: f (x) = ax 2 + bx + c . For the function name and arguments use [x,y]=maxmin(a,b,c). The input arguments are the constants a, b, and c, and the output arguments are the coordinates x and y of the maximum or the minimum. Use the function to determine the maximum or minimum of the following functions: a) f (x) = 3x 2 − 18x + 48 b) f (x) = −5x 2 + 10x − 3 Solution: We know that the minimum or maximum of a function is found by determining the value of x that makes the first derivative of the function equal to zero. Therefore, f (x) ' = 2ax + b = 0 x min = 1. Follow the “tips for solving problems” 2. Develop the function file −b 2a 3. This is how the function is called from the command window. Remember that the solution is a vector 7 PROPOSED PROBLEMS The following chemical reaction describes the neutralization of sodium hydroxide by sulfuric acid. H2SO4 + 2NaOH → Na 2SO4 + 2H2O write a function file which calculates the volume and mass of 0.75M H2SO4 needed to neutralize an NaOH solution. The density of the sulfuric acid solution is 1045 g/l. Let the user specify the volume and concentration of the NaOH in a main m-file. Pass this information to the function file and return the volume and mass of sulfuric acid from the function file. The output should be in the main file. 8