Introduction to Programming for Mechanical Engineers Lecture 5.2
Things you should have known so far! Create simple user define functions. Correctly writing the function definition line and understanding all valid naming combination [outputs] = name(inputs). Know the differences between script files and user defined files. Define variables of interest to be global and know the difference between local and global variables. You should always remember to use the same name for the user defined function and the function file name.
Anonymous and Inline functions Anonymous functions: a user defined function that is defined and written within the computer code (not in a separate function file). You can use it in the code after that.. They can be defined in any part of MATLAB (Command window, script files, in user defined functions). Syntax: name = @ (inputs) expression Example: >> distance = @ (t,v,a) v*t+a*t^2/2 distance = @(t,v,a)v*t+a*t^2/2 To call it: >> distance(8,50,-9.81) 86.06 It is a one-line simple user defined function. The expression can use any built in or user defined functions.
Anonymous and Inline functions a user defined function that is defined and written within the computer code (not in a separate function file). You can use it in the code after that.. They can be defined in any part of MATLAB (Command window, script files, in user defined functions). Syntax: name = inline(‘mathematical expression written as a string’) name = inline(‘mathematical expression written as a string’,’arg1’ ,’arg2’,..) ’arg1’ ,’arg2’,.. Are used to rearrange the order of inputs Example: >> cube = inline('x^3') cube = Inline function: cube(x) = x^3 >> cube(3) ans = 27 >> Balance = inline('x+y-a^2','y','a','x') Balance = Balance(y,a,x) = x+y-a^2 >> Balance(1,2,5) 2
feval command Variable = feval (‘function name’,argument value) Example: >> r = feval('sqrt',9) r = 3 >> A = feval('power',3,3) A = 27
Sub-Functions Here the main function is proposed to calculate the area of a given triangle. It calls a subfunction named angle that returns the angle that is enclosed by the base and one of the sides. It then calculated the height through another subfunction named height. Notice that the order here is not important! Compare this to script files!!
Nested Functions: 1st level nested functions As before, the main function is proposed to find the area. It has two nested functions names angle and height. The difference here, if you are going to use nested functions, you have to use an end after each nested function’s body to tell MATLAB that the nested function is no more in action.
Nested Functions: 2nd level nested functions The nested function (multip) is a 2nd level nested function, since it is nested in the nested function (theta)
Functions: other functions Up to this point, this material should be enough to give students a thorough understanding of function files. Other functions and function definitions are recommended for self reading.
Thank you for the attention Any Questions and Suggestions please…