32vues (au cours des 30derniers jours)
Afficher commentaires plus anciens
Kitt le 2 Août 2024 à 14:43
Commenté: Torsten le 2 Août 2024 à 19:48
Ouvrir dans MATLAB Online
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]]
0commentaires Afficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens
Afficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens
Connectez-vous pour commenter.
Connectez-vous pour répondre à cette question.
Réponses (1)
Torsten le 2 Août 2024 à 15:59
Modifié(e): Torsten le 2 Août 2024 à 16:01
Ouvrir dans MATLAB Online
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?
4commentaires Afficher 2 commentaires plus anciensMasquer 2 commentaires plus anciens
Afficher 2 commentaires plus anciensMasquer 2 commentaires plus anciens
Kitt le 2 Août 2024 à 17:00
Utiliser le lien direct vers ce commentaire
https://fr.mathworks.com/matlabcentral/answers/2142461-help-understanding-how-function-handle-is-used#comment_3227846
Ouvrir dans MATLAB Online
I have Ft as a matrix, not a function. The only thing I specify is terminal fitness,
Ft=zeros(cap,cap,cap,term);
Ft(x<=crit,:, :, term)=0;
Ft(x>crit,:,:,term)=1;
So some context to the model for clarity on what I'm doing and why:
I'm using backwards induction to identify an individual's choice of watching or not watching a demonstrator. The maximum fitness is either the fitness equation of watching or the fitness equation of not watching. Those two equations are based on the probabilities of being in several states (for watching they could be in 16 different states and not watching in 4 different states).
So an individual's state changes each timestep depending on their physical state (x) and their informational states (j and k). The reason it gets overridden is because the combination of all the probabilities of these states is stored in the larger function of whether or not they watched. I hope that makes sense.
It could be this is a poor way of doing that, and maybe the states should all be kept in their own matrix? I've been suggested that, as it could slim down my code and make it more efficient as my equations are very long. [Granted, my PI said that if it works, it works and I don't need to be fancy if I don't have to]
Torsten le 2 Août 2024 à 18:33
Utiliser le lien direct vers ce commentaire
https://fr.mathworks.com/matlabcentral/answers/2142461-help-understanding-how-function-handle-is-used#comment_3227921
How can Ft be a matrix if the physical state x and the time t used as arguments in Ft(x,j,k,t) can be non-integers ?
Kitt le 2 Août 2024 à 18:58
Utiliser le lien direct vers ce commentaire
https://fr.mathworks.com/matlabcentral/answers/2142461-help-understanding-how-function-handle-is-used#comment_3227956
Ouvrir dans MATLAB Online
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 le 2 Août 2024 à 19:48
Utiliser le lien direct vers ce commentaire
https://fr.mathworks.com/matlabcentral/answers/2142461-help-understanding-how-function-handle-is-used#comment_3228006
Ouvrir dans MATLAB Online
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)
Connectez-vous pour commenter.
Connectez-vous pour répondre à cette question.
Voir également
Tags
- interpolation
- function handle
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Une erreur s'est produite
Impossible de terminer l’action en raison de modifications de la page. Rechargez la page pour voir sa mise à jour.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asie-Pacifique
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contactez votre bureau local