(a) Write a MATLAB script RandomWalkC2D. The walker starts at the origin (xl,yl)
ID: 3707807 • Letter: #
Question
(a) Write a MATLAB script RandomWalkC2D. The walker starts at the origin (xl,yl)-(0,0), and at each step takes a unit step (rstep-1) in a random direction. Choose the direction of each step by randomly picking an angle 0e[0,21] using rand. Then k+1 kt1kTstep sin(Ox) Calculate the complete motion first, then animate the motion with another loop through time. Plot the position of the origin with a "+" sign and the position of the walker with a circle. At each time step in the animation loop, plot a line representing the path of the walker's entire history up to the current time. We are interested in seeing motion over hundreds or even thousands of steps.Explanation / Answer
MATLAB CODE:FOR A
x(1)=0;
y(1)=0;
r_step=1;
N=1000
for i=1:N
x(i+1)=x(i)+r_step*cos(2*pi*rand);
y(i+1)=y(i)+r_step*cos(2*pi*rand);
end
figure
for n=1:N
plot(x(1:n),y(1:n))
hold on
plot(x(n),y(n),'ro');
axis equal;
axis([0,max(x),0,max(y)+10]);
hold off
xlabel('x (ft)');
ylabel('y (ft)');
title('Projectile Trajectory');
M(n)=getframe;
end
% play as smooth movie 3 times at 10 frames per second
% note that it goes through the frames 2x initially to get ready for
% full speed play. So it will actually play 2x slower and 3x at full speed.
numtimes=3;
fps=1;
movie(M,numtimes,fps)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.