The vector (let’s call it s) we created contains the subsequent steps of size unity that the mouse is taking in the (x, y)-plane. The mouse starts at pos = (0, 0). Create a time step loop from t = 1 until tend = N , and store the mouse positions at e

1 view (last 30 days)
% Demo to do a random walk in 2 dimensions. % User is asked for the number of steps to take. clc; % Clear the command window. clearvars; close all; % Close all figures (except those of imtool.) workspace; % Make sure the workspace panel is showing. fontSize = 10; format shorteng; % Ask user for a number of steps to take. defaultValue = 42; %the answer is always 42 titleBar = 'Enter an integer value'; userPrompt = 'squeak squeak, please enter the steps you want me to take: '; caUserInput = inputdlg(userPrompt, userPrompt, 1, {num2str(defaultValue)}); if isempty(caUserInput),return,end; % lazy mouse, or the user bailed out and clicked Cancel. integerValue = round(str2num(cell2mat(caUserInput))); % Check for a valid integer, cause the program trips if it is a non-integer number. if isnan(integerValue) % They didn't enter a number. % They clicked Cancel, or entered a character, symbols, or something else not allowed. integerValue = defaultValue; message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue); uiwait(warndlg(message)); end numberOfSteps = integerValue; deltax = 0 + (2*pi-0).*rand(numberOfSteps); deltay = 0 + (2*pi-0).*rand(numberOfSteps); xy = zeros(numberOfSteps,2); for step = 2 : numberOfSteps % Walk in the x direction. xy(step, 1) = xy(step, 1) + deltax(step); % Walk in the y direction. xy(step, 2) = xy(step, 2) + deltay(step); % Now plot the walk so far. xCoords = xy(1:step, 1); yCoords = xy(1:step, 2); plot(xCoords, yCoords, 'bo-', 'LineWidth', 2); hold on; textLabel = sprintf('%d', step); text(xCoords(end), yCoords(end), textLabel, 'fontSize', fontSize); end % Mark the first point in red. hold on; plot(xy(1,1), xy(1,2), 'rs', 'LineWidth', 2, 'MarkerSize', 25); textLabel = '1'; text(xy(1,1), xy(1,2), textLabel, 'fontSize', fontSize); grid on; % Mark the last point in red. plot(xCoords(end), yCoords(end), 'rs', 'LineWidth', 2, 'MarkerSize', 25); title('mouse Walk', 'FontSize', fontSize); xlabel('mouse walk on the x axis', 'FontSize', fontSize); ylabel('mouse walk on the y axis', 'FontSize', fontSize); % Enlarge figure to full screen, cause that is fancier. set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Calculate the distance from the origin. distanceFromOrigin = hypot(xCoords(end), yCoords(end)); message = sprintf('Done with my walk!\nDistance of endpoint from origin = %.3f', distanceFromOrigin); msgbox(message); %... (initialisation) visited = zeros(xmax, ymax); %turn to 1 when visited
This is my script, can someone please help me to find where it is in my model and if it is not, give me hint (or answer) how to get it in there
  2 Comments
Dirk van Meer
Dirk van Meer on 20 Nov 2017
% Demo to do a random walk in 2 dimensions.
% User is asked for the number of steps to take.
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 10;
format shorteng;
% Ask user for a number of steps to take.
defaultValue = 42;
%the answer is always 42
titleBar = 'Enter an integer value';
userPrompt = 'squeak squeak, please enter the steps you want me to take: ';
caUserInput = inputdlg(userPrompt, userPrompt, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % lazy mouse, or the user bailed out and clicked Cancel.
integerValue = round(str2num(cell2mat(caUserInput)));
% Check for a valid integer, cause the program trips if it is a non-integer number.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
numberOfSteps = integerValue;
deltax = 0 + (2*pi-0).*rand(numberOfSteps);
deltay = 0 + (2*pi-0).*rand(numberOfSteps);
xy = zeros(numberOfSteps,2);
for step = 2 : numberOfSteps
% Walk in the x direction.
xy(step, 1) = xy(step, 1) + deltax(step);
% Walk in the y direction.
xy(step, 2) = xy(step, 2) + deltay(step);
% Now plot the walk so far.
xCoords = xy(1:step, 1);
yCoords = xy(1:step, 2);
plot(xCoords, yCoords, 'bo-', 'LineWidth', 2);
hold on;
textLabel = sprintf('%d', step);
text(xCoords(end), yCoords(end), textLabel, 'fontSize', fontSize);
end
% Mark the first point in red.
hold on;
plot(xy(1,1), xy(1,2), 'rs', 'LineWidth', 2, 'MarkerSize', 25);
textLabel = '1';
text(xy(1,1), xy(1,2), textLabel, 'fontSize', fontSize);
grid on;
% Mark the last point in red.
plot(xCoords(end), yCoords(end), 'rs', 'LineWidth', 2, 'MarkerSize', 25);
title('mouse Walk', 'FontSize', fontSize);
xlabel('mouse walk on the x axis', 'FontSize', fontSize);
ylabel('mouse walk on the y axis', 'FontSize', fontSize);
% Enlarge figure to full screen, cause that is fancier.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Calculate the distance from the origin.
distanceFromOrigin = hypot(xCoords(end), yCoords(end));
message = sprintf('Done with my walk!\nDistance of endpoint from origin = %.3f', distanceFromOrigin);
end

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 20 Nov 2017
xy has your coordinates at each step. If you want it in a variable called "e" for some reason, simply assign it after the loop:
e = xy;

Categories

Find more on Encryption / Cryptography in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!