COMPLETE GUIDE TO MATLAB ® Chapter 3 : Types of Functions.
-
Upload
bethanie-phelps -
Category
Documents
-
view
215 -
download
0
Transcript of COMPLETE GUIDE TO MATLAB ® Chapter 3 : Types of Functions.
COMPLETE GUIDE TO MATLAB ®
Chapter 3 : Types of Functions
Types of Functions
Chapter Outline
• Nested functions
• Private functions
• Overloaded functions
• Function handles
Types of Functions
function T = tax(income)
adjusted_income = max(income - 5000, 0);T = compute_tax;
function t = compute_tax t = 0.28*adjusted_income; end
end
%call function%tax(10000)
function T = tax(income)
adjusted_income = max(income - 5000, 0);T = compute_tax;
function t = compute_tax t = 0.28*adjusted_income; end
end
%call function%tax(10000)
Nested Function
Types of Functions
Private Functions• Reside in a subdirectory named private
• Only accessible to functions in parent directory
Only accessible to functions inparent directory.
privatedirectory
Types of Functions
Chapter Outline
• Nested functions
• Private functions
• Function handles
Types of Functions
Object Oriented Programming (OOPs) in MATLABWhat is Object-Oriented Programming?
Helps you work on big projects which has activities/elements with properties that can be grouped in a structured way
What are operations and how do I perform them?
1) First define the elements – class2) Define the properties of the class3) Define methods – constructor for creating new
objects/elements/activities4) Other Methods are defined to manipulate the input
data
Types of Functions
Classes
In class definition we create a prototype or specification for construction of a objects of a certain class or type.
Eg. classdef person
Objects
Run-time instance of a class
Object Oriented Programming
PropertiesList of properties by which the class is desired to be
characterized
Types of Functions
classdef Personpropertiesnameagegenderendmethods
function obj=Person(name,age,gender)obj.name=name;obj.age=age;obj.gender=gender;
End
function y=isRetired(obj) y=obj.age > 65;end
endend
Types of Functions
>> x=Person('john',35,'male') x = Person Properties: name: 'john' age: 35 gender: 'male' Methods
>> x.isRetired ans = 0
Types of Functions
Exercise
Extend the Person class with a list of book numbers that a person borrowed and the method holdsBook to check whether a person has borrowed a particular book. Check your class with the
commands:
x=Person('John',35,'male');
x.books=[1 2 3 4];
x.holdsBook(3)
Access to object properties
x=Person('John',35,'male');
x.age=70;
x.isRetired
returns ‘1’
Types of Functions
Use ‘handle’ to pass class – person as reference not a value.
classdef Person < handle
function changeAge(obj,age)
obj.age=age;
end
>>x=Person('John',35,'male');
>>x.changeAge(70);
>>x.age
Types of Functions
classdef date % write a description of the class here. properties % define the properties of the class here, (like fields of a struct) minute = 0; %calcSecs(d2) hour; day; month; year; end properties(Constant = true) DAYS_PER_YEAR = 365; MONTHS_PER_YEAR = 12; WEEKS_PER_YEAR = 52; end Methods (Access=public) % Change to private and run % methods, including the constructor are defined in this block function obj = date(minute,hour,day,month,year) % class constructor if(nargin > 0) obj.minute = minute; obj.hour = hour; obj.day = day; obj.month = month; obj.year = year; end end function obj = rollDay(obj,numdays) obj.day = obj.day + numdays; endend end
Types of Functions
……..Contd
Create Object
>>d1=date(0,3,27,2,1998)>>d2=date(1,0,0,0,2002)
Properties
>>day =d1.day %Access the day property>>d1.year=2008; %Set the year property
Methods
>>d1=rollDay(d1,3) % rollDay methods adds a specified number of days to the day property
Exercise
Add the following methods
function sec=calcSecs(obj) sec=obj.minute*60 + obj.hour*60*60+obj.day*24*60*60;end
And calculate secs for object date 1 min.
1st argument is the object of the class
Types of Functions
feval Function
• Syntax
• If function is a quoted string containing the name of a function (usually defined by an M-file), then feval(function,x1,...,xn) evaluates the function with the given arguments.
[y1,y2,...] = feval(function,x1,...,xn)[y1,y2,...] = feval(function,x1,...,xn)
>> x = 0:.1:2*pi;>> y = feval('sin', 0:.1:2*pi);>> plot(x,y)
>> x = 0:.1:2*pi;>> y = feval('sin', 0:.1:2*pi);>> plot(x,y)
Types of Functions
Representing Mathematical Functions
• A mathematical function can be represented in MATLAB as a function
function y = my_humps(x)y = 1 ./ ((x-.3).^2 + .01) + 1 ./ ((x-.9).^2 + .04) - 6;
function y = my_humps(x)y = 1 ./ ((x-.3).^2 + .01) + 1 ./ ((x-.9).^2 + .04) - 6;
6-0.040.9)-(x
1
01.0.3)-(x
1 F(x)
22
Types of Functions
Function Handles
• What are they?
• MATLAB data types that contain information used in referencing a function.
• Benefits:
• Allow wider access to subfunctions and private functions.
• Improve performance in repeated operations.
• Reduce the number of files that define your function.
• Ensure reliability when evaluating functions.
Types of Functions
Function Handles – continued• Syntax
• fhandle = @function_name
• Example
function y = my_humps(x)y = 1 ./ ((x-.3).^2 + .01) + 1 ./ ((x-.9).^2 + .04) - 6;% ----- end of file my_humps.m -----
>> x = 0:.1:2; >> fhandle = @my_humps;>> out = feval(fhandle,x); % or… out = feval('my_humps',x);>> plot(x,out);
function y = my_humps(x)y = 1 ./ ((x-.3).^2 + .01) + 1 ./ ((x-.9).^2 + .04) - 6;% ----- end of file my_humps.m -----
>> x = 0:.1:2; >> fhandle = @my_humps;>> out = feval(fhandle,x); % or… out = feval('my_humps',x);>> plot(x,out);
Types of Functions
Function Handles – continued
• The command functions allows the function handle information to be displayed.
• This function is helpful when working with overloaded function handles.
>> fhandle = @my_humps;>> functions(fhandle)ans = function: 'my_humps' type: 'simple' file: 'c:\class\my_humps.m'
>> fhandle = @my_humps;>> functions(fhandle)ans = function: 'my_humps' type: 'simple' file: 'c:\class\my_humps.m'
Types of Functions
Function Handle Operations
• Converting function handle to function name
• Converting function name to function handle
>> fhandle = @sin;>> func2str(fhandle)ans =sin
>> fhandle = @sin;>> func2str(fhandle)ans =sin
>> fh = str2func('sin')fh = @sin
>> fh = str2func('sin')fh = @sin
Types of Functions
Section Summary• Nested Function
• Private Function
• Objects in MATLAB
• Constructing a new class
• Function precedence
• Function Handles