Question
I just did this 3D random Walk simulation but I am trying to figure it out how to put a marker(e.g., point) to ilustrate the beginning of the simulation and another one to show the end of the simulation. Anyone has any idea how to do this? Thanks in advance. Code: r=[0 0 0]; for t= 0:0.1:20 B1 = randi (6,1); if B1==1 rnew=r+[1 0 0]; elseif B1==2 rnew=r+[-1 0 0]; elseif B1==3 rnew=r+[0 1 0]; elseif B1==4 rnew=r+[0 -1 0]; elseif B1==5 rnew=r+[0 0 1]; else rnew=r+[0 0 -1]; end hold on; plot3([r(1) rnew(1)],[r(2) rnew(2)], [r(3) rnew(3)]); drawnow ; %UPDATE the new position r=rnew; end
Expert Answer
Prashant Kumar
PhD Expert
Answered Aug 24, 2026
Something like this:
r=[0 0 0];
for t= 0:0.1:20
B1 = randi (6,1);
if B1==1
rnew=r+[1 0 0];
elseif B1==2
rnew=r+[-1 0 0];
elseif B1==3
rnew=r+[0 1 0];
elseif B1==4
rnew=r+[0 -1 0];
elseif B1==5
rnew=r+[0 0 1];
else
rnew=r+[0 0 -1];
end
hold on;
%start point Blue asterix
plot3([0 0],[0 0],[0 0], 'Marker','*', 'LineStyle','none', 'MarkerFaceColor',[1 0 0], 'MarkerSize',10);
plot3([r(1) rnew(1)],[r(2) rnew(2)], [r(3) rnew(3)]);
drawnow ;
%UPDATE the new position
r=rnew;
end
% endpoint Red circle
plot3([r(1) rnew(1)],[r(2) rnew(2)], [r(3) rnew(3)],'Marker','o','LineStyle','none','MarkerFaceColor',[1 0 0],'MarkerSize',10);
100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related matlab Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →