Project

MATLAB program to create the animation of pendulum

OBJECTIVE:

1.To solve the second order differential equation for pendulum.

2.To create the animation of pendulum movement.

EQUATION FOR PENDULUM:

Where,

b is damping cofficient

m is mass in kg

g is gravity m/s^2

l is the length of string in meter

To solve second order differential equation firstly equation is converted into first order and below is the solution:

θ=θ1

(d2 θ1/dt2 )+(dθ1/dt)*b/m+(g/l)*sin⁡θ1=0

dθ1/dt= θ2

d2 θ1/dt2=dθ2/dt

dθ2/dt+(b/m)*θ2+(g/l)*sin⁡θ1=0

dθ2/dt=-(b/m)*θ2-(g/l)*sin⁡θ1

dθ1/dt=θ2

and also to craeat the animation of pendulum following equation will be used:

x = l*sinθ

y = -l*cosθ

CODE FOR CREATING FUNCTION TO SOLVE SECOND ORDER EQUATION:

function [dtheta_dt] = ode_func(t,theta,b,g,l,m)
theta1 = theta(1)
theta2  = theta(2)
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt];
end

CODE FOR CALLING THE FUNCTION TO SOLVE THE EQUATION AND CREATING ANIMATION OF PENDULUM MOVEMENT:

close all
clear all
clc

%inputs
g = 9.81;    %gravity(m/s^2)
l = 1;       %length(m)
m = 1;       %mass(kg)
b = 0.05;    %damping cofficient

%initial conditions
theta_0 = [0;3];  % displacement = 0  angular velocity = 3

%time
time_span = linspace(0,20,500);

%solving ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),time_span,theta_0)

figure(1)
plot(t,[results(:,1) results(:,2)])
xlabel('time')
ylabel('displacement & angular velocity')

%pendulum animation
theta = results(:,1);
ct = 1;
for i = 1:length(theta)
    THETA = theta(i);
    
%initial points
x0 = 0;  %fixed point of string
y0 = -0.2;

% lower ponts of pendulum string
x1 =  l*sin(THETA);
y1 = -l*cos(THETA);

figure(2);
%for ploting string
plot([x0 x1],[y0,y1],[-0.6 0.6],[-0.2,-0.2],'linewidth',4)
hold on


%for creating pendulum bob
plot(x1,y1,'o','markersize',30,'markerfacecolor','m','markeredgecolor','k')
hold off
axis([-1 1 -1.35 0]);
pause(0.03);
A(ct) = getframe(gcf);
ct = ct +1;

end

% for movie creation
movie(A);
videofile = VideoWriter('pendulum.avi','uncompressed avi');
open(videofile)
writeVideo(videofile,A)
close(videofile)



CODE EXPLANATION:

1.First of all, a function program is created in which the second order differential is broke into first order equation as shoen in the first code.

2. Another program is created to find the solution of the equation and creating pendulum animation having different steps explain below

  1. Firstly, the value of constants is defiend such as damping coffiecient , length , mass and gravity.
  2. define the initial value of displacement and angulat veocity, and time span is set.
  3. After that, solving the equation using ode45 and by calling the already created function.
  4. Plot the diaplacement and angular veloity against time.
  5. To creating the animation of pendulum 'for loop' is used and ct variable is used to creat frames.
  6. Further, fixed end and changing end of the string is made by using the trignometric equation a mentioned in starting of this project.
  7. After that, plotting the string and bob using plot command.

8. creating animation by using the getframe and movie command.

ERROR FACED:

While running this program two types of errors were tackled.

1.it was typing error while writing theta21 instead of theta2 and gerframe instead of getframe as shown below

2. This error was related to syntax error by using ' . '(dot) instead of comma in witeVideo(videofile,A) as shown below:

these errors were simply overcome as matlab is smart enough to notify the errors.

OUTPUT:

The plot of time vs displacement and angular velocity:

The link of pendulum video is given below:

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.