//this is the revised "Oscillator.java" in order to plot the attractor in phase-space. /*
*/ // Applet for plotting solutions to oscillator problem import javax.swing.*; import P251.*; public class Oscillator extends P251Applet { DoubleWell dw1; // dynamics object VariableSet vars; // variables (time, x, momentum) double[] x, xplot, pplot, t; // arrays for plotting int N; // number of time steps private inputPanel ip1, ip2; // declare input panel objects private graphPanel gpX, gpP, gpPS; // declare graph panel objects public void fillPanels() { ip1 = new inputPanel(); ip1.addField("dt",.01); ip1.addField("A",1.); ip1.addField("force",4.); addPanel(ip1); // adds input panel 1 to applet ip2 = new inputPanel(); ip2.addField("omega",Math.PI); ip2.addField("mass",1.); ip2.addField("damping",1.); addPanel(ip2); // adds input panel 2 to applet gpX = new graphPanel(900,300,false); gpX.setXLabel("t"); gpX.setYLabel("x"); gpX.setTitle("Position Evolution"); gpX.setDotSize(2); addPanel(gpX); // adds graph panel to applet gpP = new graphPanel(900,300,false); gpP.setXLabel("t"); gpP.setYLabel("p"); gpP.setTitle("Momentum Evolution"); gpP.setDotSize(2); addPanel(gpP); // adds graph panel to applet gpPS = new graphPanel(900,300,false); //this is a new graphPanel for phase-space plot gpPS.setXLabel("x"); gpPS.setYLabel("p"); gpPS.setTitle("phase space plot"); gpPS.setDotSize(2); addPanel(gpPS); // adds graph panel to applet } //end of fillPanels public void initValues() { dw1 = new DoubleWell(); // initialize object dw1.setdt(.01); // initialize DoubleWell parameters dw1.setA(1.); dw1.setforce(4.); dw1.setomega(Math.PI); dw1.setmass(1.); dw1.setdamping(1.); //initialize input field values to match the parameters above ip1.setValue(0, dw1.getdt()); //"dt" ip1.setValue(1, dw1.getA()); //"A" ip1.setValue(2, dw1.getforce()); //"force" ip2.setValue(0, dw1.getomega()); //"omega" ip2.setValue(1, dw1.getmass()); //"mass" ip2.setValue(2, dw1.getdamping()); //"damping" N = 9999; // Number of Time Steps so that tmax < 100 xplot = new double[N+1]; // array of positions pplot = new double[N+1]; // array of momenta t = new double[N+1]; // array of times t[0] = 0.0; // initial time xplot[0] = -1.2; // initial x pplot[0] = 0.0; // initial p x = new double[2]; // pair x and p x[0] = xplot[0]; // initial x x[1] = pplot[0]; // initial p vars = new VariableSet(0.0,x); // set initial variables dw1.setvars(vars); } //end of initValues public void compute () { int i; readValues(); //this is for long time behavior for (i=1; i<=2*N; i++) { // loop to compute solution vars = dw1.nextvars(); // advance in time 1 step dw1.setvars(vars); // bring old variables up to date x = vars.getx(); // extract x and p if(i>N){ //this is the condition to plot just long time behavior t[i-N] = vars.gettime(); // extract and store t for plotting xplot[i-N] = x[0]; // store x for plotting pplot[i-N] = x[1]; // store p for plotting } } //end of i loop: now trajectories have been calculated /* //this is for since the first step for (i=1; i<=N; i++) { // loop to compute solution vars = dw1.nextvars(); // advance in time 1 step dw1.setvars(vars); // bring old variables up to date x = vars.getx(); // extract x and p t[i] = vars.gettime(); // extract and store t for plotting xplot[i] = x[0]; // store x for plotting pplot[i] = x[1]; // store p for plotting } //end of i loop: now trajectories have been calculated*/ gpX.addData(t,xplot,"name"); gpP.addData(t,pplot,"name"); gpPS.addData(xplot,pplot,"name"); //plot data (x,p) into the phase space t[0] = t[N]; // setup t for next round xplot[0] = xplot[N]; // setup x for next round pplot[0] = pplot[N]; // setup p for next round } public void readValues() { dw1.setdt(ip1.getValue(0)); // input panel 1 dw1.setA(ip1.getValue(1)); dw1.setforce(ip1.getValue(2)); dw1.setomega(ip2.getValue(0)); // input panel 2 dw1.setmass(ip2.getValue(1)); dw1.setdamping(ip2.getValue(2)); } } //end of Oscillator