Question: MATLAB BASE CODE: clc clear all close all % n = 2 ; % mumber of particles n = 3 ; qe = - 1

MATLAB BASE CODE:
clc
clear all
close all
%n =2; %mumber of particles
n =3;
qe =-1.6e-19; % the charge of an electron
me =9.11e-31; % mass of an electron
cell =5e-3; %observing a box of 1 cm
%initial positions
% Xs =[-3e-3,0]; % the list of particles x-cordinates
% Ys =[0,0]; % the list of particles y-cordinates
Xs =[-3e-3,0,2e-3]; % the list of particles x-cordinates
Ys =[0,0,15e-9]; % the list of particles y-cordinates
%initail velocities
% Us =[1e5,0]; % the list of particles x-velocity
% Vs =[0,0]; % the list of particles y-velocity
Us =[1e5,0,0]; % the list of particles x-velocity
Vs =[0,0,0]; % the list of particles y-velocity
%timesteps
dt =1e-13; %runnig 10 itteratins for every 1 ps
imax =1e-7/dt; % duration of sim: 100ns
ireport =1e-9/dt; % print once every 1 ns
%create a matrix of particle paris that makes sure we dont double count
pairs= triu(ones(n),1);
%Force initialization
%caculate the force as a sum of each particles from each particle pair
Fx = zeros([1,n]);
Fy = zeros([1,n]);
for i =1:n
for j = i+1:n
if pairs(i,j)==1%only applies to unique particle pairs
dx = Xs(j)- Xs(i); % ditance between the electrons in x dim
dy = Ys(j)- Ys(i); % ditance between the electrons in y dim
r = sqrt (dx^2+ dy^2); % the toltal distance magnitude between the two
electrons
R =[dx, dy]/r; %normalized vector for direction of the force
F = F_Coulomb(r, qe , qe); %force between i-th and j-th
%particle i
Fx(i)= Fx(i)+ F*R(1); %force componets in X
Fy(i)= Fy(i)+ F*R(2); %force componets in y
%particle i
%apply Newton's third law
Fx(j)= Fx(j)- F*R(1); %force componets in X
Fy(j)= Fy(j)- F*R(2); %force componets in y
end
end
end
%main simulation using the verlt
for t =1:imax
%update the position
Xs = Xs + Us*dt + Fx/(2*me)*dt^2; %position update in x
Ys = Ys + Vs*dt + Fy/(2*me)*dt^2; %position update in y
%half update velocity
Us = Us + Fx/(2*me)*dt; %velocity in X
Vs = Vs + Fy/(2*me)*dt; %velocity in y
% recalculate the force
Fx = zeros([1,n]);
Fy = zeros([1,n]);
for i =1:n
for j = i+1:n
if pairs(i,j)==1%only applies to unique particle pairs
dx = Xs(j)- Xs(i); % ditance between the electrons in x dim
dy = Ys(j)- Ys(i); % ditance between the electrons in y dim
r = sqrt (dx^2+ dy^2); % the toltal distance magnitude between the two
electrons
R =[dx, dy]/r; %normalized vector for direction of the force
F = F_Coulomb(r, qe , qe); %force between i-th and j-th
%particle i
Fx(i)= Fx(i)+ F*R(1); %force componets in X
Fy(i)= Fy(i)+ F*R(2); %force componets in y
%particle i
%apply Newton's third law
Fx(j)= Fx(j)- F*R(1); %force componets in X
Fy
MATLAB BASE CODE: clc clear all close all % n = 2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!