Professional Documents
Culture Documents
Pendulum
Pendulum
bob_mass=10;
g=-9.81;
L=2;
theta0=-%pi/6;
v0=0;
t0=0;
//No. of steps
steps=300;
//deltaT=0.1; t_max=t0+deltaT*steps;
t_max=5; deltaT=(t_max-t0)/steps;
if t_max<=t0 then
end
//Initial position
not_a_pendulum=%F;
for i=2:steps
t(i)=t(i-1)+deltaT;
V(i)=A(i-1)*deltaT+V(i-1);
W(i)=V(i)/L;
theta(i)=theta(i-1)+W(i)*deltaT;
F(i)=bob_mass*g*sin(theta(i));
A(i)=F(i)/bob_mass;
not_a_pendulum = %T;
end
end
clear i
bob_r=0.08*L;
bob_shape=bob_r*exp(%i.*linspace(0,360,20)/180*%pi);
bob_pos=zeros(20,steps);
rod_pos=zeros(1,steps);
for i=1:steps
rod_pos(i)=L*exp(%i*(-%pi/2+theta(i)));
bob_pos(:,i)=bob_shape'+rod_pos(i);
end
clear i
axes=gca();
axes.isoview="on";
axes.children(1).children.mark_style=3;
axes.children(1).children.mark_size=1;
axes.children(1).children.thickness=3;
plot2d(real(bob_pos(:,1)),imag(bob_pos(:,1)));
axes=gca();
axes.children(1).children.fill_mode="on";
axes.children(1).children.foreground=2;
axes.children(1).children.background=2;
if max(imag(bob_pos))>0 then
axes.data_bounds=[-L-bob_r,-L-1.01*bob_r;L+bob_r,max(imag(bob_pos))];
else
axes.data_bounds=[-L-bob_r,-L-1.01*bob_r;L+bob_r,bob_r];
end
//Animating the plot
disp("Duration: "+string(max(t)+deltaT-t0)+"s.");
sleep(850);
for i=2:steps
axes.children(1).children.data=[real(bob_pos(:,i)), imag(bob_pos(:,i))];
sleep(deltaT*1000)
end
clear i