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 件のコメント
Matthew Jerome
2018 年 6 月 8 日
James Tursa
2018 年 6 月 8 日
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.
Matthew Jerome
2018 年 6 月 8 日
James Tursa
2018 年 6 月 8 日
Can you post the exact wording of your assignment so that we can be clear what your instructor wants?
James Tursa
2018 年 6 月 8 日
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?
Matthew Jerome
2018 年 6 月 8 日
James Tursa
2018 年 6 月 8 日
See my updated response below.
Matthew Jerome
2018 年 6 月 8 日
James Tursa
2018 年 6 月 8 日
n = number of samples
Then use rand(n,1) or randn(n,1) in the statements instead of rand and randn.
Matthew Jerome
2018 年 6 月 8 日
Pranav Akshay
2023 年 4 月 27 日
actually iam doing project on tolerance analysis between the shafts in a gear box. So can i have the whole code
Walter Roberson
2023 年 4 月 27 日
What are you asking to have the whole code for?
採用された回答
その他の回答 (1 件)
Walter Roberson
2018 年 6 月 8 日
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 件のコメント
Matthew Jerome
2018 年 6 月 8 日
Image Analyst
2018 年 6 月 8 日
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).
Walter Roberson
2018 年 6 月 8 日
num_rejections = sum(x1 < 5-0.25 | x1 > 5+0.25);
カテゴリ
ヘルプ センター および File Exchange で Uniform Distribution (Continuous) についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!