Project

simple pendulum animation using matlab

objective :

1. plot the displacement and velocity graph vs. time for simple pendulum .

2. create animation to see the pendulum movement .

A simple pendulum consists of a ball (point-mass) m hanging from a (massless) string of length L and fixed at a pivot point P. When displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion.

from the above figure we can see the FBD of the simple pendulum ,

Equation of motion of pendulum is given by as ,

now in matlab we applied above equation as two first order equation for that we built a function for which code is given below,

matlab code for function :

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

from above code we get following type of two first order equation of motion ,

now to plot the graph for the between velocity vs time and displacement vs time and creating animation we use the following matlab code ,

matlab code :

close all
clear all 
clc

%input values 
b = 0.02;
m = 0.1;
g = 9.81;
l = 0.75;
%intial comditions
theta_0 = [0,5]
% time span
t_span = linspace(0,20,550)

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

%plotting the result 
plot(t,results,'linewidth',2)
hold on 
plot(t,results,'--')
xlabel('time')
ylabel('velocity and displacement')
legend('displacement','velocity')
theta = results(:,1);

%creating animation using plot command 
ct = 1
for i = 1:length(theta)
    angle = theta(i);
    figure(2)
    title('simple pendulumn')
    x0 = 0;
    y0 = 0;
    x1 = l*cos(3*pi/2+angle);
    y1 = l*sin(3*pi/2+angle);
    plot([x0,x1],[y0,y1],'linewidth',2)
    hold on
    plot(x1,y1,'o','markers',20)
    hold on
    plot([-3,3],[0,0],'linewidth',3)
    hold off
    axis([-5,5,-2,2])
    pause(0.02)
    M(ct) = getframe(gcf)
   ct = ct+1;
end
    %writing and saving the animation file 
  movie(M)
  videofile = VideoWriter('simplependulumn.avi','Uncompressed AVI');
  open(videofile)
  writeVideo(videofile,M)
  close(videofile)
 

from above code first we give the input values ,

b = damping coefficcient

m = mass

g = gravitational accelration

l = length

and than we provide the initial condition for our simple pendulum here condition is given as

theta_0 = [0,5] means intial displacement =0 and intial velocity = 5 is given as our initial condition

than we use the ode45 which is inbuilt function of matlab which we use to solve our system of first order diffrential equation for that we input our function which we earlier defined cause ode45 solver only solve first order diffrential equation hence we have to create the system of two first order diffrential equation .

after solving the system of ode we will get the output as [t,results] where t is our time and results are displacement and velocity value at perticular time interval .

so to plot the result we use the plot command and after plotting we get the following result .

result :

so above is the plot for the simple pendulum angular velocity and angular displacement with respect to time with damping coeffient .

now to create animation file first define the length and the initial position foe the pendulum here to create pendulum we use 'marker' command to detrmine size and we give shape as 'o' for the bob . now for loop is used to get the location of the pendulum at each time frame here , length is changing at each angle and we ran the loop for the ,

theta = results(:,1)for entire first column means all the displacement value and we ran the for simulatation with angle = theta(i) so we get the diffrent displacement value which we input to the ,

x1 and y1 so we get the plots and we use pause command to rest each plot for selected time interval and to get all the frame we use,

getframe (gcf)

and by above method we get the simulation of our simple pendulumn motion ,

but the problem here is if we have to convert this into video file we have to get all the frame and we use movie command to create movie and than we write the video and save that to our desired folder here we use avi format to save the animation video file .

animation file link:

https://drive.google.com/drive/u/0/folders/1ZeKKOApNjBoEAfLpixjj2n7qoTVpnwkb

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.