# How to write a Monte Carlo Simulation?

13 views (last 30 days)
Jenni on 18 Mar 2024
Commented: Jenni on 18 Mar 2024
Hi,
I am having troubles in combining plotting and Monte Carlo Simulation. My assignment is following:
"Lisa runs a resale business for boxes. Boxes for the upcoming sales period are ordered from the factory in advance without knowing the actual demand. The boxes are fashion items and have no use after the sales period: if Lisa orders too many boxes, the excess ones are thrown away. If Lisa orders too few, she loses potential sales.
• Demand D is uniformly distributed in the range [0, 300] units.
• The cost of ordering one box is c = 30 euros and the selling price is p = 120 euros.
Create a Matlab function that returns n observations of sales profit given a certain order quantity q.What is the probability that the sales profit is negative when the order quantity is q = 150?
Here is my code for that:
Matlab-function: (The function to calculate sales profit)
function pi = Profit(p,D,q,c,z)
pi = p*min(D,z*q) - c*z*q;
Code:
clear all
close all
z = 1;
q = 150;
pi = [];
c = 30;
p = 120;
for n= 1:1000;
D = 300 * rand;
pi(end+1)=Profit(p,D,q,c,z) ;
end
length(find(pi<=0))/length(pi)
So, I am having troubles with the second part where I should create a plot of the expected sales profit as a function of the order quantity:
Write a Matlab script that estimates the expected sales profit for order quantities 0, 1, ...300 using the function from step 1.
(HINT: To achieve sufficient accuracy in this task, it is advisable to use a large number of simulations. For example, 10,000 observations for each value of q should be sufficient.) Include in your answers a plot of the expected sales profit as a function of the order quantity."
How should I use the Monte Carlo Simulation to create the plot? Should it be something like this:
z = 1;
c = 30;
p = 120;
pi =[];
for q = 0:300
plot(quantity,pi)
grid on;
title('Monte Carlo simulation'));
xlabel('Quantity (pcs)')
ylabel('Sales profit (€)')
grid on
for n = 1:10000
D = 300 * rand;
pi(end+1)=Profit(p,D,q,c,z);
profit = Profit(p,D,q,c,z);
quantity = mean(profit,'all','double');
end
end
The last for-loop is really blurry for me and I have just tried stuff without succeeding. So, how should I write the code?
Thank you for your help! :)
##### 2 CommentsShow NoneHide None
John D'Errico on 18 Mar 2024
Edited: John D'Errico on 18 Mar 2024
First, save yourself endless amounts of grief in the future. (As well as an anguished question on this forum, where you ask why it is that certain tools in MATLAB now cause errors, or do not have their expected values.)
Do NOT define variables with names like pi. Why is that? When you really want to use the really useful constant named pi, it will no longer be accessible to you. For example...
% As you can see, pi is the number pi. At least, it WAS the number
% 3.14159...
pi
ans = 3.1416
% however, now assign pi to some other number.
pi = 17;
% now, what value does the variable pi cpntain?
pi
pi = 17
% what is the area of a circle, of radius 1?
r = 1;
pi*r^2
ans = 17
So instead, look for good names, but not names that conflict with existing MATLAB function and constant names. You could have use Pi instead, and have caused no conflict.
Jenni on 18 Mar 2024
You have an excellent point there @John D'Errico, thank you for the helpful tip!

Torsten on 18 Mar 2024
Edited: Torsten on 18 Mar 2024
ps = 120;
pb = 30;
Profit = @(q,D) ps*min(q,D)-pb*q;
number_of_trials = 1000000;
expected_profit = zeros(301,1);
for i = 0:300
expected_profit(i+1) = sum(Profit(i,300*rand(number_of_trials,1)))/number_of_trials;
end
plot(0:300,expected_profit)
grid on
##### 3 CommentsShow 1 older commentHide 1 older comment
Torsten on 18 Mar 2024
What does the "z" variable mean in your profit function ?
pi = p*min(D,z*q) - c*z*q;
Jenni on 18 Mar 2024
My apologies for not explaining it earlier but the z means delivered quantity from ordered quantity. In the first two assignments it is not needed so that is why the z value is 1. So, the assignment continues like this: "Lisa has noticed that the factory does not always deliver all the boxes she orders. Extend your model to estimate the expected sales profit at different order quantities when the proportion of delivered boxes out of the oredered quantity Z is uniformly distributed in the range [0, 1]."

### Categories

Find more on Triangular Distribution 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!