xxxxxxxxxx
50
//Author:Pavel Kuriscak
function setup() {
createCanvas(400, 400);
ppm = 100; // Number of pixels per meter
th = 0.1; // Pendulum angle theta
v_th = 0; // Angular velocity
C = 2; // Center point
// Prague set-up according to Charles university lab http://kdt-40.karlov.mff.cuni.cz/
L = 1.637; // Length of the pendulum
g = 9.810;
dt = 1/50;
t = 0; // Current time
num_swings = -0.25; //Number of swings
period = 0;
}
function draw() {
background(220);
old_th = th; //Remember theta before calculation
t = t + dt;
a_th = -g/L*th;
v_th = v_th + a_th*dt;
th = th + v_th*dt;
xp = C - L*sin(th); // X coordinate of pendulum ball
yp = L * cos(th); // Y coordinate of pendulum ball
scale(ppm);
strokeWeight(1/ppm);
line(C,0,xp,yp);
ellipse(xp,yp,0.2,0.2);
scale(1/ppm);
textSize(20);
text("t = "+nf(t,0,2), 50,50);
if (th*old_th < 0) {
num_swings = num_swings + 0.5;
period = t/num_swings;
}
text("N = "+nf(num_swings,0,2), 50, 75);
text("Period = "+nf(period,0,3), 50, 100);
}