Author Topic: Double pendulum/board:26-100-  (Read 5816 times)

katia

• Posts: -2
Double pendulum/board:26-100-
« on: May 22, 2010, 12:49:24 am »
i have write the java program related to double pendulum but it seems that there is something wrong because i am changes the mass of either ball1 or ball2 ,the effect is not present,its actions remains the same and this should not happen,so could you please help me so that my program will be better.
the program that i have write is as following:

package untitled10;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.math.*;
public class Applet6 extends Applet implements Runnable,AdjustmentListener,ActionListener
{
TextField tf1;
TextField tf2;
Scrollbar sc1;
Scrollbar sc2;
Scrollbar sc3;
Scrollbar sc4;
double l1=100; // defining all the constants
double l2=70;
double m1=5;
double m2=2;
float PI=3.141592654f;
private int i;
private double g=10;
private int r1=5;
private int r2=10;
//private double l=1;
double initial_theta1 = 30*PI/180;
double initial_dtheta1 = 0.6;
double initial_theta2 = 60*PI/180;
double initial_dtheta2 = 0.7;

//private double friction = 100;
double[] t,q,dq,theta1,theta2,dtheta1,dtheta2,momentum1,momentum2;
double h=0.01;

public void init()
{

sc1=new Scrollbar(0,10,4,0,200);
sc2=new Scrollbar(0,10,4,0,200);
sc3=new Scrollbar(0,5,10,60,180);
sc4=new Scrollbar(0,5,10,30,180);
tf1=new TextField(2);
tf2=new TextField(2);

t= new double[10000];
theta1 = new double [10000];
dtheta1 = new double [10000];
theta2 = new double [10000];
dtheta2 = new double [10000];
momentum1 = new double [10000];//derivative of angular frequency
momentum2 = new double [10000];
t1.start();
q= new double[4];
dq= new double [4];

theta1[0]=initial_theta1;
dtheta1[0]=initial_dtheta1;
theta2[0]=initial_theta2;
dtheta2[0]=initial_dtheta2;
momentum1[0]=(m1+m2)*l1*l1*dtheta1[0]+m2*l1*l2*dtheta2[0]*Math.cos(theta1[0]-theta2[0]);//initial condition
momentum2[0]=m2*l2*l2*dtheta2[0]+m2*l1*l2*dtheta1[0]*Math.cos(theta1[0]-theta2[0]);//initial condition

}
{
l1=sc1.getValue();
repaint();
l2=sc2.getValue();
repaint();
initial_theta1=sc3.getValue();
initial_theta2=sc4.getValue();
repaint();
}
public void actionPerformed(ActionEvent e){
m1=Integer.parseInt(tf1.getText());
m2=Integer.parseInt(tf2.getText());
repaint();
}

void derive() /* take the value of theta1, theta2, momentum1 and momentum 2
and gave us the values of dtheta1, dtheta2, dmomentum1 and dmomentum2 */

{
dq[0]=(l2*q[2]-l1*q[3]*Math.cos(q[0]-q[1]))/(l1*l1*l2*(m1+m2*Math.sin(q[0]-q[1])*Math.sin(q[0]-q[1])));
dq[1]=(l1*q[3]*(m1+m2)-l2*m2*q[2]*Math.cos(q[0]-q[1]))/(l1*l2*l2*m2*(m1+m2*Math.sin(q[0]-q[1])*Math.sin(q[0]-q[1])));
dq[2]=-(m1+m2)*g*l1*Math.sin(q[0])-(((q[2]*q[3]*Math.sin(q[0]-q[1]))/(l1*l2*(m1+m2*Math.sin(q[0]-q[1])*Math.sin(q[0]-q[1])))))+((l2*l2*m2*q[2]*q[2]+l1*l1*(m1+m2)*q[3]*q[3]-l1*l2*m2*q[2]*q[3]*Math.cos(q[0]-q[1]))/(2*l1*l2*l1*l2*(m1+m2*Math.sin(q[0]-q[1])*Math.sin(q[0]-q[1]))*(m1+m2*Math.sin(q[0]-q[1])*Math.sin(q[0]-q[1]))))*Math.sin(2*(q[0]-q[1]));
dq[3]=-m2*g*l2*Math.sin(q[1])+(((q[2]*q[3]*Math.sin(q[0]-q[1]))/(l1*l2*(m1+m2*Math.sin(q[0]-q[1])*Math.sin(q[0]-q[1])))))-((l2*l2*m2*q[2]*q[2]+l1*l1*(m1+m2)*q[3]*q[3]-l1*l2*m2*q[2]*q[3]*Math.cos(q[0]-q[1]))/(2*l1*l2*l1*l2*(m1+m2*Math.sin(q[0]-q[1])*Math.sin(q[0]-q[1]))*(m1+m2*Math.sin(q[0]-q[1])*Math.sin(q[0]-q[1]))))*Math.sin(2*(q[0]-q[1]));

}//end of derive

void eular (int i)
{
theta1[i+1]=theta1+h*dq[0];
theta2[i+1]=theta2+h*dq[1];
momentum1[i+1]=momentum1+h*dq[2];
momentum2[i+1]=momentum2+h*dq[3];
}// end of euler
public void run()
{
for(i=0;i<10000;i++)
{
t=i*h;
q[0]=theta1;
q[1]=theta2;
q[2]=momentum1;
q[3]=momentum2;
derive();
eular(i);
dtheta1=dq[0];
dtheta2=dq[1];

try
{  repaint();
t1.sleep(5);

}
catch(InterruptedException e ) {}
}
}

public void paint(Graphics g)
{
double x1,y1,x2,y2,x3,y3;
x1=getSize().width/2;
y1=200;
x2=(x1+l1*Math.sin(theta1));
y2=(y1+l1*Math.cos(theta1));
g.drawLine((int)x1,(int)y1,(int)x2,(int)y2);
g.setColor(Color.red);
g.fillOval((int)x2-r1,(int)y2-r1,2*r1,2*r1);
g.fillOval((int)x2-r2,(int)y2-r2,2*r2,2*r2);
x3=x1+l1*Math.sin(theta1)+l2*Math.sin(theta2);
y3=y1+l1*Math.cos(theta1)+l2*Math.cos(theta2);
//System.out.println(Math.cos(theta1)+"  "+Math.cos(theta2));
g.drawLine((int)x2,(int)y2,(int)x3,(int)y3);
g.setColor(Color.yellow);
g.fillOval((int)x3-r1,(int)y3-r1,2*r1,2*r1);
g.fillOval((int)x3-r2,(int)y3-r2,2*r2,2*r2);
}
}

Hope to here from you soon.
Best Regards.

Katia

Fu-Kwun Hwang

• Hero Member
• Posts: 3062
Re: Double pendulum/board:26-100-
« Reply #1 on: May 22, 2010, 04:45:11 pm »
The period of a pendulum does not depends on it's mass, it depends on gravity and the length of the pendulum.

Fabianz

• Newbie
• Posts: 2
Re: Double pendulum/board:26-100-
« Reply #2 on: May 09, 2018, 04:52:55 pm »
For this forum will help me have a lot of knowledge.