location:"directory of MATLAB"\toolbox\coder\codegendemos\coderdemo_atoms\run_atoms.m
MATLAB version: MATLAB R2011a
function atoms = manage_collision(atoms,i,j)
coder.inline('never');
xi = atoms(i).x;
yi = atoms(i).y;
xj = atoms(j).x;
yj = atoms(j).y;
vxi = atoms(i).vx;
vyi = atoms(i).vy;
vxj = atoms(j).vx;
vyj = atoms(j).vy;
nx = xi-xj;
ny = yi-yj;
n2=nx*nx+ny*ny;
n1=sqrt(n2);
nx = nx/n1;
ny = ny/n1;
vDotNi = vxi*nx+vyi*ny;
vDotNj = vxj*nx+vyj*ny;
new_vxi = 2*vDotNj*nx - vxj;
new_vyi = 2*vDotNj*ny - vyj;
new_vxj = 2*vDotNi*nx - vxi;
new_vyj = 2*vDotNi*ny - vyi;
if isfinite(new_vxi) && isfinite(new_vyi) && isfinite(new_vxj) && isfinite(new_vyj)
atoms(i).vx = new_vxi;
atoms(i).vy = new_vyi;
atoms(j).vx = new_vxj;
atoms(j).vy = new_vyj;
end
I think there is a mistake in this function.
new_vxi = 2*vDotNj*nx - vxj;
new_vyi = 2*vDotNj*ny - vyj;
new_vxj = 2*vDotNi*nx - vxi;
new_vyj = 2*vDotNi*ny - vyi;
Suppose the mass of two atoms are the same then
new_vxi = vxj;
new_vyi = vyj;
new_vxj = vxi;
new_vyj = vyi;
The two atoms exchanged their velocities each other.