MATLAB: How can i pass a handle function as a variable for another function?

projecturget

i have a handle function t = @(phi) function, and i want to make this handle function a variable t(phi) for another handle function d(t) = @(t(phi)) becuase i want to integrate d(t) with respect to t not phi. i don't know if it's possible using handle function. i hope someone help with this method or any other method so i can integrate d with respect to t.

Best Answer

There are two different questions here:
1. Can you pass a function handle as input to another function handle. Yes.
f1 = @(f, x) f(x); % f shoule be a function handle, x in input to x2
f2 = @(y) y.^2;
a = f1(f2, 3) % evaluate f2 with input 3
Result
a =
9
2. You want to integrate d with respect to t.
If that is the case, you don't need to get phi involved in it. Note that the input variable name of the anonymous function can be any valid variable name. It does not affect if the variable with the same name exists in the workspace. Anonymous functions have their own scope.
t = @(phi) phi.^2;
d = @(t) t; % this t has nothing to do with t defined above. Both are independent
b = integral(d, 0, 1)
Result
b =
0.5000