Download Understanding Matlab Functions: Definition, Use, and Creation and more Study notes Computer Science in PDF only on Docsity! 2-2 What is a Matlab function? How does the plot function work? What Matlab functions are available to do Data Analysis? How can you create your own function ? Readings: Matlab by Pratap Chapter 4.2, 4.3 Chapter 5.3 Chapter 6.1 2-3 Basic Mathematical Functions (see p62-64 in textbook) However, since there are hundreds of Matlab functions, a useful tool is Matlab Helpdesk. Exponential Function >> exp(0) (exponential function ex ) ans = 1 name argument parenthesis >> x = [-1 0 1]; >> exp(x) ans = 0.3679 1.0000 2.7183 ( [exp(-1), exp(0), exp(1)] ) The exp function argument can be a scalar, vector or matrix. 4 Three forms of the use of a function in Matlab are: >> VAR = function_name(arg1,arg2, …); >> [VAR1,VAR2,...] = function_name(arg1,arg2, …); >> function_name(arg1,arg2, …); A Matlab function like a mathematical function is a rule where given a certain input or inputs, the rule tells you how to compute the output value or how to produce an effect (e.g. the plot function produces a figure). The inputs are called the “arguments” to the function. As an illustration, a function can be likened to the sequence of instructions on an income tax form(see next two slides). • The instructions must be followed in sequential order. • Fields (or boxes in the form represent variables). All variables in a function are local to that function. They cannot been seen by other users filling out other forms. • Fields in the form that must be filled in with information provided by the person whose tax is being computed are called parameters . The values entered in these fields, (like name , social security number),... are called input arguments. • If the taxpayer recieves a return this amount is always found in one specific field on the form, field 67a. This field is called an output variable. name field refund amount amount due 2-8 Example: Plot the sine function using the built-in function “sin” . (see section 6.1) >> x = -10 : .1 : 10; % (remember ; suppresses output) >> y = sin(x); % x in radians >> plot(x,y); By default Matlab will connect the points (xi ,yi) with a straight line. A more general version of the plot command is: plot(x,y,’color_linestyle_marker’); where color is: cyan magenta yellow red green blue white black c m y r g b w k 2-17 1. Problem Definition 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) 3. Develop Algorithm (processing steps to solve problem) 4. Write the "Program" (Code) (instruction sequence to be carried out by the computer) 5. Test and Debug the Code 6. Run Code Note: These steps correspond to those found in the course C book p. 37. 2-18 1. Problem Definition Write a function that converts a temperature in Fahrenheit to Celsius. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Use the fact that celsius = (fahr - 32) * 5/9 2-19 Natural-Language Algorithm The function “definition” should list one input variable named fahr and one output variable celsius. Compute the value of the celsius temperature and assign the value to a variable “celsius” The “function” mechanism of Matlab will automatically return the value in the variable celsius when the function is used (called) at the Matlab prompt. 3. Develop Algorithm (processing steps to solve problem) 2-20 4. Write the “Function" (Code) (instruction sequence to be carried out by the computer) Any sequence of Matlab commands can be put in a file. The file suffix should end with a “.m”. The sequence of commands will be executed (from the top of the file down, command by command). >> edit to open the Matlab editor and create a new file. Then type (and save) the following: function celsius = F_to_C(fahr) % This function converts Fahrenheit to Celsius. celsius = (fahr -32)*5/9; Click “File” and then “Save As” to name the file “F_to_C.m”. Execute(call) the function in by typing “ F_to_C( temperature )”. 2-22 5. Test and Debug the Code If the program works correctly then it has no “bugs” so bring the Matlab editor back up and close out the Matlab program. Does the program work with only scalar input or does it work with vector values? (see next slide) 6. Run Code Since two points determine a linear function we know the function F_to_C works correctly. Input is a vector [32 212] 2-24 • prepared during development of a program, as comments within the program and possible additional documentation; e.g., manuals, etc. • Use the “%” symbol to write a comment in a program. • You must put comments in your program describing the function interface - i.e. input and output format. • Also a general description of what the function does. Finally the programmer should identify himself / herself. 2-25 Improved documentation for the F_to_C function. function celsius = F_to_C(fahr) % function celsius = F_to_C(fahr) % This function converts Fahrenheit to Celsius. % input fahr can be a scalar or vector of temps in degree F % output celsius is a scalar or vector of temps in degree C % Programmer: Tom Gambill % Date: 1/30/01 celsius = (fahr -32)*5/9; 2-26 1. Problem Definition Write a function that computes the time for a falling object to hit the ground. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Use the fact that height_t = height_0 + velocity_0* time + 1/2 * g * time* time, where height_t is the height of the object at any given time (in seconds), g is the acceleration due to gravity, -9.8 m/s2. velocity_0 is the velocity at time = 0. Therefore to compute the time to impact, set height_t = 0 and solve for time. This equation (after doing some algebra)can be written as: 0 = height_0 + velocity_0 * time + 1/2 * g * time * time This is a quadratic formula in terms of the variable time. 2-27 Natural-Language Algorithm Initial values of height_0 and velocity_0 are provided via the arguments when the function is called. Solve quadratic formula for time Quadratic formula has two solutions, select the positive value as the result to be returned. 3. Develop Algorithm (processing steps to solve problem) 2. (continued) The input arguments to the function consists of scalar values for height_0 and velocity_0 and the output is the exact time (in seconds) of contact with the ground. Assumptions: No air resistance and time = 0 when object begins decent. (Also height_0 > 0 implicit) 2-28 4. Write the “Function" (Code) (instruction sequence to be carried out by the computer) Use the Matlab editor to create a file “time_to_impact.m” . function time = time_to_impact(height_0,velocity_0) % function time = time_to_impact(height_0,velocity_0) % Input: Two scalars height_0 (meters) and velocity_0 (meters/sec) % Output: time to impact (sec) given initial height is % height_0 and initial velocity is velocity_0. % Programmer: Tom Gambill % Date: 9/10//00 % Assumptions: No air resistance % time = zero when object begins decent % g = 9.8 is constant p = [-.5*9.8 , velocity_0, height_0]; time=roots(p); time = max(time);