How to pass additional variables into a function handle that is taken as an argument?
83 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Zhangxi Feng
il 30 Mag 2018
Commentato: Zhangxi Feng
il 31 Mag 2018
How to pass additional variables into a function handle that is taken as an argument?
For example, the integral function is used as:
Q = integral(fun,A,B)
Where fun is initialized as the function handle:
fun = @myFunction
That can be otherwise evaluated as:
y = myFunction(x)
I understand if I need to pass in additional parameters into myFunction, I can do the following:
y = myFunction(x,a,b,c,d)
But how do I do that with the integral function that takes the handle as an argument? Would it be like this?
Q = integral(fun(x,a,b,c),A,B)
Just to put it into perspective, myFunction would be an error evaluating function that returns 20 results at a time in a 20x1 column array. The 20x1 array is then minimized in an optimization algorithm such as the genetic algorithm with multiple objectives, so things like nested functions will not work so well in terms of sharing the extra parameters. Currently, I am using global variables which is working fine but I would like to try to avoid the potential problems as much as possible.
Thanks!
0 Commenti
Risposta accettata
Walter Roberson
il 30 Mag 2018
3 Commenti
Guillaume
il 30 Mag 2018
Of course, your function can be defined with an anonymous function.
Following your example, let's say you want to use myFunction (defined any way you want) with integral with additional arguments a, b, c, and d, then
Q = integral(@(x) myFunction(x, a, b, c, d), A, B)
where @(x) myFunction(x, a, b, c, d) is the anonymous function that binds the arguments to myFunction.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Genetic Algorithm in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!