How to create a loop on function handle

10 views (last 30 days)
I want to write the following in an efficent way using a loop over j = 1:n. A is n by n matrix, t(i) is a scalar (where i is another index, c is n by 1 vector, and f is a function handle. I want the output to be function handle XI which is an n by 1 vector and whose argument is the vector xi of length n by 1
Here n = 3, but I want it for general n.
A = ones(3,3);
c = ones(3,1);
f = @(t,y) t * y;
XI = @(xi) [xi - (y(i,:) + dt* A(:,1) .* f(t(i)+c(1)*dt,xi(1)) +...
dt* A(:,2) .* f(t(i)+c(2)*dt,xi(2)) +...
dt* A(:,3) .* f(t(i)+c(3)*dt,xi(3)))];

Accepted Answer

James Tursa
James Tursa on 24 Jun 2022
Edited: James Tursa on 24 Jun 2022
Isn't this just a standard matrix*vector multiply? E.g.,
XI = @(xi) xi - (y(i,:) + A*((t(i)+c*dt).*xi)*dt);
Note that this function handle will be pulling all the non-arguments A, y, i, t, and dt from the workspace at the time the function handle is created. I.e., it will take a shapshot of those variables and store them inside the function handle. I strongly suspect this isn't what you intended or wanted. To make the function handle responsive to changes in variables, you need to include those variables in the argument list. E.g.,
XI = @(xi,A,y,i,t,dt) xi - (y(i,:) + A*((t(i)+c*dt).*xi)*dt);
Now the function handle XI will be using the values of the variables passed into it for all the calculations, instead of just xi and past snapshots of the other variables as you have originally coded.
  2 Comments
Romio
Romio on 24 Jun 2022
Edited: Romio on 24 Jun 2022
Thanks!
The first expression might work. But it is not multiplication, these are inputs to the handle f and they are summed at the end.
Yes I want the argument of the handel XI only to be xi, and the rest are taken from the workspace.
James Tursa
James Tursa on 24 Jun 2022
Edited: James Tursa on 24 Jun 2022
"... But it is not multiplication ..."
Sure looks like your calculations boil down to a matrix*vector multiply to me.
" ... the rest are taken from the workspace ..."
Realize that they are taken from the workspace as snapshots at the time the function handle is created. They are not taken from the current workspace. Your current code would be unusual unless it was re-creating the function handle each time just prior to its being used. E.g.,
a = 4; t = 1:3; i = 1;
f = @(x) a + t(i)*x
f = function_handle with value:
@(x)a+t(i)*x
f(3)
ans = 7
i = 2;
f(3)
ans = 7
i = 3;
f(3)
ans = 7
Note that the f(3) results don't change when we change the value of i in the workspace. That is because the function handle f took snapshots of a, t, and i at the time f was created and stored them inside f. As far as f is concerned, a has a fixed value of 4, t is a fixed vector 1:3, and i has a fixed value of 1. f is nonresponsive to changes in any of these variables in the workspace. Once you create the function handle, the a, t, and i in the function handle are completely different variables than the a, t, and i in the workspace. Maybe this behavior is what you want, but it would be unusual (at least to me) given the indexed calculations involved unless f itself is constantly being redefined in your code. But only you can answer this.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!