Trying to do simple Monte Carlo simulation

Hey, so I'm trying to do some a simple monte carlo simulation for some tolerances.
Essentially, I have lengths and their tolerances:
5 +- .2 in
6 +- .3 in
7 +- .4 in
I am trying to do a normal distribution of these 3. So my current code is:
n = 100000
x1 = ( randn(n,1) * 3 ) + 5;
x2 = ( randn(n,1) * 3 ) + 6;
x3 = ( randn(n,1) * 3 ) + 7;
y = sqrt(x1.^2+x2.^2+x3.^2)
y_mean = mean(y)
y_std = std(y)
y_median = median(y)
My issue is that how do I take into account the tolerances into the x1, x2, x3 functions? There is a place for the standard deviation, which is 3, and a place for the mean, but I am unsure how to put in the tolerances / do an analysis of the tolerances.
Any help would be greatly appreciated. Thank you.

12 Comments

To further clarify, I believe I'm able to do it assuming everything is uniformly distributed.
% Generate n samples from a uniform distribution
% r = a + rand(n,1) * (b-a)
% a : minimum
% b : maximum
Since I can make the minimum like -.3 and the max + .3 for example. My issue is that the mean of these is 0...so I don't know how to program it making it normally distributed.
% Generate n samples from a normal distribution
% r = ( randn(n,1) * sd ) + mu
% mu : mean
% sd : standard deviation
The question you need to ask yourself is what distribution you want on the sampled values. E.g., are those hard tolerances and you want the samples to be "truncated normal" distributed inside those tolerances? Where does that sd of 3 come from? Etc. Once you decide this, then we can help you get the sampling you want.
I'm not 100% sure what you mean by "hard" tolerances. From my understanding, 3 standard deviations of the tolerances means that around 97% of the time, the tolerance will fall within the -.3 to .3 range, but 3% of the time it won't. So, I don't think they are 'hard' tolerances since it would be possible for them to go out of bounds.
Really only the tolerances matter, I don't think the lengths really matter at all. My goal is to see whether or not the tolerances will fit within 0.5 in. And, I have to do it using a normal distribution method.
So, really I believe I need to generate random numbers between -0.3 and 0.3 with a standard deviation of 3. The 3 standard deviations comes from my assignment, so that is standard.
I believe if everything was uniformly distributed, my code would look like this:
C = .2 + rand(n,1) * (-.2-.2) %this three functions are what I need help with
B = .3 + rand(n,1) * (-.3-.3)
A = .4 + rand(n,1) * (-.4-.4)
y = sqrt(A.^2 + B.^2 + C.^2) %transfer function so I don't need help with this
In my assignment, I am not to use a uniform distribution, I am to use a normal distribution. So I need help changing those to using the normal distribution function using the given standard deviation.
I also would need to count how many times it goes above or below the 0.5 in mark, but I was wanting to focus on getting it to work first.
Also, thank you for your time and helping me today.
Can you post the exact wording of your assignment so that we can be clear what your instructor wants?
What is the definition of "gap"? Is it the RSS of the individual deviations as you seem to be doing? Or sum(individual deviations)? Or ...? Is there anything in the assignment wording that specifically defines this?
Gap is just the total tolerance amount given by the equation sqrt(tolerance A^2 + tolerance B^2, tolerance C^2); I believe it is the RSS of the individual deviations.
Since the tolerance can be between -.2 to.2, -3. to .3, -.4 to .4, the tolerance could be in between any of those numbers. The "worst case" it would be .2 + .3 + .4 = .9, which is greater than .5 and would fail. The point of the assignment is to do monte carlo in 2 different statistical ways, uniform distribution and normal distribution. Uniform I believe I've done in my previous code above. I'm trying to figure out how to do normal.
See my updated response below.
Hey, one last dumb question if you'll humor me. What you gave me is perfect, the only thing is that I need to generate a whole bunch of random numbers, (without using a loop), how would I do that using the statement you gave me?
n = number of samples
Then use rand(n,1) or randn(n,1) in the statements instead of rand and randn.
Oh yeah, I was doing rand(n,1) but then it was only producing positive values. randn(n,1) is correct and works. thanks so much once again!
actually iam doing project on tolerance analysis between the shafts in a gear box. So can i have the whole code
What are you asking to have the whole code for?

Sign in to comment.

 Accepted Answer

Based on the wording of the assignment:
To generate a normal distributed sample from the "5 +- .2 in":
r = randn*(0.2/3) + 5;
That is, 0.2 is 3 sd, so you use 0.2/3 as the sd of the sample, and then you add the mean of 5.
The uniform one you already know,
r = rand*(0.2*2) + (5 - 0.2);
Note that the target sample mean values (5 or 6 or 7) really don't play a part in your final answer, since you will be subtracting these values out of the sample vectors before you take the sqrt(etc) of the tolerance error results.

More Answers (1)

If the point is that you need to generate values that are within that range, then switch to using the facilities of the Statistics toolbox, and see https://www.mathworks.com/help/stats/prob.normaldistribution.truncate.html
However, my take would be that you should instead be generating the values the way you are, and then testing, for example,
mask1 = x1 >= 5-0.2 & x1 <= 5+0.2;
If you wanted to know the fraction, then that would be mean(mask1)

3 Comments

Sorry, but I'm kind of confused about this answer. I do need to generate random values between -.3 and 0.3 for example, but I believe they can't just be uniformly random values between them, they have to be normal values between them generated around a mean with a standard deviation. You can see my comment above for further clarification of what I'm trying to do.
Thank for your time and help today
You generate the values with randn() like you did. Masking just allows you to count the number of values that are in the range, like values in the range 5 +/- 0.2. You can use sum() to get the absolute count, or use mean() to get the fraction (=count/ total # of elements).
num_rejections = sum(x1 < 5-0.25 | x1 > 5+0.25);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!