Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;iPE[i]= -1/dr; }// end j-loop }// end i-loop

} //------------------object motion public void positionstep(double cof){ for(int i = 0;i 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 to sum 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; trail.addPoint(x[0], y[0]); trail.addPoint(t, sumK); trail.addPoint(t, sumP);

}

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2014 Integration Services

Authors: Brian Knight, Devin Knight

1st Edition

1118850904, 9781118850909

More Books

Students also viewed these Databases questions

Question

Discuss and illustrate the role of warehouses in reverse logistics.

Answered: 1 week ago