# Monte Carlo simulation to find weight and statistics calculations

6 vues (au cours des 30 derniers jours)
Petch Anuwutthinawin le 3 Juil 2021
Modifié(e) : dpb le 3 Juil 2021
I need to make a monte carlo simulation for a problem of weight.
The weight of one shipping pallete is 15 boxes, and each box contains 260 X pieces and 140 Y pieces. The weights of x are distributed normally with mean of 1.2 and 0.18 standard deviation. The weights of y are distributed uniformly between 1.1 and 1.5. The max weight of each pallete is 7430.
I need to make a monte carlo simulation to find the convergence for the chance of the pallete being max weight or overweight. I also need to find the mean, standard deviation.
I have written code for this sampled from another simulation I wrote.
N = 100 ;
Box = zeros(N,1);
Relativefreq=zeros(N,1);
count=0 ;
for i=1:100
X =(260*(1.2+0.18*randn(1,10))) %weight of X already multiplied by number of parts
Y =(140*(1.1+(1.5-1.1)*rand(10,1)))' %weight of Y multiplied by number of parts
Pallete(i)=15*X+15*Y; %should calculate weight of each box, multiplied by 15 per pallete. Keeps getting error that it is not the same number of elements.
if Pallete(i)>=7430; %test to see if the box is overweight
count = count + 1 ;
end
Relativefreq(i) = count/i ; %should be relative frequency of the box being overweight
end
figure
[x,c]=hist(Box,ceil(2*length(i))^1/3); %makes histogram data with the number of bins according to the Reiss rule.
h=gca;
set(h,'XTick',c);
x/sum(x);
bar(c,x/sum(x)); %should print the histogram for the relative frequency of the weights.
xlabel('Weight Of Palletes'), ylabel('Relative Frequency');
figure;
plot(1:N,Relativefreq),grid %should make a convergence diagram to find the probability it is at max or overweight.
axis tight;
xlabel('Number of trials'),ylabel('Prob that its overweight');
mean=mean(Pallete(i)) %should calc mean weight of each pallete, dosent work.
s=std(Pallete(i)) %should calc the standard deviation of each pallete weight
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

dpb le 3 Juil 2021
Modifié(e) : dpb le 3 Juil 2021
m=mean(Pallete(i)) %should calc mean weight of each pallete, dosent work.
s=std(Pallete(i)) %should calc the standard deviation of each pallete weight
remove the subscript (i); you want the full array here.
NB: Do NOT redefine the builtin function mean() by using mean as a variable name.
Reading the code a little more thoroughly, I think
X =(260*(1.2+0.18*randn(1,10))) %weight of X already multiplied by number of parts
Y =(140*(1.1+(1.5-1.1)*rand(10,1)))' %weight of Y multiplied by number of parts
Pallete(i)=15*X+15*Y; %should calculate weight of each box, multiplied by 15 per pallete. Keeps getting error that it is not the same number of elements.
isn't what you want, though.
If I read the problem description correctly, it is the distribution for each part that is given; you're only generating 10 random parts but then multiplying that by the number of parts of each type per box. The box weights would be
WtX=sum(1.2+0.18*randn(260,1)); % weight of X parts in box
WtY=sum(1.1+(1.5-1.1)*rand(140,1)); % weight of Y parts
WtBox=WtX+WtY; % total box weight
Then your simulation needs to create 15 of these boxes for each pallet if you want to keep statistics on the distribution of box weights in addition to the overall pallet weight.
Or, if you only care about pallets, then you could increase the number of samples by 15X and then the total weight would be the estimate for the pallet for each realization.
WtX=sum(1.2+0.18*randn(15*260,1)); % weight of X parts in 15 boxes
WtY=sum(1.1+(1.5-1.1)*rand(15*140,1)); % weight of Y parts in 15 boxes
WtPallet=WtX+WtY; % total pallet weight
It would be far better coding practice to have all the "magic" constants defined as variables at the beginning of the script/function. Then, one could change any parameter at will without having to rewrite the code to handle it.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Plus de réponses (1)

Image Analyst le 3 Juil 2021
You should format your code properly. In MATLAB, type control-a (to select all) followed by control-i (to fix indenting). then paste back here and highlight the code and click the code icon on the tool ribbon.
You have two problems.
1. One is naming your variable "mean", which destroys the built-in function mean (at least for this run of your program). In general it's a very bad idea to call your variables the same names as keywords or built-in functions (even though MATLAB lets you -- DON'T DO IT).
2. Secondly you're taking the mean and stdev of just a single element, not a whole vector.
To fix
theMean = mean(Pallete)
theStdDev = std(Pallete)
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Image Arithmetic dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by