How to pass an argument for a function when it is an argument in another function?
Mostrar comentarios más antiguos
I would like to pass a function to another function, but with a parameter for it to use. for example, I have written a cobweb function to plot a cobweb of an iterative series function. It takes a function as an argument. eg. cobweb(@sin, x0, [0 1.5 0 1.5], max_iter) returns a cobweb plot like below for repeated sin function.

However, I want to pass another function I've written, an iterative map
function x1 = it_map(x0, r)
x1 = r*x0*(1-x0);
however to do this using the same code, I'd need to pass this function in with its own argument (r), and I can't see anyway to do that.
Is there anyway to do this? or should I just fix the argument as r in the code, which is fiddly..
Respuesta aceptada
Más respuestas (1)
Guillaume
el 23 de Feb. de 2018
"Is there anyway to do this?"
Yes, it's call argument binding. In matlab, this is achieved by defining an anonymous function:
r = 5;
it_map_bound = @(x0) it_map(x_0, r);
cobweb(it_map_bound, x0, [0 1.5 0 1.5], max_iter);
Note that the anonymous function capture the value of r at creation. Changing r afterward does not affect the anonymous function.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!