Question
I am trying to create a n-body simulation using Java. The purpose of this is to create a graph of the Kinetic, Potential, and Total
I am trying to create a n-body simulation using Java. The purpose of this is to create a graph of the Kinetic, Potential, and Total energies as a function of time. The code involving all the properties of the bodies can be seen below. The code generates n bodies on a graph, then allows them to move. As time goes on, the bodies slow down and eventually come to rest due to the velocities being repeatedly multiplied by 0.999 in velocitystep(double cof). The lines relevant to the energies are in bold.
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 is created."); } //-------------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 } public void nbaccel(){ // for(int i = 0;i accel(); for(int i = 0;i } //------------------object motion public void positionstep(double cof){ for(int i = 0;i } public void draw(DrawingPanel panel, Graphics g) { int irad=5; int xpix = panel.xToPix(0.0)-irad; int ypix = panel.yToPix(0.0)-irad; //sun at the origin g.setColor(Color.BLUE); g.fillOval(xpix, ypix, 2*irad, 2*irad); for(int i = 0;i } irad=5; //smaller moving planet // for(int i = 0;i irad=5; xpix = panel.xToPix(t)-irad; ypix = panel.yToPix(sumP)-irad; g.setColor(Color.YELLOW); g.fillOval(xpix, ypix, 2*irad, 2*irad); // trail.draw(panel, g); // trail.draw(panel, g); } } So far, I have only been able to continuously sum the energies as the simulation goes on. When graphing this, the kinetic energy goes off to infinity when in actuality it should go to zero since the bodies eventually stop moving. I need help finding the total kinetic energy for the bodies at each individual step.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started