Thread Subject: Constrained Random Walk Model

Subject: Constrained Random Walk Model

From: Rich

Date: 29 Oct, 2009 19:40:20

Message: 1 of 2

I have a working (though very ugly) constrained random walk model for a particle. I have two questions regarding this code.
1. I would like to replace the black dot used to illustrate the particle with an image, preferable a .jpg image from the current directory. I'm not sure this a handle graphics option
2. I would like to simultaneously loop this. As it currently is there is only 1 object moving. I would like it to run for more particles at once.

I appreciate any suggestions

% Constrained movement of particles
x = 0; % starting x location
y = 0; % starting y location
t = 0; % starting time
dt = 1; % time step
v = .1; % horizontal velocity
u = .05; % vertical velocity
q = []; % reassignment for x in calculations
r = []; % reassignment for y in calculations
nsteps = 1;

tic

while t <1000
    t = t + dt; % time steps forward at time step value
    x = v * rwalk(nsteps);
    y = u * rwalk(nsteps);
    q = [q x]; % add q to end of x = set up as q
    r = [r y]; % add r to end of y = set up as y
    if q > 5; % if particle is further than 5 units to right
        if randn > .5; % "roll" a random n and if it greater than .5
            q(i) = q - 1.5; % cause particle to "jump" back towards center
        end
    end
    if q > 15;
        q (i) = q -1.5;
    end
    if q < -5; % if particle is further than 5 units to right
        if randn > 0; % "roll" random n and if it greater than 0
            q(i) = q + 1; % cause particle to "jump" one unit to right
        end
    end
    if q < -15;
        q (i) = q + 1.5;
    end
    if r < -4;
        r (i) = r +1.5;
    end
    if r < -3
        if randn >.7;
            r(i) = r + .5;
        end
    end
    if r > 14;
        r (i) = r - 1.5;
    end
    if r > 11;
        if randn > .7;
            r(i) = r - .5;
        end
    end
end

phandle = plot([0],[0],'.k');
axis([-19 19 -19 19])
axis square
axis off
text(-15, -9, 'Bulkhead', 'FontSize', 11, 'Color', 'k', 'FontWeight', 'Bold');
text(-20, -7, '____________________', 'Color', 'k', 'FontWeight', 'Bold');
text(-2, -9, 'Marsh', 'FontSize', 11, 'Color', [.5 1 .2], 'FontWeight', 'Bold');
text(-5, -7, '||||||||||||||||||||||||||||||', 'Color', [.5 1 .2], 'FontWeight', 'Bold');
text(11, -9, 'Riprap', 'FontSize', 11, 'Color', 'r', 'FontWeight', 'Bold');
text(5, -7, 'OoOoOoOoOoOoOoOoOoOo','Color', 'r', 'FontWeight', 'Bold');
text(-18, 17, '~~~~~~~~~~~~~~~~Water Surface ~~~~~~~~~~~~~', 'Fontsize', 10, 'Color', 'b')
text(-18, -5, '---------------------------------Creek Floor--------------------------------', 'FontSize', 10, 'Color', [.6 .3 .5])

for j = 1:nsteps
    set(phandle,'XData',x(:,j),'YData',y(:,j))
    drawnow
end

toc

Subject: Constrained Random Walk Model

From: Ram Lakshmi

Date: 21 Nov, 2009 17:28:01

Message: 2 of 2

"Rich " <rbalousk@udel.edu> wrote in message <hccr34$cvq$1@fred.mathworks.com>...
> I have a working (though very ugly) constrained random walk model for a particle. I have two questions regarding this code.
> 1. I would like to replace the black dot used to illustrate the particle with an image, preferable a .jpg image from the current directory. I'm not sure this a handle graphics option
> 2. I would like to simultaneously loop this. As it currently is there is only 1 object moving. I would like it to run for more particles at once.
>
> I appreciate any suggestions
>
> % Constrained movement of particles
> x = 0; % starting x location
> y = 0; % starting y location
> t = 0; % starting time
> dt = 1; % time step
> v = .1; % horizontal velocity
> u = .05; % vertical velocity
> q = []; % reassignment for x in calculations
> r = []; % reassignment for y in calculations
> nsteps = 1;
>
> tic
>
> while t <1000
> t = t + dt; % time steps forward at time step value
> x = v * rwalk(nsteps);
> y = u * rwalk(nsteps);
> q = [q x]; % add q to end of x = set up as q
> r = [r y]; % add r to end of y = set up as y
> if q > 5; % if particle is further than 5 units to right
> if randn > .5; % "roll" a random n and if it greater than .5
> q(i) = q - 1.5; % cause particle to "jump" back towards center
> end
> end
> if q > 15;
> q (i) = q -1.5;
> end
> if q < -5; % if particle is further than 5 units to right
> if randn > 0; % "roll" random n and if it greater than 0
> q(i) = q + 1; % cause particle to "jump" one unit to right
> end
> end
> if q < -15;
> q (i) = q + 1.5;
> end
> if r < -4;
> r (i) = r +1.5;
> end
> if r < -3
> if randn >.7;
> r(i) = r + .5;
> end
> end
> if r > 14;
> r (i) = r - 1.5;
> end
> if r > 11;
> if randn > .7;
> r(i) = r - .5;
> end
> end
> end
>
> phandle = plot([0],[0],'.k');
> axis([-19 19 -19 19])
> axis square
> axis off
> text(-15, -9, 'Bulkhead', 'FontSize', 11, 'Color', 'k', 'FontWeight', 'Bold');
> text(-20, -7, '____________________', 'Color', 'k', 'FontWeight', 'Bold');
> text(-2, -9, 'Marsh', 'FontSize', 11, 'Color', [.5 1 .2], 'FontWeight', 'Bold');
> text(-5, -7, '||||||||||||||||||||||||||||||', 'Color', [.5 1 .2], 'FontWeight', 'Bold');
> text(11, -9, 'Riprap', 'FontSize', 11, 'Color', 'r', 'FontWeight', 'Bold');
> text(5, -7, 'OoOoOoOoOoOoOoOoOoOo','Color', 'r', 'FontWeight', 'Bold');
> text(-18, 17, '~~~~~~~~~~~~~~~~Water Surface ~~~~~~~~~~~~~', 'Fontsize', 10, 'Color', 'b')
> text(-18, -5, '---------------------------------Creek Floor--------------------------------', 'FontSize', 10, 'Color', [.6 .3 .5])
>
> for j = 1:nsteps
> set(phandle,'XData',x(:,j),'YData',y(:,j))
> drawnow
> end
>
> toc


Hello,

I'm also needing the same input. The moving object i'm looking is human and have that as image file.
Let me know if you have the answer.

thanks
R Laksmi

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
random walk Rich 29 Oct, 2009 15:44:03
rssFeed for this Thread

Contact us at files@mathworks.com