How can I simulate a hiker who goes in a different direction every time?

2 views (last 30 days)
Desmond Anthony
Desmond Anthony on 23 Nov 2021
Edited: Image Analyst on 24 Nov 2021
Hey guys,
I must write a MATLAB program where a hiker walks in different direction every time. The directions are (N,NW,W,SW,S,SE,E,NE) and the length of his steps are 1 for (N,W,S,E) and sqrt(2) for the others. I also must also draw the path in 3D. My problem is that I do not know how I can use the length. In my code the hiker walks in the same radius but actually he must keep walking.
I hope you can help me.
clear;close all;clc;
N=1;NW=2;W=3;SW=4;S1=5;SO=6;O=7;NO=8;
i=10;
xx=0;yy=0;
x=0;y=0;
for k=1:i
S = randi(8);
if S==N||S==W||S==SO||S==O
n=1;
else
n=sqrt(2);
end
if S==N
y=n;x=0;
elseif S==NW
x=-n;y=n;
elseif S==W
x=-n;y=0;
elseif S==SW
x=-n;y=-n;
elseif S==S1
x=-n;y=-n;
elseif S==SO
x=n;y=-n;
elseif S==O
x=n;y=0;
else
x=n;y=n;
end
xx(k)=x;
yy(k)=y;
end
t=linspace(0,120,i);
plot3(xx,yy,t);grid on;
ylim([-5,5]);
xlim([-5,5]);

Accepted Answer

Benjamin
Benjamin on 23 Nov 2021
The idea is that you have to add the new x and y steps to the previous position each time through the loop.
Change your for line to this:
for k=2:i
And change your xx(k) and yy(k) assignment lines to this:
xx(k)=xx(k-1)+x;
yy(k)=yy(k-1)+y;
This way the first position is at the origin (0,0) and each step is added to the previous position to create a path.

More Answers (1)

Image Analyst
Image Analyst on 24 Nov 2021
Edited: Image Analyst on 24 Nov 2021
You don't need to define the directions like that. All you need to do is to get a deltax of +/- 1, and same for y, and for z if you want to move in 3-D. You can use randi([-1, 1], 1) for that.
For what it's worth, I'm also attaching several of my random walk demos that do different things.
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
numSteps = 30;
x = zeros(1, numSteps)
y = zeros(1, numSteps);
z = zeros(1, numSteps);
% Label starting point.
text(x(1), y(1), '1', 'Color', 'r', 'FontSize', fontSize)
hold on;
for k = 2 : numSteps
% Get delta x, y, and z.
deltax = randi([-1, 1], 1);
deltay = randi([-1, 1], 1);
deltaz = randi([-1, 1], 1);
% Add this onto the last point to get a new point location.
x(k) = x(k-1) + deltax;
y(k) = y(k-1) + deltay;
z(k) = z(k-1) + deltaz;
% Plot it.
plot3(x(1:k), y(1:k), z(1:k), 'b.-', 'MarkerSize', 20, 'LineWidth', 2);
text(x(k), y(k), z(k), [' ', num2str(k)], 'Color', 'r', 'FontSize', fontSize)
grid on;
drawnow;
end
% Compute the distances from the origin.
distances = sqrt(x .^ 2 + y .^ 2)
maxDistance = max(distances)
xlim([-maxDistance, maxDistance]);
ylim([-maxDistance, maxDistance]);
zlim([-maxDistance, maxDistance]);
view(45, 45)
xlabel('x', 'FontSize',fontSize);
ylabel('y', 'FontSize',fontSize);
zlabel('z', 'FontSize',fontSize);
What you have to do (to modify the code) is to check the delta's and make sure they are not the same as the delta's from the prior step. If they are all 3 the same, then he's walking in the same direction as last time. Since that's not allowed you'd have to get a new set of delta's and check again.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by