ENG College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions and Function Files / Chapter 6
ENG MATLAB has many useful built-in functions which we have been using like sum, sin, max, etc. User-defined functions: MATLAB allows you to create your own functions. This is useful when a special function may be needed many times or used in many programs. Functions : Introduction
ENG Functions : Introduction Like a built-in function, the user-defined function can be used within script files or in the command window. To create your own user-defined function you start with a new file but the format is strictly specified. Functions are designed to handle one or multiple inputs and outputs.
ENG Function files within script files More complex programs are broken into logical pieces to be written by one or a team of programmers. Functions are used to perform these subtasks. (Next slide) Every programming language has a way to do this. In MATLAB it’s called a user-defined function In FORTRAN and BASIC it’s called a subroutine In PASCAL it’s called a procedure In C it’s also called a function
ENG Function files in complex programs Statement 1 ……… m=fnc1(n,p) ………… Statement n ……….. [y z]=fnc2(x) …………. Statement m …………… Script file: function [c] = fnc1(a,b) ………. c= ….. fnc1.m fnc2.m function [b,c]=fnc2(a) ……….. b=…. c=…. n, p x c b, c User Input / Output
ENG Open a new function file (not script file) Creating a function file
ENG Creating a function file The first line in the function file should be the function definition line: function [output vars] = function_name(input vars) If this is omitted, MATLAB interprets the file as a script file, not a function file.
ENG After adding commands, save the function file using the name function_name.m Thus, if the name of your function is my_calc, save the function file as my_calc.m Saving Function Files function [output vars] = function_name(input vars)
ENG Function file: Single Input – Single Output Create a function area_sq where the input is the side of the square and the output is the area of the square. function x= area_sq(side) x=side*side; Now in the command window type: >> area_sq(6) ans = 36 Since no assignment of the result from the function was made, the result was put in ans. Function file : area_sq.m
ENG Suppose you want to find the area of a rectangle given its 2 sides using a function area_rect. function [area_calc] = area_rect(length, width) area_calc = length * width; Now in the command window type the following: >> AREA = area_rect(2, 4) AREA = 8 Note: the function’s output (area_calc) was assigned to variable AREA. More discussion later. Function file: Multiple Inputs – Single Output Function file : area_rect.m
ENG Using a function file in a script file You can use the area_rect function created earlier in a program. Create a script file called my_area.m as follows: clear clc len=input('Enter the length of the rectangle :'); wid=input('Enter the width of the rectangle :'); AREA = area_rect(len,wid) Enter the length of the rectangle :2 Enter the width of the rectangle :4 AREA = 8 Output: Note the assignment of the output to variable AREA
ENG FUNCTION OPERATION len=input(‘Length :’); wid=input(‘Width :’); AREA = area_rect(len,wid)... Script Variables len wid AREA 2 function [area_calc] = area_rect(length, width) area_calc = length * width; Function Variables length width area_calc Call a function to calculate the area of a rectangle with sides len and wid. Script file
ENG EXAMPLES OF FUNCTION DEFINITION LINES Three input variables, two output variables, save as loan.m Two input variables, one output variable, save as RectArea.m One input variable, two output variables, save as SphereVolArea.m Three input variables, no output variables, save as trajectory.m function [mpay, tpay] = loan(amount, rate, years) function trajectory(velocity, height, gravity) function [volume, surfArea] = SphereVolArea(radius) function [area] = RectArea(width, height)
ENG Function file: Multiple Inputs – Multiple Outputs Suppose you want to find the surface area (curved surface only) and volume of a cylinder given the radius of the base and the height, you can write a function cylinder by creating a function file cylinder.m as follows: function [area, volume] = cylinder(radius, height) area = 2 * pi * radius * height; volume = pi * (radius^2) * height; H = Height R= Radius Surface Area = 2 π RH Volume = π R 2 H
ENG Functions Example function v = freefall(t,m,cd) %freefall: bungee velocity with second-order drag %v=freefall(t,m,cd) computes the free-fall velocity of an object with %second-order drag %input: %t=time (s) %m=mass(kg) %cd = second-order drag coefficient(kg/m) %output: %v=downward velocity (m/s) g = 9.81; %acceleration due to gravity v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t); Command Window Output: >>freefall(12,68.1,.25) ans = Now try changing the argument values