Question
I need to create a graph of the kinetic, potential, and total energy as a function of time for an n-body system using Java. The
I need to create a graph of the kinetic, potential, and total energy as a function of time for an n-body system using Java. The code I have so far is below:
NBodyObject.java
import java.awt.*; import org.opensourcephysics.display.*;
public class NBodyObject implements Drawable { double cof,dt,t,E,K,P,T; 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];
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); irad=5; //smaller moving planet for(int i = 0;i for(int i = 0;i } NBodyApp.java import org.opensourcephysics.controls.*; import org.opensourcephysics.display.*; import org.opensourcephysics.frames.*; /** * PlanetApp models an orbiting planet. * * This program demonstrates: * how to use the Simulation control to run and single step a time dependent model. * * @author Wolfgang Christian, Jan Tobochnik, Harvey Gould * @version 1.0 */ public class NBodyApp extends AbstractSimulation { PlotFrame frame = new PlotFrame("x", "y", "N-Body Orbits"); NBodyObject planet = new NBodyObject(); /** * Constructs the PlanetApp. */ public NBodyApp() { frame.setSize(600,600); frame.addDrawable(planet); // frame.setPreferredMinMax(-1.2, 1.2, -1.2, 1.2); frame.setPreferredMinMax(-10., 25., -30., 30.); frame.setSquareAspect(true); } /** * Steps the time. */ public void doStep() { // planet.trail.clear(); for(int i = 0;i /** * Initializes the animation using the values in the control. */ public void initialize() { // planet.x = control.getDouble("x"); // planet.vx = control.getDouble("vx"); // planet.y = control.getDouble("y"); // planet.vy = control.getDouble("vy"); // initialize planet.nbody = control.getInt("nbody"); planet.dt = control.getDouble("dt"); planet.nspeed = control.getInt("nspeed"); planet.t = 0.0; for(int k = 0;k /** * Resets animation to a predefined state. */ public void reset() { // control.setValue("x", 10.0); // control.setValue("vx", 0.00); // control.setValue("y", 0.00); // control.setValue("vy", 0.10); control.setValue("nbody", 3); control.setValue("dt", 0.01); control.setValue("nspeed", 10); initialize(); } /** * Starts the Java application. * @param args command line parameters */ public static void main(String[] args) { SimulationControl.createApp(new NBodyApp()); } }
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