You are on page 1of 8

Newton’s Cannonball

Thought Experiment
simulation in MATLAB
BY: PRASHANT KUMAR CHOUHAN (2019MEM2761)
MANISH KUMAR (2019MEM2759)
State Space Representation Small
Displacement of double pendulum

  

 
• x1 & x3 represent the & coordinate respectively

• Converting the above two 2nd order differential equation into four 1 st order differential equation
 

• The above four coupled 1st order ODEs have been solved using ODE45 Solver.
State Space Representation Large
Displacement of Double Pendulum

  

 
• x1 & x3 represent the & coordinate respectively

• Converting the above two 2nd order differential equation into four 1 st order differential equation
 

• The above four coupled 1st order ODEs have been solved using ODE45 Solver.
State Space Representation Double
Compound Pendulum

  

 
• x1 & x3 represent the & coordinate respectively

• Converting the above two 2nd order differential equation into four 1 st order differential equation
 

• The above four coupled 1st order ODEs have been solved using ODE45 Solver.
MATLAB PROGRAM FOR SMALL DISPLACEMENT OF DOUBLE
PENDULUM

Code Code for Animation


Function for defining differential    for solving ODE and for i=1:length(t)
Plotting of &
equations x1(i)=l*sin(x(i,1));
y1(i)=-l*cos(x(i,1));
function dxyt=odefunc(t,x)
tspan=0:0.05:15;
x2(i)=x1(i)+l*sin(x(i,3));
dxyt=zeros(4,1); % Defining a 4x1 null matrix
y0=[pi/18 0 pi/15 0]; %condition
y2(i)=(y1(i)-l*cos(x(i,3)));
g=9.81; % accln due to gravity
[t,x] = ode45(@odefunc,tspan,y0);
plot(0,0,'k.','markersize',15)
m=2; % Mass of ball
l=1;
hold on
l=1;
figure(1)
plot(x1(i),y1(i),'.','markersize',30)
k1=50;
plot(t,x(:,1)*180/pi,'b');
plot(x2(i),y2(i),'.','markersize',30)
k2=50;
hold on
plot([0 x1(i)],[0 y1(i)],'k')
dxyt(1)=x(2);
plot(t,x(:,2)*180/pi,'g');
plot(x1,y1,'r')
dxyt(3)=x(4);
axis square
plot([x1(i) x2(i)],[y1(i) y2(i)],'k')
dxyt(4)=(k1*x(1)-3*k2*(x(3)-x(1))-2*m*g*l*(x(3)-x(1)))/(m*l^2);
xlabel('time (in sec)');
plot(x2,y2,'b')
dxyt(2)=(2*k2*(x(3)-x(1))-k1*x(1)+m*g*l*(x(3)-2*x(1)))/
ylabel('theta & phi (in degrees)');
(m*l^2); hold off
legend('theta','phi')
end axis equal
title('Variation of theta & phi with time')
axis([-2.5 2.5 -2.5 1])
drawnow
MATLAB PROGRAM FOR LARGE DISPLACEMENT OF DOUBLE
PENDULUM

Function for defining differential Code


   for solving ODE and Code for Animation
equations Plotting of &

function dxyt=odefunc(t,x) tspan=0:0.05:20; % Defining time step for i=1:length(t)


dxyt=zeros(4,1); % Defining a 4x1 null matrix y0=[(pi/2) 0 (pi/2) 0]; %initial condition x1(i)=l*sin(x(i,1));
g=9.81; % accln due to gravity [t,x] = ode45(@odefunc,tspan,y0); y1(i)=-l*cos(x(i,1));
m=2; % Mass of ball l=1; x2(i)=x1(i)+l*sin(x(i,3));
k1=0; figure(1) y2(i)=(y1(i)-l*cos(x(i,3)));
k2=0; plot(t,x(:,1)*180/pi,'b'); plot(0,0,'k.','markersize',15)
l=1; hold on hold on
dxyt(1)=x(2); plot(t,x(:,2)*180/pi,'g'); plot(x1(i),y1(i),'.','markersize',30)
dxyt(3)=x(4); axis square plot(x2(i),y2(i),'.','markersize',30)
a=m*l^2*sin(x(3)-x(1))*(x(2)^2*cos(x(3)-x(1))+x(4)^2); xlabel('time (in sec)'); plot([0 x1(i)],[0 y1(i)],'k')
b=-m*g*l*(2*sin(x(1))-sin(x(3))*cos(x(3)-x(1))); ylabel('theta & phi (in degrees)'); plot(x1,y1,'r')
c=k2*(x(3)-x(1))*(1+cos(x(3)-x(1)))-k1*x(1); legend('theta','phi') plot([x1(i) x2(i)],[y1(i) y2(i)],'k')
d=m*l^2*(2-(cos(x(3)-x(1)))^2); title('Variation of theta & phi with time') plot(x2,y2,'b')
dxyt(2)=(a+b+c)/d; hold off
e=2*m*g*l*(sin(x(1))*cos(x(3)-x(1))-sin(x(3))); axis equal
f=-m*l^2*sin(x(3)-x(1))*(x(4)^2*cos(x(3)-x(1)) axis([-2.5 2.5 -2.5 2.5])
+2*x(2)^2); drawnow
g=k1*x(1)*cos(x(3)-x(1))-k2*(x(3)-x(1))*(cos(x(3)-x(1))
+2);
h=m*l^2*(2-(cos(x(3)-x(1))^2));
dxyt(4)=(e+f+g)/h;
end
MATLAB PROGRAM FOR COMPOUND DOUBLE PENDULUM

Function for defining differential Code


   for solving ODE and Code for Animation
equations Plotting of &
function dxyt=odefunc(t,x) tspan=0:0.005:10; % Defining time step for i=1:length(t)
dxyt=zeros(4,1); % Defining a 4x1 null matrix y0=[((pi/2)+(pi/1800)) 0 ((-pi/2)+(- x1(i)=l*sin(x(i,1));
g=9.81; % accln due to gravity pi/1800)) 0]; %Array containing initial y1(i)=-l*cos(x(i,1));
m=2; % Mass of ball condition x2(i)=x1(i)+l*sin(x(i,3));
k1=100; [t,x] = ode45(@odefunc,tspan,y0); y2(i)=(y1(i)-l*cos(x(i,3)));
k2=200; l=2; plot(0,0,'k.','markersize',15)
l=2;   hold on
dxyt(1)=x(2); plot(t,(180/pi)*x(:,1),'b') plot([0 x1(i)],[0 y1(i)],'k','Linewidth',3)
dxyt(3)=x(4); hold on plot(x1,y1,'r')
a=m*l*l*(sin(x(3)-x(1)))*(((3/4)*(x(2)^2)*cos(x(3)- x(1)))+(0.5*(x(4)^2)));
plot(t,(180/pi)*x(:,3),'r') plot([x1(i) x2(i)],[y1(i)
b=1.5*m*g*l*((0.5*sin(x(3))*cos(x(3)-x(1)))-sin(x(1))); legend('Theta','Phi') y2(i)],'k','Linewidth',3)
c=k2*(x(3)-x(1))*(1+(1.5*cos(x(3)-x(1)))); xlabel('Time') plot(x2,y2,'b')
d=-k1*x(1); ylabel('Theta/Phi(degree)') hold off
e=m*l*l*(((-3/4)*(cos(x(3)-x(1)))^2)+4/3); title('Variation in theta and phi with time) axis equal
dxyt(2)=(a+b+c+d)/e; axis([-5 5 -5 5])
f=m*(l^2)*sin(x(3)-x(1))*(((3/16)*x(4)^2)+((1/2)*x(2)^2)); drawnow
g=((-9/16)*m*g*l*sin(x(1))*cos(x(3)-x(1)))+(0.5*m*g*l*sin(x(3)));
h=(-3/8)*k1*x(1)*cos(x(3)-x(1));
j=k2*(x(3)-x(1))*(1+((3/8)*cos(x(3)-x(1))));
k=m*l*l*(((3/16)*(cos(x(3)-x(1)))^2)-(1/3));
dxyt(4)=(f+g+h+j)/k;
end
MATLAB PROGRAM TO SAVE ANIMATED FILE IN gif

N_count=0;
fram=0;
N_count=N_count+1;
fram=fram+1;
h=gca;
get(h,'fontSize')
set(h,'fontSize',12)
xlabel('X','fontSize',12);
ylabel('Y','fontSize',12);
title('For double compound pendulum with spring constant k1=100,k2=200 theta=90, phi=-90','fontsize',14)
fh = figure(4);
F=getframe(fh);
im = frame2im(F);
[imind,cm] = rgb2ind(im,256);
if i == 1
imwrite(imind,cm,'pendulum47.gif','gif','LoopCount',Inf,'DelayTime',.1);
else
imwrite(imind,cm,'pendulum47.gif','gif','WriteMode','append','DelayTime',.1);
end
movie(F,fram,1000)

You might also like