# Using an anonymous function handle as input into another function handle

4 views (last 30 days)
Noob on 26 Sep 2023
Answered: Steven Lord on 26 Sep 2023
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?

Matt J on 26 Sep 2023
Edited: Matt J on 26 Sep 2023
It has nothing to do with nesting of hte handles. If you don't call an anonymous function with inputs, it can't return anything numerical
f=@(t,y) t+y;
f(1,2) + 3
ans = 6
f + 3
Operator '+' is not supported for operands of type 'function_handle'.

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

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 = function_handle with value:
@(t)omega(t,1:10)+10
z(5) % Evaluate z, which will evaluate omega(t, 1:10) [for t = 5] as part of its evaluation
ans = 1×10
16 19 24 31 40 51 64 79 96 115
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)
q = 5
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.

### Categories

Find more on Programming in Help Center and File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!