# Functions in MatLab Create a new folder on your Z:drive called MatLab_Class24 Start MatLab and change your current directory to MatLab_Class24 Topics:

## Presentation on theme: "Functions in MatLab Create a new folder on your Z:drive called MatLab_Class24 Start MatLab and change your current directory to MatLab_Class24 Topics:"— Presentation transcript:

Functions in MatLab Create a new folder on your Z:drive called MatLab_Class24 Start MatLab and change your current directory to MatLab_Class24 Topics: 1.User-Defined Functions 2.Function files

Functions are special purpose programs MATLAB has many Built-in Functions... sin(x), log(x), length(v), sum(v)... You can also create your own functions These are called User-Defined Functions Functions in MatLab y = f(x)

Consider the function f 1 (x), where: y = f 1 (x) = 2 x 2 + 5 x – 3 The name of the function is f 1 It takes the number x (input variable) And calculates a number y (output variable) Now, let's do the same thing in MatLab.... User-Defined Functions y = f(x)

Open a new M-File Type the first line: function [y] = f1(x) Type the second line: y = 2*x^2 + 5*x – 3; SAVE ! the M-File with the filename f1.m The FunctionName and the FileName MUST be the same ! ! ! y = f 1 (x) = 2 x 2 + 5 x – 3 User-Defined Functions

function [y] = f1(x) y = 2*x^2 + 5*x – 3; Let's use the function to calculate f1(2) >> x = 2; >> y = f1(x) y = 15 y = f 1 (2) = 2 * 2 2 + 5 * 2 – 3 = 15 M-File: f1.m User-Defined Functions

function [y] = f1(x) y = 2*x^2 + 5*x – 3; Try these commands (after clear, clc): >> f1(2) ans = 15 >> a = 3 >> b = f1(a) b = 30 M-File: f1.m >> c = f1(2)+f1(3) c = 45 >> d = f1(c) d = 4272 User-Defined Functions

function [y] = f1(x) y = 2*x^2 + 5*x – 3 Let's see if f1.m works if x is a vector! >> x = [1:10] >> y = f1(x) y = 2*x.^2 + 5*x – 3 >> y = f1(x) y = 4 15 30 49 72... M-File: f1.m User-Defined Functions ??? Error !! Why? SAVE !!!

Open a new M-File Define the function function [Joules] = BTUs2Joules(BTUs) % This function converts BTUs to Joules Joules = BTUs * 1055.056 SAVE ! the M-File.... BTUs2Joules.m The FunctionName and the FileName MUST be the same ! ! ! Let's create a function to convert BTUs to Joules User-Defined Functions

function [Joules] = BTUs2Joules(BTUs) % This function converts BTUs to Joules Joules = BTUs * 1055.056 >> B1 = 1 >> J1 = BTUs2Joules(B1) J1 = 1055.056 >> J5 = BTUs2Joules(5) J5 = 5275.280 BTUs2Joules.m User-Defined Functions % Convert 1 BTU to Joules % Convert 5 BTUs

For a ballistic projectile, thrown with an initial velocity v 0, at an angle theta, create a function to calculate the Distance, Height, and Time in the air. User-Defined Functions Functions can have several inputs and several outputs

function [D,H,T] = ballistic(v0,theta) % For a ballistic object thrown with speed v0 at % an angle theta, this function calculates the % Distance (D), Height (H), and Time (T) of the % object's flight. All dimensions are in meters. g = 9.81; D = v0^2*sind(2*theta)/g; H = v0^2*(sind(theta))^2/(2*g); T = 2*v0*sind(theta)/g; ballistic.m User-Defined Functions Save the file as: ballistic.m !

function [D,H,T] = ballistic(v0,theta) g = 9.81; D = v0^2*sind(2*theta)/g; H = v0^2*(sind(theta))^2/(2*g); T = 2*v0*sind(theta)/g; >> v0 = 50; >> theta = 45; >> [d,h,t] = ballistic(v0,theta) d = 254.8420 h = 63.7105 t = 7.2080 User-Defined Functions

User-Defined Functions can be called from: the command window inside MatLab programs (script files) Every programming language has this capability MATLAB calls it a User-Defined Function In FORTRAN and BASIC it’s a Subroutine In PASCAL it’s a Procedure In C it’s a Function In programming literature it’s a Subroutine User-Defined Functions

1.Syntax function [output list] = Function_Name(input list) 2.Save the function m-file as Function_Name.m 3.The function definition is the first line in the m-file. 4.All functions use Local Variables. 5.Functions can have any combination of scalars, vectors and arrays (and string variables, too) as inputs and outputs. Rules for User-Defined Functions

Download ppt "Functions in MatLab Create a new folder on your Z:drive called MatLab_Class24 Start MatLab and change your current directory to MatLab_Class24 Topics:"

Similar presentations