1 / 14

Functions in MatLab

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

johnda
Download Presentation

Functions in MatLab

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

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

  4. 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 ! ! !

  5. 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

  6. 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

  7. 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 !!!

  8. 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 ! ! !

  9. 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

  10. 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.

  11. 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 !

  12. 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

  13. 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

  14. 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.

More Related