A drunk man walking simulation A drunk man stands at origin. There is a cliff at...

70.2K

Verified Solution

Question

Mechanical Engineering

A drunk man walking simulation
A drunk man stands at origin. There is a cliff at x=10 and home atx=-10. He takes steps randomly left and right. Each step has theprobability p of going left and q=1−p of going right.
We desire to simulate and plot 100 random trajectories for theinterval of 100 time instants. Once hit at home or cliff, atrajectory is stopped immediately and success or dead is declared.Otherwise, alive is declared. Obtain histograms of [success, dead,alive] for p=0.25, 0.5, and 0.75.

alive = 0
success = 0
dead = 0
p = 0.5
for (n in 1:1000)
{
i = 0
s = 0
while (s < 10 && s > -10 && i < 100)
{
x = rbinom(1, 1, p)
if(x == 1)
s = s-1
else
s = s+1
i = i+1
}
if(s == 0)
alive = alive +1
if(s == 10)
success = success + 1
if(s == -10)
dead = dead + 1
}
barplot(c(success, dead, alive))

this code not working

i need histogram and trajectories and code of mathlab

Answer & Explanation Solved by verified expert
4.4 Ratings (921 Votes)
Note The plotsgenerated by you maybe different as each time the    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students