Using an anonymous function handle as input into another function handle
7 views (last 30 days)
Show older comments
Hi there!
I have a question that's mostly about syntax: Let's say I write the anonymous function handle (for a program to give to ode45 to solve):
omega = @(t, y) ...
And then I want to use omega as input to the next function.
How come I have to input omega as omega(t, y)?
When I input just omega, without the (t, y), I get an error message about it being a function handle.
Is omega(t, y) a scalar quantity, while omega is a function handle?
Is it like specifying that omega takes two inputs t and y?
Another related question is:
Later, when I got to solve my odes, how can I evaluate omega at some time t and some value y?
Is it using the deval function?
Thanks so much in advance,
0 Comments
Answers (3)
Walter Roberson
on 26 Sep 2023
Your premise is incorrect. The below example shows that it is valid to pass function handles between multiple levels
omega = @(t, y) cos(3*pi*y + t);
plotdriver(omega)
function plotdriver(g)
plotit(g, -10, 10, -1, 1)
end
function plotit(f, lb1, ub1, lb2, ub2)
fsurf(f, [lb1, ub1, lb2, ub2])
end
0 Comments
Steven Lord
on 26 Sep 2023
And then I want to use omega as input to the next function.
What specifically do you mean "as input to the next function"?
How come I have to input omega as omega(t, y)?
That depends on what you're trying to do. If you want to pass the function handle omega into the function you don't have to specify t and y. If you want to evaluate the function handle and operate on its output you need to call omega.
omega = @(t, y) t+y.^2; % function handle
fsurf(omega) % pass the function handle into fsurf and let fsurf evaluate it
z = @(t) omega(t, 1:10) + 10 % evaluate omega when z is evaluated and work with its output
z(5) % Evaluate z, which will evaluate omega(t, 1:10) [for t = 5] as part of its evaluation
When I input just omega, without the (t, y), I get an error message about it being a function handle.
MATLAB does not know how to add function handles (or multiply function handles, or divide function handles, or really perform any sort of math on function handles.) It knows how to evaluate function handles to get numbers from them and it knows how to add / multiply / divide / perform math on numbers. That's what the anonymous function z above does: it evaluates omega then adds the number(s) returned and 10 together.
Is omega(t, y) a scalar quantity, while omega is a function handle?
What size omega(t, y) is will depend on the sizes of t and y and what instructions are inside omega. You can see when I evaluated z(5) above that it called omega(5, 1:10) which returned a 1-by-10 array. I could just as easily have called omega with two scalars and received a scalar output.
q = omega(1, 2)
Is it like specifying that omega takes two inputs t and y?
omega = @(t, y) something does indicate that omega is a function handle that accepts two input arguments. Using omega(t, y) calls the omega function with two input arguments.
If you haven't already, you might want to read through the first section on this documentation page for a basic description of how anoynmous functions are defined and how to call them.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!