Presentation is loading. Please wait.

Presentation is loading. Please wait.

ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions.

Similar presentations


Presentation on theme: "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions."— Presentation transcript:

1 ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions and Function Files / Chapter 6

2 ENG 1181 2  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

3 ENG 1181 3 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.

4 ENG 1181 4 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

5 ENG 1181 5 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

6 ENG 1181 6  Open a new function file (not script file) Creating a function file

7 ENG 1181 7 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.

8 ENG 1181 8  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)

9 ENG 1181 9 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

10 ENG 1181 10 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

11 ENG 1181 11 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

12 ENG 1181 12 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 2 4 8 8 4 Call a function to calculate the area of a rectangle with sides len and wid. Script file

13 ENG 1181 13 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)

14 ENG 1181 14 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

15 ENG 1181 15 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 = 50.6175 Now try changing the argument values


Download ppt "ENG 1181 1 College of Engineering Engineering Education Innovation Center 1 MATLAB – Functions 1 Topics Covered: 1.Functions 2.Function files Functions."

Similar presentations


Ads by Google