### (Solved) : Trying Create N Body Simulation Using Java Purpose Create Graph Kinetic Potential Total En Q35612169 I am trying to create a n-body simulation using Java. Thepurpose of this is to create a graph of the Kinetic, Potential, andTotal energies as a function of time. The code involving all theproperties of the bodies can be seen below. The code generates nbodies on a graph, then allows them to move. As time goes on, thebodies slow down and eventually come to rest due to the velocitiesbeing repeatedly multiplied by 0.999 in velocitystep(doublecof). The lines relevant to the energies are inbold.

import java.awt.*;
import org.opensourcephysics.display.*;

public class NBodyObject implements Drawable {

double cof,dt,t,E;
int nspeed,nbody;
private double dx,dy,b,dr,dr2;
final static int arraysize = 100;

Trail trail = new Trail();
double x[]=new double[arraysize];
double y[]=new double[arraysize];
double vx[]=new double[arraysize];
double vy[]=new double[arraysize];
double ax[]=new double[arraysize];
double ay[]=new double[arraysize];
double r[] =new double[arraysize];
double r2[]=new double[arraysize];
double KE[]=new double[arraysize];
double PE[]=new double[arraysize];
double TE[]=new double[arraysize];
double sumP;
double sumK;

public NBodyObject(){System.out.println(“A new NBody object iscreated.”);
}
//————-object properties
//   public void energy(){
//          E=0.5*(vx*vx+vy*vy)-1./Math.sqrt(x*x+y*y);
//   }
public void accel(){
for(int i = 0;i<nbody;i++) {
r2[i] =x[i]*x[i]+y[i]*y[i];
r[i] =Math.sqrt(r2[i]);
// ax[i]=-nbody*x[i]/r[i]/r2[i];
// ay[i]=-nbody*y[i]/r[i]/r2[i];
ax[i]=-x[i]*r2[i]*r2[i]*.0001;
ay[i]=-y[i]*r2[i]*r2[i]*.0001;
// ax[i]=-x[i];
// ay[i]=-y[i];
}// end for-loop

}
public void nbaccel(){
// for(int i = 0;i<nbody;i++) {
// ax[i]=0.0;
// ay[i]=0.0;
// }// end for-loop

accel();
for(int i = 0;i<nbody-1;i++) {
for(int j =i+1;j<nbody;j++) {
dx=x[i]-x[j];
dy=y[i]-y[j];
dr2=dx*dx+dy*dy;
dr =Math.sqrt(dr2);
b=2.5/dr2/dr;
ax[i]=ax[i]+b*dx;
ay[i]=ay[i]+b*dy;
ax[j]=ax[j]-b*dx;
ay[j]=ay[j]-b*dy;
PE[i]= -1/dr;
}// end j-loop
}// end i-loop

}
//——————object motion
public void positionstep(double cof){
for(int i = 0;i<nbody;i++) {
x[i] = x[i]+vx[i]*dt*cof;
y[i] = y[i]+vy[i]*dt*cof;
}// end for-loop
}
public void velocitystep(double cof){
nbaccel();
for(int i = 0;i<nbody;i++) {
vx[i] = vx[i]+ax[i]*dt*cof;
vy[i] = vy[i]+ay[i]*dt*cof;
vx[i] = vx[i]*0.999;//—————————–these two lines cause the slowdownand eventual stoppage of particle motion
vy[i] = vy[i]*0.999;//—————————–and thus causes a change in theenergies
KE[i] = 0.5*(vx[i]*vx[i]+vy[i]*vy[i]);

}// end for-loop
}
public void sym2bstep(double cof){
positionstep(0.5*cof);
velocitystep(1.0*cof);
positionstep(0.5*cof);
}
public void doStep(double cof){
sym2bstep(cof);

// Iterate through all elements and add them tosum
for (int i = 0; i < PE.length; i++){
sumP += PE[i];
}

for (int i = 0; i <KE.length; i++){
sumK += KE[i];
}

// t=t+dt;

}

public void draw(DrawingPanel panel, Graphics g) {
int ypix = panel.yToPix(0.0)-irad; //sun at the origin
g.setColor(Color.BLUE);

for(int i = 0;i<nbody;i++) {
g.setColor(Color.RED);

}
// for(int i = 0;i<nbody;i++) {
g.setColor(Color.BLACK);
//   }