Question

In: Mechanical Engineering

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

A drunk man walking simulation
A drunk man stands at origin. There is a cliff at x=10 and home at x=-10. He takes steps randomly left and right. Each step has the probability p of going left and q=1−p of going right.
We desire to simulate and plot 100 random trajectories for the interval of 100 time instants. Once hit at home or cliff, a trajectory 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

Solutions

Expert Solution

Note: The plots generated by you maybe different as each time the code is run, different values of probability are calculated at random.

The required code is given below.

    clear;
clc;


p = [0.25 0.5 0.75];
figN = 1;
for k = 1:numel(p)
    alive = 0;
    success = 0;
    dead = 0;
    for n = 1:100
        figure(figN)
        my_title = sprintf('p = %.2f\n',p(k));
        title(my_title)
        hold on
        i = 0;
        s = 0;
        y = 0;
        while (s<10 && s>-10 && i<100)
            x = binornd(1,p(k));

                if x == 1
                    sNew = s - 1;
                    plot([s sNew],[y y+1],'k-')
                    s = sNew;
                else
                    sNew = s + 1;
                    plot([s sNew],[y y+1],'k-')
                    s = sNew;
                end
                i = i + 1;
                y = y + 1;
        end

        if s == 0
            alive = alive + 1;
        elseif s == 10
            success = success + 1;
        elseif s == -10
            dead = dead + 1;
        end
    end
    figure(figN+1)
    my_title = sprintf('p = %.2f\n',p(k));
    labels = {'Success', 'Dead', 'Alive'};
    bar([success, dead, alive])
    title(my_title)
    set(gca,'xticklabel',labels)
    figN = figN+2;
end



Related Solutions

A woman stands at the edge of a cliff and throws a rock horizontally over the...
A woman stands at the edge of a cliff and throws a rock horizontally over the edge with a speed of v0 = 16.0 m/s. The rock leaves her hand at a height of h = 33.0 m above level ground at the bottom of the cliff, as shown in the figure. Note the coordinate system in the figure, where the origin is at the bottom of the cliff, directly below where the rock leaves the hand. Write the equations...
A student stands on the edge of a cliff and throws a stone horizantally over the...
A student stands on the edge of a cliff and throws a stone horizantally over the edge with a speeed "v1". The cliff is "h" meters high. Given [h, v1] a. The time for the stone to hit the ground b. The horizantal distance the stone traveled while in the air. c. The magnitude and direction of the stone's velocity just before hitting the ground
A student stands at the edge of a cliff and throws a stone horizontally over the...
A student stands at the edge of a cliff and throws a stone horizontally over the edge with a speed of vi = 10.0 m/s. The cliff is h = 41.9 m above a body of water as shown in the figure below. With what speed and angle of impact does the stone land?
A student stands at the edge of a cliff and throws a stone horizontally over the...
A student stands at the edge of a cliff and throws a stone horizontally over the edge with a speed of 10.0 m/s. the cliff is 50.0 m above a flat horizontal beach. (a) how long after being released does the stone strike the beach before the cliff? (b) find the speed just before the stone strikes the beach. (c) find the traveling horizontal distance for the stone
A physics student stands on a cliff overlooking a lake and decides to throw a golf...
A physics student stands on a cliff overlooking a lake and decides to throw a golf ball to her friends in the water below. She throws the golf ball with a velocity of 18.5 m/s at an angle of 37.5∘ above the horizontal. When the golf ball leaves her hand, it is 10.5 mabove the water. How far does the golf ball travel horizontally before it hits the water? Neglect any effects of air resistance when calculating the answer.
A mountain climber stands at the top of a 41.5-m cliff that overhangs a calm pool...
A mountain climber stands at the top of a 41.5-m cliff that overhangs a calm pool of water. She throws two stones vertically downward 1.00 s apart and observes that they cause a single splash. The first stone had an initial velocity of -1.40 m/s. (Indicate the direction with the sign of your answer.) (a) How long after release of the first stone did the two stones hit the water? (Round your answer to at least two decimal places.) s...
A major League pitcher stands on top of a 100m cliff. He throws a baseball straight...
A major League pitcher stands on top of a 100m cliff. He throws a baseball straight upwards with a speed of 30.5 m/s. ignore air friction, ignore the height of the pitcher. Q: What is the maximum height above the ground the ball rises to? Q: What is the path length of the ball for the entire trip from hand? Q: What is the average velocity of the ball for the entire trip from hand to the ground below just...
If a man is walking 3.5 mph on a treadmill, how steep should the grade be...
If a man is walking 3.5 mph on a treadmill, how steep should the grade be to elicit a VO2 of 34.7 mL.kg-1.min-1?
A man stands at the center of a platform that rotates without friction with an angular...
A man stands at the center of a platform that rotates without friction with an angular speed of 2.46 rev/s. His arms are outstretched, and he holds a heavy weight in each hand. The moment of inertia of the man, the extended weights, and the platform is 14 kg*m^2. When the man pulls the weights inward toward his body, the moment of inertia decreases to 5.1 kg*m^2. a) What is the resulting angular speed of the platform? b) What is...
A man stands on the roof of a building of height 15.7 m and throws a...
A man stands on the roof of a building of height 15.7 m and throws a rock with a velocity of magnitude 30.1 m/s at an angle of 25.0 ? above the horizontal. You can ignore air resistance. A) Calculate the maximum height above the roof reached by the rock B)Calculate the magnitude of the velocity of the rock just before it strikes the ground C) Calculate the horizontal distance from the base of the building to the point where...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT