Project

ANIMATION OF A SIMPLE PENDULUM USING MATLAB

AIM: To solve second order ODE of a pendulum and create its animation.

OBJECTIVE:

  • To write a MATLAB program that solves the second order ODE of a pendulum in motion.
  • To create the animation of pendulum.

INTRODUCTION:

In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum

The way the pendulum moves depends on the Newton's 2nd law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the "ball" w.r.t time.

Similarly, there are hundreds of ODEs that describe key phenomena and if you have the ability to solve these equations then you will be able to simulate any of these systems.

This ODE represents the equation of motion of a simple pendulum with damping

In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

The following inputs are considered,

L=1 metre

m=1 kg

b=0.05

g=9.81 m/s^2

EXPLANATION OF CODE:

  • The appropriate inputs are provided as mentioned above.
  • Sub program is written and intitial conditions and time poitns are provided
  • The inbuilt ODE solver (ode45)in MATLAB is used to solve the ODE defined in sub program.
  • Now the sub function is called and using ode45 the ode is solved.
  • The graph is ploted.
  • Now for the animation part counter is set for the increment by 1 (ct=1) in every iteration of the loop.
  • Loop is used to calculate the increment of index variable (i) from initial value (1) to the maximum value of displacement.
  • The motion of pendulum is based on the ODE equation which is traced.
  • Now the results are plotted and captured to animate the links.
  • Each frame is written in a video file with avi format.

CODE:

(1) CODE for ploting:

clear all
close all
clc

b=0.05;    %damping co-efficient
g=9.81;   %gravitational acceleration
l=1;      %length of the pendulum
m=1;    %mass

%time points
theta_0=[0;5];

%initial condittions 
t_span =linspace(0,10,500);

%calling sub function ode_function to solve ODE
[t, k] = ode45(@(t,theta) ode_function(t,theta,b,l,m,g),t_span,theta_0);


%ploting 
plot(t,k(:,1))
hold on
plot(t,k(:,2))
ylabel('plot')
xlabel('time')
hold off
grid on

Output:

(2) Sub program:

%ode45 function (sub program)

function [dtheta_dt]= ode_function(t,theta,b,l,m,g)

theta1= theta(1);
theta2= theta(2);
dtheta1= theta2;
dtheta2= -(b/m)*theta2 -(g/l)*sin(theta1);

dtheta_dt = [dtheta1; dtheta2];
end

(3) CODE for creating animation:

clear all
close all
clc

b=0.05;    %damping co-efficient
g=9.81;   %gravitational acceleration
l=1;      %length of the pendulum
m=1;    %mass

%time points
theta_0=[0;5];

%initial condittions 
t_span =linspace(0,10,500);

%calling sub function ode_function to solve ODE
[t, k] = ode45(@(t,theta) ode_function(t,theta,b,l,m,g),t_span,theta_0);

%fixed coordinates
x=0;
y=-0.1;
displacement = k(:,1);
velocity= k(:,2);
ct=1;

for i = 1:length(displacement);
    
    x1=l*cos(displacement(i)-pi/2);
    y1=l*sin(displacement(i)-pi/2);
    
    
    plot([-1 1],[0 0],'linewidth',5,'color','r');
    hold on ;
    line([0 x1],[0 y1],'linewidth',2,'color','black');
    plot(x1,y1,'o','markers',20,'markerfacecolor','black');
    axis equal;
    axis([-1 1 -1 1]);
    hold off;
    title('Simple Pendulum');
    grid on ;
    M(ct)= getframe(gcf);
    ct=ct+1;
    
end
%creating movie
movie(M)
v = VideoWriter('pendulum.avi','uncompressed AVI')
open(v)
writeVideo(v,M)
close(v)

Output:

The output of the code is saved in a video file.

The animation can be seen from the following link.

https://www.youtube.com/watch?v=Qr_GlVlVySY

Errors:

(1)

Error: VideoWriter command was incorrect.

Soluiton: the error was rectified by writing correct command.

Conclusion:

Learn the solution of the Second order ODE using MATLAB and a program was successfully written to simulate the motion of a simple pendulum.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.