How to test the MATLAB code in Monte carlo simulation?

I have written the code for UAV travelling along the trajectory including it avoids the obstacle along the travelling path.I have executed the MATLAB code it runs properly. Now, how to test these codes in Monte carlo simulation? I don't know my question is right or not.I don't know about Monte carlo simulation.

 Accepted Answer

Monte Carlo means getting random values to use in your process, and then doing it a bunch of times (a bunch of experiments) to see all the different outcomes that you may get. Each run (experiment) will give a different outcome because random values were used in the process. For example you may get the mean outcome or find the max and min outcomes, or get a distribution of outcomes.
See attached Monte Carlo and random walk demos that I've written. Adapt as needed.

8 Comments

I can't get the concept of Monte carlo simulation.How to get those random values. Actually we are using for loop having more than 30000 iterations.Is there any separate code have to be written for implementing monte carlo simulation?
Just write code for one run/experiment. You can use rand() or randi() or randn() to generate random numbers for your experimental parameters. Then put it inside a loop where the parameters for your experiment are determined randomly so that your code is executed for multiple (30000) runs of your experiment. Like
for k = 1 : 30000
inputParameter = rand(1); % Or however many random numbers you need.
% Now do your experiment with the code for one experiment
% using inputParameter.
end
Thank you for your explanation.I got it.
Apologize for asking this type of questions.rand() is for generating random numbers.Is monte carlo simulation is generating only random numbers?if not,anyother toolbox is available?
Monte Carlo generates and USES random numbers as part of the simulation, so it's more than just a random number program. It's really more of a simulation program that uses randomness as part of the whole experimental scenario. For example let's say that you wanted to get a distribution of the number of people in line at your bank as a function of time of day. You could use a random number generator to come up with random times that people enter your bank office, and random lengths of time for the teller to service each customer. But then the program still needs additional coding to handle all that, including to build lines, chart line lengths, save results to workbook, etc.
  1. if rng(0) is used in some code which uses rand then is there any use of montecarlo simulations?
  2. montecarlo simulations are done to average the different outputs as rand is used in code i think.
  3. Also i have another doubt.. do we get best output when rng(0) is used?? if not used then for different runs different outputs but the best output always i observed when rng(0) is used.
  1. Yes, of course. just because you seed the random number generator with some number does not mean that the simulation is no good. Let's say you called rng(0) and then got a million numbers and used them to do your simulation. That simulation is fine. It's usable. See this demo:
r1 = rand(1, 3)
r2 = rand(1, 3)
rng(0)
r3 = rand(1, 3)
rng(0)
r4 = rand(1, 3)
r1 =
0.91338 0.63236 0.09754
r2 =
0.2785 0.54688 0.95751
r3 =
0.81472 0.90579 0.12699
r4 =
0.81472 0.90579 0.12699
Note that sets r3 and r4 are the same set of numbers because I called rng(0) before generating each one, while r1 and r2 have different numbers because I didn't call rng(0). But either r4 or r3 are perfectly valid sets of numbers to use in your simulation.
2. The Monte Carlo simulation generates a bunch of results - one for each random number. So you'll have a distribution of results. You could histogram those results to see that distribution, or you could take the mean of the results if you just wanted the mean.
3. You don't get any better or worse results by using rng(0). It simply gives you a repeatable set of numbers. So if you got a million numbers in one run (a million experiments), and then wanted to repeat that, then if you used rng(0) before the first million runs, and before the second million runs, then both sets of million runs would give the same distribution and mean because both sets of a million random numbers are identical. If you did not use rng(0), then the distributions and means from the second run of a million experiments would be similar to but not exactly the same as the first run because the random numbers would be different.
I see no reason why using rng(0) before a run would give you better results than not using it, as you say. If you did a small number of runs, like say 3, then you have 3 results and it's possible that you can rank them from "closest" to "farthest away" from some theoretical result, and it's possible that the one with rng(0) is closest but it would just be a coincidence. I mean, out of some number of runs, one of them is always closest, isn't it? Of course. It may just be a conincidence that using rng(0) makes that run closest to your theoretical result. If you did many more runs without using rng(0), then it's likely some different run would be closer, like the 10th one or 2000th run or whatever.
One of the main purposes behind rng is repeatability. If you initialize the pseudorandom number generator to a known state using rng before running a simulation, if something "interesting" happens during that simulation you can set the generate back to that known state and rerun the simulation until the "interesting" thing happens.
While 0 is probably the most common "known state" to use in initializing the pseudorandom number generator, you could choose another state.
figure
rng(0)
plot(rand(1, 10))
title('0')
figure
rng(42)
plot(rand(1, 10))
title('42')
figure
rng(2020)
plot(rand(1, 10))
title('2020')
figure
rng(42)
plot(rand(1, 10))
title('42 again')

Sign in to comment.

More Answers (1)

Hello, I am trying to write a Monte Carlo simulation to create damages in a bridge and its really difficult. Please could you provide some assistance.

1 Comment

Sure. We'll try, if you give us something to work with and not be so vague. Please read this link and then write back with a new question since your "Answer" is definitely not an answer to Dhana's question on Monte Carlo simulation.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!