Help understanding how function handle is used
조회 수: 2 (최근 30일)
이전 댓글 표시
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]]
댓글 수: 0
채택된 답변
Torsten
2024년 8월 2일
편집: Torsten
2024년 8월 2일
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
Torsten
2024년 8월 2일
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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!