How i can use function handle in another function handle?
Mostrar comentarios más antiguos
f1 = @(x, y1, y2, y3) y2;
f2 = @(x, y1, y2, y3) y3;
f3 = @(x, y1, y2, y3) -0.5*y1*y3;
ff1 = @(xxx, yy1, yy2) yy2;
ff2 = @(xxx, yy1, yy2) -.5*f1.*yy2;
When i run the code i get the error
Undefined operator '*' for input arguments of type 'function_handle'.
I need to use f1 in ff2
Respuestas (1)
"How i can use function handle in another function handle?"
Exactly like any other time you use a function, you have to call it with all of its required input arguments. E.g.:
>> f1 = @(x,y) 2*x + sqrt(y);
>> f2 = @(a,b) f1(a,b) - 1;
>> f2(2,4)
ans = 5
7 comentarios
So to be explicit,
ff2 = @(xxx, yy1, yy2) -.5*f1(xxx, yy1, yy2).*yy2;
While we're at it, I would define ff1 as:
ff1 = @(~, ~, yy2) yy2;
to make it clear that the fact it doesn't use the first two inputs is intended.
Mohammad Qasem
el 20 de Nov. de 2018
Stephen23
el 20 de Nov. de 2018
"i need to use y1 in f22 as follow but it does not work"
y1 either needs to be defined as an input argument, or it needs to exist in the workspace where you create that anonymous function. Only you can decide which of these is appropriate for your situation.
What you cannot do is refer to a variable that simply does not exist anywhere, which is what you are currently trying to do.
Mohammad Qasem
el 20 de Nov. de 2018
"could you please find the solution for me because i don't have an experiance in MATLAB?"
Of course, I am happy to help you. You just need to tell us where y1 is defined, either
- in the workspace where the anonymous function is created, or
- as an input argument to the anonymous function.
I cannot decide this for you. Only you can decide this, becuse only you know the algorithm that you are trying to encode. Once you tell us which of 1. or 2. you need for the variable y1, then we can show you how to write that using MATLAB.
Stephen23
el 22 de Nov. de 2018
Mohammad Qasem
el 27 de Nov. de 2018
Categorías
Más información sobre Function Handles 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!