Download presentation

Presentation is loading. Please wait.

Published byQuinn Cheever Modified over 2 years ago

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: 1.User-Defined Functions 2.Function files

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

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

4
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

5
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

6
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

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

8
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

9
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

10
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

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

12
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

13
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

14
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

Similar presentations

OK

12.010 Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room 54-820A, Chris Hill, Room 54-1511,

12.010 Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room 54-820A, Chris Hill, Room 54-1511,

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on zener diode symbol Ppt on world literacy day Download ppt on biodiversity in india Ppt on chromosomes and genes and locus Ppt on electricity and circuits for class 6 Ppt on water resources in india Raster scan display ppt online Ppt on auto turn off water pump Ppt on conservation of wildlife and natural vegetation in pakistan Electronic paper display ppt on ipad