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: 3751289 • 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. If you can condense the code, please post it below. Will thumbs up!

Edit: this is in Matlab

________________

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=5000;

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(t1);

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

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

    d1(i) = hypot(xt1(i), yt1(i));

end

for i = 1:n

    [x,y] = rw(t2);

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

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

    d2(i) = hypot(xt2(i), yt2(i));

end

for i = 1:n

    [x,y] = rw(t3);

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

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

    d3(i) = hypot(xt3(i), yt3(i));

end

f1 = subplot(1,3,1);

hist(d1, 1:2:50)

f2 = subplot(1,3,2);

hist(d2, 1:2:50)

f3 = subplot(1,3,3);

hist(d3, 1:2:50)

b = 2;

d = 0:2:50;

pl=b*(2*n*d/t1).*exp(-d.^2/(t1));

f1 = subplot(1,3,1);

hold on

plot(d,pl,'r')

% put all of the labels here so that it looks nicer

hold off

%plotting for second t value (t=200)

b = 2;

d = 0:2:50;

p2 = b*(2*n*d/t2).*exp(-d.^2/(t2));

f2 = subplot(1,3,2);

hold on

plot(d,p2,'r')

hold off

%plotting for third t value (t=300)

b = 2;

d = 0:2:50;

p3 = b*(2*n*d/t3).*exp(-d.^2/(t3));

f3 = subplot(1,3,3);

hold on

plot(d,p3,'r')

hold off

Explanation / Answer

That's all what can be done, I have simply merged three for loops into one. If you think of "trying to use for loops and the likes to reiterate the plotting", then if that is done by any means it would add time complexity. GUI and plotting process is time consuming so there is no point in doing that. Also, I doubt this step:

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

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

x(t1+1) this operation is independent of for loop, I guess it should be x(t1+i). Do comment, Then I may help you in much better manner.

Find condensed code below:

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=5000;

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);


[x1,y1] = rw(t1);
[x2,y2] = rw(t2);
[x3,y3] = rw(t3);

for i = 1:n
xt1(i)=x1(t1+1);

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

d1(i) = hypot(xt1(i), yt1(i));


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

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

d2(i) = hypot(xt2(i), yt2(i));


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

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

d3(i) = hypot(xt3(i), yt3(i));

end

f1 = subplot(1,3,1);

hist(d1, 1:2:50)

f2 = subplot(1,3,2);

hist(d2, 1:2:50)

f3 = subplot(1,3,3);

hist(d3, 1:2:50)

b = 2;

d = 0:2:50;

pl=b*(2*n*d/t1).*exp(-d.^2/(t1));

f1 = subplot(1,3,1);

hold on

plot(d,pl,'r')

% put all of the labels here so that it looks nicer

hold off

%plotting for second t value (t=200)

b = 2;

d = 0:2:50;

p2 = b*(2*n*d/t2).*exp(-d.^2/(t2));

f2 = subplot(1,3,2);

hold on

plot(d,p2,'r')

hold off

%plotting for third t value (t=300)

b = 2;

d = 0:2:50;

p3 = b*(2*n*d/t3).*exp(-d.^2/(t3));

f3 = subplot(1,3,3);

hold on

plot(d,p3,'r')

hold off

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