How to Subplot in a for loop
Mostrar comentarios más antiguos
I need help with making a subplot for the two histograms this code creates
%% Setting Initial Conditions
clc %Clearing the command window.
clear all %Making output nicer.
%0 is a step to the left and 1 is a step to the right.
Left = 0; %Setting the initial left value.
Right = 0; %Setting the initial right value.
%% Creating the Ensemble of Random Walkers
for N = [25000, 100000] %Creating a vector for all the ensemble sizes.
for x = 1:N %Creating an ensemble of N walkers.
for i = 2:2501 %Setting the for loop to run 2500 times.
Walker = -1 + rand(1)*(2); %Setting the walker to be a random number between -1 or 1.
if Walker > 0 %Saying if the walker takes a step to the right.
Left(i) = Left(i-1); %Left number of steps stays the same.
Right(i) = Right(i-1) + Walker; %Right number of steps goes up by walker value.
elseif Walker < 0 %Saying if the walker takes a step to the left.
Left(i) = Left(i-1) + Walker; %Left number of steps goes up by walker value.
Right(i) = Right(i-1); %Right number of steps stay the same.
end %Ending the if statement.
end %Ending the for loop.
location = Right(end) - abs(Left(end)); %Making the location the right number of steps minus the left.
Finallocation(x) = location; %Setting the location to be the end values of the walkers.
end %Ending the for loop.
%% Plotting the Results
figure('Name','Ensemble Histogram') %Labeling the figure appropriately.
histogram(Finallocation,20) %Making a histogram of the final locations.
z1 = sprintf('Ensemble of step size = %d',N); %Making a title that updates as variables are changed.
title(z1) %Making z1 the title.
xlabel('Final Location') %Labeling the x-axis appropriately.
ylabel('Number of Walkers') %Labeling the y-axis appropriately.
end %Ending the for loop.
2 comentarios
Image Analyst
el 4 de Abr. de 2020
You have 3 nested for loops. What do you want to plot, and where?
Patrick Keaveney
el 4 de Abr. de 2020
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Data Distribution Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!