Help understanding how function handle is used

2 views (last 30 days)
So I had an extremely helpful replier in an earlier question given me an equation for an interpolation function (I didn't know about the floor and ceil function which would've answered my question and given me the opportunity to try writting the interpolation function myself, but I'm not going to look a gift horse in the mouth)
function fitness = interFt(Ft, x, j, k, t)
% Find the floor and ceiling for j and k
j_floor = floor(j);
j_ceil = ceil(j);
k_floor = floor(k);
k_ceil = ceil(k);
% Calculate probabilities based on the distance from the actual values
p_j_floor = j_ceil - j;
p_j_ceil = j - j_floor;
p_k_floor = k_ceil - k;
p_k_ceil = k - k_floor;
% Calculate the weighted fitness for each combination
fitness = 0;
fitness = fitness + p_j_floor * p_k_floor * Ft(x, j_floor, k_floor, t);
fitness = fitness + p_j_floor * p_k_ceil * Ft(x, j_floor, k_ceil, t);
fitness = fitness + p_j_ceil * p_k_floor * Ft(x, j_ceil, k_floor, t);
fitness = fitness + p_j_ceil * p_k_ceil * Ft(x, j_ceil, k_ceil, t);
end
They also gave me an example of how to use the function, but they used a function handle and i'm not sure how it's being used
for tt=19:-1:1
for i=1:15
for j=1:15
for k=1:15
% here is a snipet of my code that I'm using with one of the
% examples of how I'm using the InterFt function; it's set up
% exactly how the replier showed
state1 = interFt(@(x,j,k,t) Ft(x,j,k,t), xp(i),zp(j),yy(k),tt+1);
end
end
end
end
Why is Ft not included in the @()?
Why is it that when I change the x's to i's it messes things up (while also changing the x to an i in the written function)?
I know you're not supposed to include the entire code, but let me know if that's needed to see what I mean
Here is the link to the original question: https://www.mathworks.com/matlabcentral/answers/2119211-creating-a-function-for-linear-interpolation-based-on-two-changing-states?s_tid=srchtitle
[[I have a lot of code that I think has something wrong with it, and I think has to do with the states, but I'm not sure how I need to "fix" it]]

Accepted Answer

Torsten
Torsten on 2 Aug 2024
Edited: Torsten on 2 Aug 2024
Somewhere in your code you defined a fitness function which assumes that inputs j and k are integers
function fitness = Ft(x,j,k,t)
%Some computations
fitness = ...;
end
Now you want to define a fitness value for j and k not being integers by weighting the values of Ft in the rectangle surrounding the point (j,k):
function fitness = interFt(Ft, x, j, k, t)
% Find the floor and ceiling for j and k
j_floor = floor(j);
j_ceil = ceil(j);
k_floor = floor(k);
k_ceil = ceil(k);
% Calculate probabilities based on the distance from the actual values
p_j_floor = j_ceil - j;
p_j_ceil = j - j_floor;
p_k_floor = k_ceil - k;
p_k_ceil = k - k_floor;
% Calculate the weighted fitness for each combination
fitness = 0;
fitness = fitness + p_j_floor * p_k_floor * Ft(x, j_floor, k_floor, t);
fitness = fitness + p_j_floor * p_k_ceil * Ft(x, j_floor, k_ceil, t);
fitness = fitness + p_j_ceil * p_k_floor * Ft(x, j_ceil, k_floor, t);
fitness = fitness + p_j_ceil * p_k_ceil * Ft(x, j_ceil, k_ceil, t);
end
And now you want the fitness value at some arbitrary point (xp,jp,kp,tp):
fitness = interFt(@Ft,xp,jp,kp,tp)
That's all.
You don't need
fitness = interFt(@(x,j,k,t) Ft(x,j,k,t),xp,jp,kp,tp)
if your fitness function Ft should be called with the usual order of the input arguments (and this is the case in function interFt because Ft is always called as Ft(xp,some j, some k, tp)).
The only question that remains is:
Why do you overwrite state1 again and again in your 4-fold loop?
  4 Comments
Kitt
Kitt on 2 Aug 2024
x and t are integers that range from 1-15 for x and 1-20 for t
the interpolation was for the informational states.
One thing that I was thinking was "oh wait, I have the physical state in the loop represented as "i" and the time represented as "tt", I should change that annotation in the interpolation function (so x and t where changed to i and tt when creating the function) and in the call
for tt=19:-1:1
for i=1:15
for j=1:15
for k=1:15
%I substituted what I originally had:
state1 = interFt(@(x,j,k,t) Ft(x,j,k,t), xp(i),zp(j),yy(k),tt+1);
%for this version:
state1 = interFt(@(i,j,k,tt) Ft(i,j,k,tt), xp(i),zp(j),yy(k),tt+1);
The code did not like that and it totally botched up my results. Is it confused because the same symbol is being used in different ways, or is this actually the way I should be doing it?
If you can't answer or aren't getting it that's totally fine! I'm very bad at explaining it because I've been so deep in for like 8 months now. I can always go over it with my PI when I meet them next, but sometimes it can be faster and easier to tell strangers online I have no idea what I'm doing than my PI. Hopefully my humor here is understood
Torsten
Torsten on 2 Aug 2024
If Ft is a 4d-matrix, pass it to interFt as a matrix instead of a function handle:
fitness = interFt( Ft,xp,jp,kp,tp)

Sign in to comment.

More Answers (0)

Categories

Find more on Spline Postprocessing 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!