Help understanding how function handle is used (2024)

32vues (au cours des 30derniers jours)

Afficher commentaires plus anciens

Kitt le 2 Août 2024 à 14:43

  • Lien

    Utiliser le lien direct vers cette question

    https://fr.mathworks.com/matlabcentral/answers/2142461-help-understanding-how-function-handle-is-used

  • Lien

    Utiliser le lien direct vers cette question

    https://fr.mathworks.com/matlabcentral/answers/2142461-help-understanding-how-function-handle-is-used

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

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

Connectez-vous pour commenter.

Connectez-vous pour répondre à cette question.

Réponses (1)

Torsten le 2 Août 2024 à 15:59

  • Lien

    Utiliser le lien direct vers cette réponse

    https://fr.mathworks.com/matlabcentral/answers/2142461-help-understanding-how-function-handle-is-used#answer_1494061

  • Lien

    Utiliser le lien direct vers cette réponse

    https://fr.mathworks.com/matlabcentral/answers/2142461-help-understanding-how-function-handle-is-used#answer_1494061

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

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

  • Lien

    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:Help understanding how function handle is used (4)

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

  • Lien

    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

  • Lien

    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

  • Lien

    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.


Translated by Help understanding how function handle is used (8)

Help understanding how function handle is used (9)

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

Help understanding how function handle is used (2024)

FAQs

How do you use a function handle? ›

Function handles are variables that you can pass to other functions. For example, calculate the integral of x2 on the range [0,1]. q = integral(f,0,1); Function handles store their absolute path, so when you have a valid handle, you can invoke the function from any location.

What is function handle as input in MATLAB? ›

You can use function handles as input arguments to other functions, which are called function functions. These functions evaluate mathematical expressions over a range of values. Typical function functions include integral , quad2d , fzero , and fminbnd .

What do you use at handle for? ›

T-handle keys are commonly used in automotive repair, furniture assembly, and other applications where precise torque and control are required.

How do you use a function key? ›

Press and hold down the Fn key and another key simultaneously to perform a keyboard shortcut action. For example, to turn off the sound: Fn + (F2) Press and hold down the Fn key, then press the F2 key. Some keyboard functions can only be used while Windows is running.

What is function handle in Python? ›

The handle is just an object identifier you pass to functions — it doesn't get called.

How do you use a function procedure? ›

A Function procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. The Function procedure performs a task and then returns control to the calling code. When it returns control, it also returns a value to the calling code.

How do you use a crank handle? ›

Crank handles are manually operated. Most of them require hand-turning, meaning you grip the mechanical arm with a hand to turn it in a circular motion. With that said, some crank handles are leg-operated. They are essentially foot pedals that you turn by pushing down on the arm with one or both feet.

Top Articles
Molly-Mae Hague plays cheating prank on Tommy Fury in resurfaced clip
Killer Edward Johnston went on rant about public figures being “taken out”
Northern Counties Soccer Association Nj
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Goodbye Horses: The Many Lives of Q Lazzarus
Western Union Mexico Rate
Trade Chart Dave Richard
Skylar Vox Bra Size
Turning the System On or Off
Local Collector Buying Old Motorcycles Z1 KZ900 KZ 900 KZ1000 Kawasaki - wanted - by dealer - sale - craigslist
Labor Gigs On Craigslist
Dc Gas Login
Craigslist Blackshear Ga
Mbta Commuter Rail Lowell Line Schedule
Honda cb750 cbx z1 Kawasaki kz900 h2 kz 900 Harley Davidson BMW Indian - wanted - by dealer - sale - craigslist
Aspen Mobile Login Help
CVS Near Me | Columbus, NE
Juicy Deal D-Art
Georgetown 10 Day Weather
Rqi.1Stop
Https Paperlesspay Talx Com Boydgaming
The BEST Soft and Chewy Sugar Cookie Recipe
The Tower and Major Arcana Tarot Combinations: What They Mean - Eclectic Witchcraft
Ice Dodo Unblocked 76
Talk To Me Showtimes Near Marcus Valley Grand Cinema
SN100C, An Australia Trademark of Nihon Superior Co., Ltd.. Application Number: 2480607 :: Trademark Elite Trademarks
Low Tide In Twilight Ch 52
§ 855 BGB - Besitzdiener - Gesetze
Wku Lpn To Rn
Cylinder Head Bolt Torque Values
Mawal Gameroom Download
25Cc To Tbsp
Urban Blight Crossword Clue
Human Unitec International Inc (HMNU) Stock Price History Chart & Technical Analysis Graph - TipRanks.com
Beaver Saddle Ark
Ixl Lausd Northwest
Σινεμά - Τι Ταινίες Παίζουν οι Κινηματογράφοι Σήμερα - Πρόγραμμα 2024 | iathens.gr
Tamil Play.com
Pill 44615 Orange
Spinning Gold Showtimes Near Emagine Birch Run
Hannibal Mo Craigslist Pets
Labyrinth enchantment | PoE Wiki
NHL training camps open with Swayman's status with the Bruins among the many questions
Skip The Games Grand Rapids Mi
Joey Gentile Lpsg
Unveiling Gali_gool Leaks: Discoveries And Insights
Brake Pads - The Best Front and Rear Brake Pads for Cars, Trucks & SUVs | AutoZone
Wordle Feb 27 Mashable
Juiced Banned Ad
Join MileSplit to get access to the latest news, films, and events!
sin city jili
Obituaries in Westchester, NY | The Journal News
Latest Posts
Article information

Author: Msgr. Benton Quitzon

Last Updated:

Views: 6068

Rating: 4.2 / 5 (43 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Msgr. Benton Quitzon

Birthday: 2001-08-13

Address: 96487 Kris Cliff, Teresiafurt, WI 95201

Phone: +9418513585781

Job: Senior Designer

Hobby: Calligraphy, Rowing, Vacation, Geocaching, Web surfing, Electronics, Electronics

Introduction: My name is Msgr. Benton Quitzon, I am a comfortable, charming, thankful, happy, adventurous, handsome, precious person who loves writing and wants to share my knowledge and understanding with you.