import java.io.*; public class omg { // Declare constants public static final double DT = 1e-2; // time interval in seconds public static final double Tmax = 1000.; // duration of simulation in seconds public static final double G = 9.807; // acceleration due to gravity in m/s/s public static final double M = 1.0; // Mass of pendulum in kg public static final double L = 9.8; // Length of pendulum in meter public static final double MAXAMPLITUDE =Math.PI ;//Math.PI; // Amplitude in rad public static final double DAMPLITUDE = (1.0/100.0); // Amplitude interval for sampling in rad public static final double PRINTAMPLITUDE = 0.20; // Used to print the time dependant data at a single amplitude value // Start main method public static void main(String[] args) { //////////////////////////////////////////////////////////////// //Opening files /////////////////////////////////////////////////////////////// // Open log file to write the angle vs time data PrintWriter outputFile = null; try { outputFile = new PrintWriter(new FileOutputStream("angle_vs_time.txt",false)); } catch(FileNotFoundException e) { System.out.println("File error. Program aborted."); System.exit(0); } // Open log file to write the period vs amplitude data PrintWriter outputFile2 = null; try { outputFile2 = new PrintWriter(new FileOutputStream("period_vs_amplitude.txt",false)); } catch(FileNotFoundException e) { System.out.println("File error. Program aborted."); System.exit(0); } ////////////////////////////////////////////////////////////////////// //Starting for real. ////////////////////////////////////////////////////////////////////// // Calculate length of arrays int imax = (int)(Tmax/DT); // Maximal index // Declare main variables double[] t = new double[imax]; // time in sec double[] angle = new double[imax]; // angular position in radians double[] ang_vel = new double[imax]; // angular velocity in rad/s double[] ang_acc = new double[imax]; // angular acceleration in rad/s/s double[] Etot = new double[imax-1]; // Total energy in J double amplitude=Math.toRadians(0.25); double cntr; // Starting the loop that will sample the value of the amplitude for(cntr = DAMPLITUDE; cntr < MAXAMPLITUDE;cntr+=DAMPLITUDE) { ///////////////////////////////////////////////////////////////////// // Applying Verlet's method to solve the ODE (get angle vs time) ///////////////////////////////////////////////////////////////////// // Initialize time t[0] = 0; // We start the pendulum from rest and with its angular position equal to the amplitude angle[0] = amplitude; ang_vel[0] = 0; // Get the angular acceleration by calling the ang_acceleration method ang_acc[0] = ang_acceleration(angle[0],ang_vel[0],t[0]); // Remember, for Verlet, we need the previous two points so we // do one step of Euler to get value for i=1 t[1] = t[0]+DT; angle[1] = angle[0] + ang_vel[0]*DT; ang_acc[1] = ang_acceleration(angle[1],ang_vel[1],t[1]); for(int i=2;i<imax;i++) { // Update time t[i] = t[i-1]+DT; // TASK 5: Update the angular position using Verlet's method angle[i]= (2.0*angle[i-1])-angle[i-2]+(ang_acc[i-1]*DT*DT); // TASK 6: Update angular velocity ang_vel[i-1]=ang_vel[i-2]+ang_acc[i-2]*DT; // Update angular acceleration ang_acc[i]=ang_acceleration(angle[i],ang_vel[i],t[i]); } ////////////////////////////////////////////////////////////////////////////// // Print time dependent data for the case where amplitude == PRINTAMPLITUDE ////////////////////////////////////////////////////////////////////////////// if(Math.abs(cntr - PRINTAMPLITUDE) < 1e-10) { for(int i=0;i<(imax-2);i+=50) // Only print every 10 points { outputFile.println(t[i] + " " + angle[i] + " " + ang_vel[i] + " " + ang_acc[i] + " "); } } ////////////////////////////////////////////////////////////////////////////// // Find and then print period as a function of amplitude ////////////////////////////////////////////////////////////////////////////// // BONUS TASK (ONLY DO IF YOU'RE DONE WITH EVERYTHING) // Find a better way to get the period from the angle data. // Look for two consecutive times when the angle is zero. That correspond to half a cycle. int j = 0; // Index looping over time double angleIsZero = 0; // Storing the time when angle is zero while(angleIsZero == 0) { // If the angle switch sign if(angle[j]*angle[j+1]<0){ // Choose the angle with the smallest magnitude angleIsZero = Math.abs(angle[j]) < Math.abs(angle[j+1]) ? t[j]:t[j+1]; } j++; } double angleIsZeroNext = 0; // Storing the next time when angle is zero while(angleIsZeroNext == 0) { // If the angle switch sign if(angle[j]*angle[j+1]<0){ // Choose the angle with the smallest magnitude angleIsZeroNext = Math.abs(angle[j]) < Math.abs(angle[j+1]) ? t[j]:t[j+1]; } j++; } // The 2* below is needed because we have the time difference for half a cycle. outputFile2.println(amplitude + " " + 2*(angleIsZeroNext-angleIsZero)); } /////////////////////////////////////////////////////////////////////// // Closing files /////////////////////////////////////////////////////////////////////// outputFile.close(); outputFile2.close(); } //////////////////////////////////////////////////////////////////////////// // Method calculating the angular acceleration //////////////////////////////////////////////////////////////////////////// public static double ang_acceleration(double angle, double angvel,double time) { double ang_accel; double q, dt, omega, drivingForce=0.5; q=0.5; dt=0.01; omega=2.0/3.0; // TASK 4: Write the equation for the angular acceleration ang_accel= (-G/L)*Math.sin(angle)-q*angvel+(drivingForce*Math.sin(omega*time)); return ang_accel; } }
The main part of the program is the method listed at the bottom.I've done all i could think of but i still get an error for an array that is out of bounds...The program basically simulates the movement of a pendulum,the ang_acceleration method calculates the angular acceleration and the for loop loops for every single array index. You guys can ignore the part where it says //BONUS.
is there something wrong with my initial for loop?I had an earlier version of this code with a simplified ang_acceleration method that only had one variable called to it and it was working perfectly.Sorry if this sounds way too specific.Ask questions if needed and I will try to answer as best as i can.