how to combine two plots in one figure

1 Ansicht (letzte 30 Tage)
Jamie Al
Jamie Al am 9 Feb. 2021
Bearbeitet: Jamie Al am 10 Feb. 2021
I have the following code and would like to plot the motion of a particle under the influence of magnetic field with two intensity It should look something like this:
But for some reason my code doesn't show the right sizes:
dt = 1e-2; % time step
mass = 1.0; % mass of particle
charge = 1.0; % charge of particle
n = 500; % number of time steps
%Initial parameters:
v = [0, 1, 0]; % initial velocity
x = [0, 0, 0]; % initial position
B1 = [0, 0, 10];%B = [0, 0, 10]; initial mag. field along z directions
B2 = [0, 0, 2];
E = [0, 0, 0]; % initial E field, for case 1) E = 0 and B =/ 0
X = zeros(n,3); % initialize an array of zeros with size nx3 for positions
V = zeros(n,3);
X1 = zeros(n,3); % initialize an array of zeros with size nx3 for positions
V1 = zeros(n,3);
X2 = zeros(n,3); % initialize an array of zeros with size nx3 for positions
V2 = zeros(n,3);
for time = 1:1:n
[x1,v] = boris_rotation(x,v,charge,mass,dt,B1,E);
[x2,v2] = boris_rotation(x,v,charge,mass,dt,B2,E);
X1(time,:) = x1;
V1(time,:) = v;
X2(time,:) = x2;
V2(time,:) = v2;
end
figure;
plot(X1(:,1),X2(:,2),'k','Linewidth',2);
grid on
hold on
plot(X2(:,1),X1(:,2),'r','Linewidth',2);
I am getting this:

Akzeptierte Antwort

Jakob B. Nielsen
Jakob B. Nielsen am 10 Feb. 2021
Bearbeitet: Jakob B. Nielsen am 10 Feb. 2021
In your code, you input the initial x and v during every loop iteration - not the most recent x and v. That is why you just get two circles on top of each other.
This small change gives me a figure that looks, not quite like the first one you posted, but a whole lot closer, and you can work from there :)
x1=x; %initial positions 'zeroed'
x2=x;
for time = 1:1:n
[x1,v] = boris_rotation(x1,v,charge,mass,dt,B1,E); %input x1 instead of x
[x2,v2] = boris_rotation(x2,v2,charge,mass,dt,B2,E); %input x2 instead of x, and also v2 instead of v
X1(time,:) = x1;
V1(time,:) = v;
X2(time,:) = x2;
V2(time,:) = v2;
end

Weitere Antworten (0)

Kategorien

Mehr zu Particle & Nuclear Physics finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by