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

Antworten (1)

Stephen23
Stephen23 am 20 Nov. 2018
Bearbeitet: Stephen23 am 20 Nov. 2018

0 Stimmen

"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 Kommentare

Guillaume
Guillaume am 20 Nov. 2018
Bearbeitet: Guillaume am 20 Nov. 2018
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?
Stephen23
Stephen23 am 21 Nov. 2018
Bearbeitet: Stephen23 am 21 Nov. 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
  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

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 20 Nov. 2018

Erneut geöffnet:

am 7 Dez. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by