Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Is there any way to condense this code (below)? I have been trying to use for lo

ID: 3751286 • Letter: I

Question

Is there any way to condense this code (below)? I have been trying to use for loops and the likes to reiterate the plotting but am not getting the desired graphs.

________________

rw function used in the code:

function[x,y] = rw(t)

x = zeros(1,t+1);

y = zeros(1,t+1);

for i = 1:t

    ang = 2*pi*rand(1);

    x(i+1) = x(i)+cos(ang);

    y(i+1) = y(i)+sin(ang);

end

________________

n=1000;

t1=100;

t2=200;

t3=300;

xt1=zeros(1,n);

yt1=zeros(1,n);

xt2=zeros(1,n);

yt2=zeros(1,n);

xt3=zeros(1,n);

yt3=zeros(1,n);

for i = 1:n

    [x,y]=rw(t3);

    xt1(i)=x(t1+1);

    yt1(i)=y(t1+1);

    xt2(i)=x(t2+1);

    yt2(i)=y(t2+1);

    xt3(i)=x(t3+1);

    yt3(i)=y(t3+1);

end

subplot(1,3,1);

plot(xt1,yt1, '.');

xlabel('x')

ylabel('y')

title('t=100')

axis([-50 50 -50 50])

axis square

subplot(1,3,2);

plot(xt2,yt2, '.');

xlabel('x')

ylabel('y')

title('t=200')

axis([-50 50 -50 50])

axis square

subplot(1,3,3);

plot(xt3,yt3, '.');

xlabel('x')

ylabel('y')

title('t=300')

axis([-50 50 -50 50])

axis square

Explanation / Answer

n=1000;

t = [100,200,300];

xt = [];

yt = [];

for i = 1:3

xt(i,:) = zeros(1,n);

yt(i,:) = zeros(1,n);

end

for i = 1:n

[x,y]=rw(t(3));

for j = 1:3

xt(j,i)=x(t(j)+1);

yt(j,i)=y(t(j)+1);

end

end

for i = 1:3

subplot(1,3,i);

plot(xt(i,:),yt(i,:), '.');

xlabel('x')

ylabel('y')

title(strcat('t=',int2str(t(i))))

axis([-50 50 -50 50])

axis square

endfor

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote