Monte Carlo simulation to find weight and statistics calculations
Show older comments
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
Accepted Answer
More Answers (1)
Image Analyst
on 3 Jul 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.
- 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).
- 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)
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!