Printed by Domenico Galli Feb 14, 03 14:27 5 10 15 20 25 30 35 40 45 50 55 60 65 70 PeRK4plotter.java Page 1/3 //***************************************************************************** // $Log: PeRK4plotter.java,v $ // Revision 1.2 2003/02/14 01:04:33 galli // − bug fixed // Revision 1.1 2003/02/14 00:59:50 galli // − Initial revision //***************************************************************************** // pendolo semplice // punto materiale vincolato a una circonferenza giacente su di un piano // verticale // usa il metodo di Runge−Kutta del IV ordine // traccia i grafici dei risultati //***************************************************************************** import java.io.*; import java.util.*; import java.awt.*; import java.awt.geom.*; //***************************************************************************** public class PeRK4plotter { double l; double g=9.80665; double x0,v0; double t,x,v,a; double tMax,deltaT; java.util.List lxt,lvt,lat; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− public static void main(String args[]) { PeRK4plotter peRK4plotter=new PeRK4plotter(); peRK4plotter.dataRead(); peRK4plotter.run(); peRK4plotter.plot(); } //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // creatore PeRK4plotter() { lxt=new ArrayList(); lvt=new ArrayList(); lat=new ArrayList(); } //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // legge i dati iniziali da console void dataRead() { BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); try { System.out.print("lunghezza=m "); l=Double.parseDouble(stdin.readLine()); System.out.print("phi0=gradi "); x0=Math.PI/180.0*Double.parseDouble(stdin.readLine()); System.out.print("omega0=gradi/s "); v0=Math.PI/180.0*Double.parseDouble(stdin.readLine()); System.out.print("tMax=s "); tMax=Double.parseDouble(stdin.readLine()); System.out.print("deltaT=s "); deltaT=Double.parseDouble(stdin.readLine()); } catch(IOException ioe) { System.out.println(ioe); System.exit(1); } catch(NumberFormatException nfe) { System.out.println(nfe); System.exit(1); } Friday February 14, 2003 1/3 Printed by Domenico Galli Feb 14, 03 14:27 PeRK4plotter.java Page 2/3 System.out.println("l="+l+", phi0="+x0+", omega0="+v0+", tMax="+tMax+ ", deltaT="+deltaT); 75 80 85 90 95 100 105 110 115 120 125 130 135 140 2/3 } //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // esegue il calcolo void run() { int i; double xpg; double k1,j1,k2,j2,k3,j3,k4,j4,vm1,am1,vm2,am2,vf,af; // storePeriod=periodo di disegno dati sul grafico // Disegna 1 punto ogni storePeriod dati // Disegna al massimo 10000 punti int storePeriod=Math.max(1,(int)(tMax/deltaT)/10000); x=x0; v=v0; a=−g*Math.sin(x)/l; // accelerazione iniziale lxt.add(new Point2D.Float((float)0,(float)(180.0*x0/Math.PI))); lvt.add(new Point2D.Float((float)0,(float)(180.0*v0/Math.PI))); lat.add(new Point2D.Float((float)0,(float)(180.0*a/Math.PI))); for(t=0.0,i=0;;) { // algoritmo di Runge−Kutta IV ordine k1=v*deltaT; j1=a*deltaT; vm1=v+j1/2; am1=−g*Math.sin(x+k1/2)/l; k2=vm1*deltaT; j2=am1*deltaT; vm2=v+j2/2; am2=−g*Math.sin(x+k2/2)/l; k3=vm2*deltaT; j3=am2*deltaT; vf=v+j3; af=−g*Math.sin(x+k3)/l; k4=vf*deltaT; j4=af*deltaT; x=x+(k1+2*k2+2*k3+k4)/6; v=v+(j1+2*j2+2*j3+j4)/6; a=−g*Math.sin(x)/l; // fine algoritmo di Runge−Kutta IV ordine // xpg=angolo nel primo giro. −PI<xpg<PI xpg=x−2*Math.PI*Math.floor((x+Math.PI)/(2*Math.PI)); t=t+deltaT; i++; if(t>tMax)break; // se ci sono > 10000 punti, memorizza 1 punto ogni storePeriod punti if(i%storePeriod==0) { lxt.add(new Point2D.Float((float)t,(float)(180.0*xpg/Math.PI))); lvt.add(new Point2D.Float((float)t,(float)(180.0*v/Math.PI))); lat.add(new Point2D.Float((float)t,(float)(180.0*a/Math.PI))); System.out.println(i+"\tt="+t+"\tx="+xpg+"\tv="+v+"\ta="+a); } } } //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // disegna il grafico void plot() { Plot2D2M pP0=new Plot2D2M(700,400); pP0.setPlotColor(Color.red); pP0.setXunits("s"); pP0.setYunits("gradi"); pP0.setXmin(0); pP0.resizeForVals(lxt); pP0.drawAxes(); pP0.drawPoints(lxt); pP0.drawPolyLine(lxt); PlotFrame pF0=new PlotFrame(); Friday February 14, 2003 Printed by Domenico Galli Feb 14, 03 14:27 145 150 155 160 165 170 175 PeRK4plotter.java Page 3/3 pF0.setTitle("Spostamento angolare"); pF0.setLocation(new Point(20,20)); pF0.getContentPane().add(pP0,BorderLayout.CENTER); pF0.pack(); pF0.setVisible(true); //......................................................................... Plot2D2M pP1=new Plot2D2M(700,400); pP1.setPlotColor(Color.magenta); pP1.setXunits("s"); pP1.setYunits("gradi/s"); pP1.setXmin(0); pP1.resizeForVals(lvt); pP1.drawAxes(); pP1.drawPoints(lvt); pP1.drawPolyLine(lvt); PlotFrame pF1=new PlotFrame(); pF1.setTitle("Velocità angolare"); pF1.setLocation(new Point(40,40)); pF1.getContentPane().add(pP1,BorderLayout.CENTER); pF1.pack(); pF1.setVisible(true); //......................................................................... Plot2D2M pP2=new Plot2D2M(700,400); pP2.setPlotColor(Color.blue); pP2.setXunits("s"); pP2.setYunits("gradi/s²"); pP2.setXmin(0); pP2.resizeForVals(lat); pP2.drawAxes(); pP2.drawPoints(lat); pP2.drawPolyLine(lat); PlotFrame pF2=new PlotFrame(); pF2.setTitle("Accelerazione angolare"); pF2.setLocation(new Point(60,60)); pF2.getContentPane().add(pP2,BorderLayout.CENTER); pF2.pack(); pF2.setVisible(true); } //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− 180 } //***************************************************************************** Friday February 14, 2003 3/3