setHandle

Purpose

Assign function handle to existing Function object

Syntax

setHandle(F,@fun)
F.setHandle(@fun)

Description

Overwrites function handle of the Function object with a new one. This method is suitable for specification of functions where the parameters are stored under Data property. Look at examples for better explanation.

Input Arguments

F

Existing Function object which function we want to overwrite.

Class: Function

F

Representation of the new function given as function_handle class.

Class: function_handle

Output Arguments

F

Modified Function objects.

Class: Function

Example(s)

Example 1

Construct anonymous function ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle1.png where ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle2.png is a parameter. Assume that the value of ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle3.png is not going to change in the future. It suffices to declare the value of ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle4.png before constructing the object, i.e.
 k=2; 
 Then we can construct the object with this value of the parameter as 
 F1 = Function(@(x) k*x) 
Function: @(x)k*x
Evaluating this function will always take ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle5.png value, e.g in this case we get 2
 feval(F1.Handle,1) 
ans =

     2

Changing the value of ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle6.png does not affect the function
 k=3; 

 feval(F1.Handle,1) 
ans =

     2

If the value of ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle7.png may change in the future, one option is to make function depedent on two variables
 F2 = Function(@(k,x) k*x) 
Function: @(k,x)k*x
Evaluation of this function requires two arguments, i.e.
 feval(F2.Handle,2,1) 
ans =

     2

Another option is to store the parameter ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle8.png inside the Data property. Let's create empty function with the initial value ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle9.png
 F3 = Function([],struct('k',2)) 
Empty Function
Now we can assign the function handle with the reference to the parameter ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle10.png stored inside F3 object, i.e.
 F3.setHandle(@(x) F3.Data.k*x) 
Function: @(x)F3.Data.k*x
If we change the value of the parameter ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle11.png inside the object
 F3.Data.k = 3; 
 this new value will be taken for evaluation.
 feval(F3.Handle,1) 
ans =

     3

Example 2

Construct anonymous function ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle12.png with parameters ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle13.png and ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle14.png. Firstly, construct empty Function object and store the parameters in the Data property.
 F = Function([], struct('a',10.4,'b',-0.56)) 
Empty Function
Secondly, set the handle pointing to the parameters stored inside Data property
 F.setHandle(@(x)F.Data.a*sin(F.Data.b*x)) 
Function: @(x)F.Data.a*sin(F.Data.b*x)
The values of the parameters ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle15.png, ../../../../../../fig/mpt/modules/geometry/functions/@Function/sethandle16.png can be modified after construction of the object. The Function object will then take the new values of the parameters.
 F.Data.a = 12.78; F.Data.b = -0.93 
Function: @(x)F.Data.a*sin(F.Data.b*x)

Example 3

For functions arrays you must correctly refer to the appropriate index of the function data Allocate an array of Function objects
 F(2,1) = Function 
Array of 2 Functions.
Assign the data
 F(1).Data = struct('a',-1,'b',2); 

 F(1).setHandle(@(x)F(1).Data.a*sin(F(1).Data.b*x)); 

 F(2).Data = struct('a',-3,'b',4); 

 F(2).setHandle(@(x)F(2).Data.a*sin(F(2).Data.b*x)); 

Example 4

Assignment of multiple function handles is possible as well. Allocate the array of 2 Function objects.
 F(2) = Function 
Array of 2 Functions.
Assign two function handles must be done in a cell
 F.setHandle({@(x)x, @(x)x^2}) 
Array of 2 Functions.

See Also

afffunction, quadfunction, sethandle


© 2010-2013 Martin Herceg: ETH Zurich, herceg@control.ee.ethz.ch