Magnetic pendulum part 1

Jonathon Sumner
Physics Department
Dawson College
Jean-François Brière
Physics Department
Dawson College
Sameer Bhatnagar
Physics Department
Dawson College
March 26, 2019
March 26, 2019

Modelling the trajectory of a magnetic pendulum.

A pendulum, made with a magnet, is moving over a surface with embedded magnets that are attracting the pendulum. In this lab, we want to model the trajectory of the magnetic pendulum.

Lab instructions

Magnetic pendulum pt 1 instructions

Codes

We will need two codes for this lab:

The two files are zipped together. Don't forget to unzip them before starting to work on your tasks.

Magnetic pendulum codes

Code sample: Magnetic pendulum
/**
   Motion of a magnetic pendulum

   Author: J-F Briere
   Current version written: March 2019
   Description: Calculates and plot the motion of a pendulum made with
				a magnet moving above a plane with 3 fixed magnets
*/
import java.io.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.Scanner;        // For the Scanner class
import java.text.DecimalFormat;  // For the DecimalFormat class
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;  // For the JOptionPane class

public class magneticPendulumStudents
{
    ///////////////////////////////////////////////////////////////////////////////////////////////
    // 1. Declare constants
    ///////////////////////////////////////////////////////////////////////////////////////////////

    public static final double DT = 1.e-3;    //time interval in s
    public static final double Tmax = 50.;    //max time in s

    // Parameters of the equations
    public static final double k = 0.5;    	// spring constant N/m/kg
    public static final double b = 0.15;		// drag coefficient Ns/m/kg
    public static final double z = 0.25;	// vertical distance between the pendulum and the magnets m
    public static final double J = 1.00;	// strength of the magnets N m^2/kg

    // Magnet location
    public static final double[] Mx = {1,-0.5, -0.5};	//Position of magnets in x (m).
    public static final double[] My = {0,Math.sqrt(3.)/2., -Math.sqrt(3.)/2.};	//Position of magnets in y (m).


    // Start main method
    public static void main(String[] args)
    {

		///////////////////////////////////////////////////////////////////////////////////
		// 2. Open file to write the data
		///////////////////////////////////////////////////////////////////////////////////
		PrintWriter outputFile = null;
		try
		{
			outputFile = new PrintWriter(new FileOutputStream("magnetic_pendulum.txt",false));
		}
		catch(FileNotFoundException e)
		{
			System.out.println("File error.  Program aborted.");
			System.exit(0);
		}

		///////////////////////////////////////////////////////////////////////////////////
		// 3. Setting up initial conditions
		///////////////////////////////////////////////////////////////////////////////////

		double xInitial, yInitial;

		xInitial = 0.8;
		yInitial = -3.0;

		// Calculate length of arrays
		int imax = (int)(Tmax/DT);		// Maximal index

		// Declare main variables
		double[] t = new double[imax];		// time in sec

		double[] x1 = new double[imax];  // x-position in m
		double[] vx1 = new double[imax]; // x-velocity in m/s
		double[] ax1 = new double[imax];// x-acceleration in m/s/s

		double[] y1 = new double[imax];	// y-position in m
		double[] vy1 = new double[imax];// y-velocity in m/s
		double[] ay1 = new double[imax];// y-acceleration in m/s/s

		// Initialize the first step in time;
		t[0] = 0;

		x1[0] = xInitial;
		y1[0] = yInitial;

		vx1[0] = 0;
		vy1[0] = 0;

		ax1[0] = accelerationx(x1[0],y1[0],vx1[0]);
		ay1[0] = accelerationy(x1[0],y1[0],vy1[0]);

		///////////////////////////////////////////////////////////////////////////////////
		// 4. Using Verlet to calculating the trajectory
		///////////////////////////////////////////////////////////////////////////////////

		// Do one step of Euler to get value for i=1
		t[1] = t[0]+DT;

		// Mass 1
		x1[1] = x1[0] + vx1[0]*DT;
		vx1[1] = vx1[0]+ax1[0]*DT;
		ax1[1] = accelerationx(x1[1],y1[1],vx1[1]);

		y1[1] = y1[0] + vy1[0]*DT;
		vy1[1] = vy1[0]+ay1[0]*DT;
		ay1[1] = accelerationy(x1[1],y1[1],vy1[1]);

		// Starting Verlet's method
		for(int i=2;i<imax;i++)
		{
			// Update time
			t[i] = t[i-1]+DT;

			// Update position
			// TASK A.3 Update the x and y-positions of the pendulum using Verlet's method
			x1[i] = 0; //STUDENTS: TO UNCOMMENT
			y1[i] = 0; //STUDENTS: TO UNCOMMENT

			// Update velocity
			// TASK A.4 Update the x and y-velocity of the pendulum using Euler's method.
			vx1[i] = 0; //STUDENTS: TO UNCOMMENT
			vy1[i] = 0; //STUDENTS: TO UNCOMMENT

			// Update acceleration
			ax1[i] = accelerationx(x1[i],y1[i],vx1[i]);
			ay1[i] = accelerationy(x1[i],y1[i],vy1[i]);
		}

		///////////////////////////////////////////////////////////////////////////////////
		// 5. Pringing and plotting positions
		///////////////////////////////////////////////////////////////////////////////////
		for(int i=0;i<imax;i+=100) // the i+=10 means we are only printing the position every 10 steps
		{
			outputFile.println(x1[i] + "	" + y1[i]);
		}

		//Cleaning up
		outputFile.close();

		// Plotting
		PlotTrajectory.main(new String [0]);


	} // end main

	///////////////////////////////////////////////////////////////////////////////////
	// 6. Calculating acceleration in x
	// parameters: 
	//	xpos = position of pendulum in x
	// 	ypos = position of pendulum in y-acceleration
	// 	xvel = x component of velocity of the pendulum
  	///////////////////////////////////////////////////////////////////////////////////
    public static double accelerationx(double xpos, double ypos, double xvel)
    {
		double accx;

		// Spring and damping contribution
		accx = -k*xpos -b*xvel;

		// TASK A.5
		// ADD THE CONTRIBUTION OF THE THREE MAGNETS TO THE ACCELERATION IN X.

		return accx;
    }

	///////////////////////////////////////////////////////////////////////////////////
	// 7. Calculating acceleration in y
	// parameters: 
	//	xpos = position of pendulum in x
	// 	ypos = position of pendulum in y-acceleration
	// 	yvel = x component of velocity of the pendulum
	///////////////////////////////////////////////////////////////////////////////////
    public static double accelerationy(double xpos, double ypos, double yvel)
    {
		double accy;
		

		// Spring and damping contribution
		accy = -k*ypos -b*yvel;

		// TASK A.5 (again)
		// ADD THE CONTRIBUTION OF THE THREE MAGNETS TO THE ACCELERATION IN Y.

		return accy;
    }
} //class