MATLAB Answers

How can I add a joining line between each iteration loop of scatter points

2 views (last 30 days)
Lucy Worrall
Lucy Worrall on 19 Oct 2020
Edited: Adam Danz on 23 Oct 2020
So i am plotting a random walk of electrons in a box and i would like my graph to show lines between the electrons subsequent positions so i can see the path each has taken
figure
%define randoms for 10 particles and 10 steps
a = -1;
b = 1;
x = zeros(10,10);
y = zeros(10,10);
z = zeros(10,10);
%initial positions are in first column - just before first scatter
x(:,1) = (b-a)*rand(10,1) + a; %random number between 1 and -1 for x,y,z,
y(:,1) = (b-a)*rand(10,1) + a;
z(:,1) = (b-a)*rand(10,1) + a;
%set different rows = different electrons
rphi = rand(10); %angles after scatter
rtheta = rand(10);
rlambda = rand(10);
%equations
phi=2*pi*rphi;
costheta=2*rtheta-1;
lambda=0.6; %mean free path
s=-lambda*log(rlambda); %step length s
for i=1:10 % loop over 10 electrons
for j=2:10 % loop over 9 scatters **j=1 is a column holding the initial angles
s=-lambda*log(rlambda(i,j));
A=[cos(phi(i,j-1)).*costheta(i,j-1) -sin(phi(i,j-1)) cos(phi(i,j-1)).*sin(acos(costheta(i,j-1)));...
sin(phi(i,j-1)).*costheta(i,j-1) cos(phi(i,j-1)) sin(phi(i,j-1)).*sin(acos(costheta(i,j-1)));
-sin(acos(costheta(i,j-1))) 0 costheta(i,j-1)];
% and i want the next collumn from the previous collumn of phi and costheta for B
B=[sin(acos(costheta(i,j))).*cos(phi(i,j)) ; sin(acos(costheta(i,j))).*sin(phi(i,j)) ; costheta(i,j)];
M=A*B;
%want first collumns of s to times by M and i want to store all x
x(i,j)= s*M(1,1) + x(i,j-1); %positions after first scatter and then one steplength in the new direction
y(i,j)= s*M(2,1) + y(i,j-1);
z(i,j)= s*M(3,1) + z(i,j-1);
%BCs and reflections
if x(i,j) > 1
x(i,j) = 2 - x(i,j);
elseif x(i,j) < -1
x(i,j) = -2 - x(i,j);
end
if y(i,j) < -1
y(i,j) = -2 - y(i,j);
elseif y(i,j) > 1
y(i,j) = 2 - y(i,j);
end
if z(i,j) < -1
z(i,j) = -2 - z(i,j);
elseif z(i,j) > 1
z(i,j) = 2 - z(i,j);
end
end
end
for j = 2:10
scatter3(x(:,j),y(:,j),z(:,j), 'filled')
hold on
scatter3(x(:,j-1),y(:,j-1),z(:,j-1),'k')
end

  0 Comments

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 19 Oct 2020
Edited: Adam Danz on 23 Oct 2020
>How can I add a joining line between each iteration loop of scatter points
Use plot3() instead of scatter3().
For example,
hold on
for j = 2:10
h = plot3(x(:,j),y(:,j),z(:,j), 'o-');
% Specify marker and line ^^
h.MarkerFaceColor = h.Color;
plot3(x(:,j-1),y(:,j-1),z(:,j-1),'ko-')
% Specify color, marker and line ^^^
end

  0 Comments

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!