Filter löschen
Filter löschen

Simulating a 1D Random Walk

11 Ansichten (letzte 30 Tage)
William
William am 6 Dez. 2023
Kommentiert: William am 11 Dez. 2023
This is my code:
% Function to simulate 1D random walk
function trajectory = simulateRandomWalk(N)
% Initialize position
position
= 0;
% Initialize trajectory array
trajectory
= zeros(1, N+1);
trajectory(1) = position;
% Simulate random walk
for step = 1:N
% Generate a random number to decide left or right movement
move
= randi([0, 1])*2 - 1; % -1 for left, 1 for right
% Update position
position
= position + move;
% Store the current position in the trajectory array
trajectory(step+1) = position;
end
end
% Step 2: Plot the entire trajectory for a single random walk
N
= 100; % Number of steps
single_trajectory
= simulateRandomWalk(N);
figure
;
plot(0:N, single_trajectory, '-o');
title('1D Random Walk - Single Trajectory');
xlabel('Steps');
ylabel('Position');
% Step 3: Repeat the process for different values of P and create histograms
P_values
= [50, 500, 5000, 50000, 500000];
for p_index = 1:length(P_values)
P
= P_values(p_index);
final_positions
= zeros(1, P);
for i = 1:P
single_trajectory
= simulateRandomWalk(N);
final_positions(i) = single_trajectory(end);
end
% Plot histogram of final positions
figure
;
histogram(final_positions, 'Normalization', 'probability');
title(['Histogram for P = ' num2str(P)]);
xlabel('Final Position');
ylabel('Probability');
end
Its giving me an error on N.
  1 Kommentar
Image Analyst
Image Analyst am 6 Dez. 2023
This is not valid MATLAB code. You cannot have a script that starts with a function. It will, and does, throw an error. Your function needs to be at the END of your script. Or else don't have it be a function. Also, don't put the right hand side of the = sign be on the line below the line that takes the assignment.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Torsten
Torsten am 6 Dez. 2023
Bearbeitet: Torsten am 6 Dez. 2023
% Step 2: Plot the entire trajectory for a single random walk
N = 100; % Number of steps
single_trajectory = simulateRandomWalk(N);
figure
plot(0:N, single_trajectory, '-o')
title('1D Random Walk - Single Trajectory')
xlabel('Steps')
ylabel('Position')
% Step 3: Repeat the process for different values of P and create histograms
P_values = [50, 500, 5000, 50000, 500000];
for p_index = 1:length(P_values)
P = P_values(p_index);
final_positions = zeros(1, P);
for i = 1:P
single_trajectory = simulateRandomWalk(N);
final_positions(i) = single_trajectory(end);
end
% Plot histogram of final positions
figure
histogram(final_positions, 'Normalization', 'probability')
title(['Histogram for P = ' num2str(P)])
xlabel('Final Position')
ylabel('Probability')
end
% Function to simulate 1D random walk
function trajectory = simulateRandomWalk(N)
% Initialize position
position = 0;
% Initialize trajectory array
trajectory = zeros(1, N+1);
trajectory(1) = position;
% Simulate random walk
for step = 1:N
% Generate a random number to decide left or right movement
move = randi([0, 1])*2 - 1; % -1 for left, 1 for right
% Update position
position = position + move;
% Store the current position in the trajectory array
trajectory(step+1) = position;
end
end
  2 Kommentare
William
William am 6 Dez. 2023
Thank you so much!!!
William
William am 11 Dez. 2023

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by