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개)
"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.
Mohammad Qasem
2018년 11월 20일
Stephen23
2018년 11월 20일
"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
2018년 11월 20일
"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
2018년 11월 22일
Mohammad Qasem
2018년 11월 27일
카테고리
도움말 센터 및 File Exchange에서 Function Handles에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!