Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching...

27
1 CH5: USER DEFINED FUNCTIONS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS Matlab Programming for Engineers Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Introduction to Matlab Matlab Basics Matlab Basics Branching Branching Statements Statements Loops Loops User Defined Functions User Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)

Transcript of Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching...

Page 1: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

1

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

Matlab Programming for Engineers Matlab Programming for Engineers Matlab Programming for Engineers Matlab Programming for Engineers

Dr. Nidal Farhat

Introduction to MatlabIntroduction to Matlab Matlab Basics Matlab Basics Branching Branching Statements Statements Loops Loops User Defined FunctionsUser Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)

Introduction to MatlabIntroduction to Matlab Matlab Basics Matlab Basics Branching Branching Statements Statements Loops Loops User Defined FunctionsUser Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)

Page 2: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

2

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

CONTENTS CONTENTS

Introduction to Matlab Functions

Optional Arguments

Sharing Data Using Global Memory

Preserving Data Between Calls to a Function

Function Functions

Subfunctions

Page 3: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

3

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

INTRODUCTION INTRODUCTION

Advantages: Independent Testing of Sub-Tasks:

Each task is an independent unit that can be tested separately (unit testing).

Reusable Code: Within the same program (the code is not repeated). It can be used in another program.

Isolation from unintended side effects: Isolation of function variables from the main program variables. The input and the output variables are specified clearly.

Page 4: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

4

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

INTRODUCTION INTRODUCTION

Dummy arguments or placeholders

The name of the m.file must be the same as the

fname.

The function can be called directly in the

command window, or by any other program or

function.

Displayed if a text is found by lookfor command

Displayed if the function is found by help command

Optional

(end)

Important: if the input variables are changed in the functions they will not change in the main program (PASS-BY-VALUE SCHEME)

Page 5: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

5

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

INTRODUCTIONINTRODUCTION

Example,

Page 6: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

6

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

INTRODUCTIONINTRODUCTION

Example,

Page 7: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

8

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

INTRODUCTIONINTRODUCTION

Exercise, program a function that sorts the data in ascending order.

Page 8: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

9

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

OPTIONAL ARGUMENTS (Input and Output)OPTIONAL ARGUMENTS (Input and Output)

Optional output Optional input

Page 9: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

10

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

OPTIONAL ARGUMENTSOPTIONAL ARGUMENTS

arguments

Page 10: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

11

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

OPTIONAL ARGUMENTSOPTIONAL ARGUMENTS

nin = nargin;

nout = nargout;

message = nargchk(min_arg,max_arg,num_arg);

message empty if OK, otherwise standard error message is returned

error ('message')

warning ('message')

name = inputname(argno);

Page 11: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

12

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

OPTIONAL ARGUMENTSOPTIONAL ARGUMENTS

Example,

Page 12: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

13

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

SHARING DATA USING GLOBAL MEMORYSHARING DATA USING GLOBAL MEMORY

Global memory is a special memory that can be accessed from any workspace.

Syntax:

global var1 var2 var3 …

Remark:

Each global variable must be declared to be global before it is used for the first time in a function. Better to be the first statement in the function after the initial comments.

Page 13: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

14

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

PRESERVING DATA BETWEEN CALLS TO A FUNCTION PRESERVING DATA BETWEEN CALLS TO A FUNCTION

Each time the function is finished data in the workspace is destroyed. To make some variable persistent for the next function calls, use the persist command. Special type of memory that can be accessed only from the same function.

Syntax:

persistent var1 var2 var3 …

Page 14: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

15

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

PRESERVING DATA BETWEEN CALLS TO A FUNCTION PRESERVING DATA BETWEEN CALLS TO A FUNCTION

Exercise: running averages and standard deviations. User must enter the number of values in data set, and then enter value by value. Each time he enters a value the program calls the function and then displays the current output (average and standard deviation).

Use this table to test your program.

Page 15: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

16

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

FUNCTION FUNCTIONS FUNCTION FUNCTIONS

Input variables of this type of function is the name of other functions.

Example,

Page 16: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

17

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

FUNCTION FUNCTIONS FUNCTION FUNCTIONS

Page 17: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

18

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

FUNCTION FUNCTIONS FUNCTION FUNCTIONS

, ,

Page 18: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

19

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

FUNCTION FUNCTIONS FUNCTION FUNCTIONS

Page 19: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

20

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

FUNCTION FUNCTIONS FUNCTION FUNCTIONS

Example, make a function that plots directly any function providing the function name and interval.

Page 20: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

21

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

SUBFUNCTIONS SUBFUNCTIONS

Functions in the same file

Normal/principle function:The m-file name should be mystats

Subfunction:Accessible only by the other functions in the same file

Page 21: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

22

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)

Three dimensional line plots:

plot3(x,y,z);

Example: (2 dimensional )

Page 22: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

23

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)

Three dimensional line plots:

plot3(x,y,z);

Example: (3 dimensional)

Page 23: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

24

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)

Three dimensional surface, mesh, and contour plots:

Page 24: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

25

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)

Three dimensional surface, mesh, and contour plots:

Page 25: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

26

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)

Three dimensional surface, mesh, and contour plots:

Page 26: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

27

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)

Three dimensional surface, mesh, and contour plots:

Page 27: Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.

28

CH5: USER DEFINED FUNCTIONS

Mechanical Engineering Department

MATLAB PROGRAMMING FOR ENGINEERS

ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL) ADDITIONAL PLOT FEATURES (THREE DIMENSIONAL)

Three dimensional surface, mesh, and contour plots: