How to write a Monte Carlo Simulation?
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
John D'Errico
2024-3-18
编辑:John D'Errico
2024-3-18
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
% however, now assign pi to some other number.
pi = 17;
% now, what value does the variable pi cpntain?
pi
% what is the area of a circle, of radius 1?
r = 1;
pi*r^2
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.
采纳的回答
Torsten
2024-3-18
编辑:Torsten
2024-3-18
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 个评论
Torsten
2024-3-18
What does the "z" variable mean in your profit function ?
pi = p*min(D,z*q) - c*z*q;
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!