How i can use function handle in another function handle?

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

回答(1 个)

Stephen23
Stephen23 2018-11-20
编辑:Stephen23 2018-11-20
"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 个评论

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.
thank you
but if i need to use y1 what i should do as follow
f1 = @(x, y1, y2, y3) y2; %% f^
f2 = @(x, y1, y2, y3) y3; %% f^^
f3 = @(x, y1, y2, y3) -0.5*y1*y3; %% f^^^
i need to use y1 in f22 as follow but it does not work
ff1 = @(xxx, yy1, yy2) yy2; %% theta ^
ff2 = @(xxx, yy1, yy2) -0.5.*y1.*yy2; %% theta^^
"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.
could you please find the solution for me because i don't have an experiance in MATLAB?
"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
  1. in the workspace where the anonymous function is created, or
  2. 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.
@Mohammad Qasem: you forgot to answer my question: is the answer 1. or 2. ?
Thank you very much
I found the solution

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Function Handles 的更多信息

重新打开:

2018-12-7

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by