In: Electrical Engineering
MATLAB Question
Simulate 10,000 trading interactions of 100 people trading between each other. Interactions are random and between 2 randomly chosen people. Each person starts with $100. In each interaction one person makes $1 and the other loses $1 with 50% chance. Find the distribution of wealth (money per person) after 10,000 trading interactions.
Hello,
Please find
the answer attached as under. Please give a thumbs up
rating if you find the answer useful! Have a rocking day
ahead!
************ Matlab Coding ***********
%% 100 people with 100 dollars each
money = 100*ones(1,100);
%% generating random pair of people for trading
n = 10000;
person1 = randi([1 100],1,n);
person2 = randi([1 100],1,n);
%% picking 1 person to win, at random, between person 1 and person
2
win = randi([1 2],1,n);
%% playing the game
for i=1:n
p1 = person1(i);
p2 = person2(i);
if(win(i)==1)
money(p1) = money(p1) +
1;
money(p2) = money(p2) -
1;
else
money(p1) = money(p1) -
1;
money(p2) = money(p2) +
1;
end
end
%% plotting the distribution
histogram(money);xlabel('Money');ylabel('Frequency');
title('Distribution of money after 10,000 tradings')
********** Output ************
*****************************************************************
PS: Please do not forget the thumbs
up!