Probability and If commands.

  • MATLAB
  • Thread starter Beer-monster
  • Start date
  • Tags
    Probability
In summary, the conversation discusses the development of a MATLAB script for a 2D Ising model using a Monte Carlo method. The method involves randomly switching values in a binary array and the question arises about how to introduce a probability for these switches. The solution suggested is to generate a random number and check if it is smaller than the desired probability, and then flip the value accordingly.
  • #1
296
0
Hi

I'm working on my first MATLAB script that I've worked on from scratch. I've never taken a course in matlab, the only experience I've had is to modify a couple of other scripts. I'm hoping to develop my matkab skills with a 'relatively' simple monte carlo model: a 2D Ising model.

One part of my method is to take a binary array of zeros and ones, pick a 'random' element and switch the value from zero to one or vice versa. I've done this with a simple if/else command.

if I==1
I=0;
else I=1;
end


What I'd like to do is introduce a probability, so that the value flip will occur with a certain probability p. Is there anyway to add this into my if command or do I need tp try something more sophisticated?
 
Physics news on Phys.org
  • #2
You can generate a random number between 0 and 1 or between 0 and N (whatever Matlab can do easily) and then check if this random number is smaller than your target probability (or N times that probability). Flip if yes.
Code:
if rand<p
  I=1-I;
end
 
  • Like
Likes Wrichik Basu

1. What is probability?

Probability is a branch of mathematics that deals with the likelihood or chance of an event occurring. It is often expressed as a number between 0 and 1, where 0 indicates impossibility and 1 indicates certainty.

2. How is probability calculated?

The probability of an event is calculated by dividing the number of favorable outcomes by the total number of possible outcomes. This can be represented as a fraction, decimal, or percentage.

3. What is an "if" command in probability?

An "if" command in probability is a conditional statement that is used to represent the probability of an event occurring given that another event has already occurred. It is often expressed as "if A, then B" or "A implies B".

4. What is the difference between theoretical and experimental probability?

Theoretical probability is based on mathematical calculations and assumes that all outcomes are equally likely. On the other hand, experimental probability is determined through actual experiments or observations and may vary from the theoretical probability due to chance or other factors.

5. How can probability be used in real life?

Probability is used in many real-life situations, such as predicting the outcome of a sports game, determining the likelihood of winning a lottery, or assessing risk in insurance and financial industries. It can also be used to make decisions by considering the probability of different outcomes.

Suggested for: Probability and If commands.

Back
Top