250 likes | 535 Views
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: User-Defined Functions Function files. y = f(x). Functions in MatLab. Functions are special purpose programs
E N D
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: • User-Defined Functions • Function files
y = f(x) Functions in MatLab • 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
y = f(x) User-Defined Functions Consider the function f1(x) , where: y = f1(x) = 2 x2 + 5 x – 3 The name of the function is f1 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 = f1(x) = 2 x2 + 5 x – 3 • 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 FileNameMUST be the same ! ! !
User-Defined Functions M-File: f1.m 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 = f1(2) = 2 * 22 + 5 * 2 – 3 = 15
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 ??? Error !! Why? SAVE !!!
User-Defined Functions Let's create a function to convert BTUs to Joules • 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 FileNameMUST be the same ! ! !
User-Defined Functions BTUs2Joules.m 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 % Convert 1 BTU to Joules % Convert 5 BTUs
User-Defined Functions Functions can have several inputs and several outputs For a ballistic projectile, thrown with an initial velocity v0 , at an angle theta, create a function to calculate the Distance, Height, and Time in the air.
User-Defined Functions ballistic.m 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; Save the file as: ballistic.m !
User-Defined Functions 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
Rules for 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.