// Checkpoint 4 pendulum class. Comprehend, answer the questions in the code, // write your own comments, figure out how to convert it to do part 1, // the mass-on-spring code (this requires deleting eight characters) and // solve the questions on the checkpoint page // Explain the results from the default settings - why doesn't the position // tend to oscillate about 0, what are the wiggles in the velocity graph, // and what is the pendulum doing? import java.io.*; import display.*; import VisualNumerics.math.Sfun; import gov.noaa.pmel.sgt.cplab.*; import java.awt.*; class DDpend { static BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); public static void main (String argv []) throws IOException { Vector3 position = new Vector3(0,0,0) ; Vector3 velocity = new Vector3(0,0,0) ; Vector3 acceleration = new Vector3(0,0,0); Vector3 xhat = new Vector3(1,0,0); Vector3 vtemp; Vector3 vx; Vector3 force; boolean catholicPope=true; double massive = 1; double q = 1; double dt =.0023472843; int nprint = 1000; Vector3 FD = new Vector3(2,0,0); Particle fermion = new Particle(position,velocity,acceleration,massive,q,0.0); PrintWriter out; out = new PrintWriter(new FileWriter("output.dat")); Display panel = new Display("Damped, Driven Pendulum motion"); panel.setBackground(Color.yellow); Input omegaInput= new Input(" Driving Frequency",0.2); Input gammaInput= new Input(" Damping coefficient",0.50); Input initInput = new Input("Initial displacement",1.0); Input velInput = new Input("Initial velocity",10.0); Input ampInput = new Input(" Driving Amplitude",2.0); Input kInput = new Input(" Spring Constant",1.0); Input timeInput = new Input(" Time for graph",150.0); panel.addInput(omegaInput); panel.addInput(gammaInput); panel.addInput(initInput); panel.addInput(velInput); panel.addInput(ampInput); panel.addInput(kInput); panel.addInput(timeInput); SimpleGraph graph = new SimpleGraph("Damped, driven oscillator","Time","seconds", "Displacement", "Radians"); graph.showGraph(); SimpleGraph graph1 = new SimpleGraph("Damped, driven oscillator","Time","seconds", "Angular velocity", "Radians/fs"); graph1.showGraph(); Color colour = Color.red; // What does icol do? int icol = 1; int newSize = 600; // What does this while loop do? while (catholicPope) { panel.waitForButtonPress(); panel.setSize(panel.getSize().width,newSize); double omegaD= omegaInput.getDouble(); double bdamp = -gammaInput.getDouble() ; FD.x= ampInput.getDouble(); fermion.pos.x = initInput.getDouble(); fermion.vel.x = velInput.getDouble(); double kspring = kInput.getDouble(); double tottime = timeInput.getDouble(); DataSet data = new DataSet(); DataSet data1 = new DataSet(); fermion.time = 0.0; // Why are there two loops here? for(int j = 1; j < nprint;j++) { for(double itime = 0; itime< tottime/nprint; itime=itime+dt) { fermion.pos = Vector3.add(Vector3.add(fermion.pos, Vector3.multiply(fermion.vel,dt)) , Vector3.multiply(fermion.acc,dt*dt/2.0)); vtemp = Vector3.add(fermion.vel, Vector3.multiply(fermion.acc,dt/2.0)); force = Vector3.add(Vector3.multiply(xhat,-Math.sin(fermion.pos.x)*kspring), Vector3.multiply(FD,Math.cos(omegaD*fermion.time))); force = Vector3.add(force, Vector3.multiply(vtemp,bdamp)) ; fermion.acc = Vector3.multiply(force,1.0/fermion.mass) ; fermion.vel = Vector3.add(vtemp , Vector3.multiply(fermion.acc,dt/2.0)); fermion.time = fermion.time + dt; } data.addPoint(fermion.time,fermion.pos.x); data1.addPoint(fermion.time,fermion.vel.x); out.println(fermion.time + " " +fermion.pos.x); } if(icol==1){ data.setColor(Color.red); data1.setColor(Color.red);} else {if(icol==2){ data.setColor(Color.blue);data1.setColor(Color.blue);} else {if(icol==3){ data.setColor(Color.green);data1.setColor(Color.green);} else {if(icol==4){ data.setColor(Color.orange);data1.setColor(Color.orange);} else {if(icol==5){ data.setColor(Color.yellow); data1.setColor(Color.yellow); icol=0;}}}}} icol = icol+1; graph.addData(data); graph1.addData(data1); out.flush(); } out.close(); }}