Challenge

Simulating the pendulum motion (using MATLAB)

AIM: To write a program that will simulate the pendulum motion.

use,

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.

THEORY:

The following second order 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.

CODE:

Main code:

clear all
close all
clc

%taking inputs
b=0.05;                          %damping
g=9.81;                          %acceleration due to gravity
l=1;                             %length of the rope 
m=1;                             %mass of the bob

theta0=[0;3];
tspan=linspace(0,20,500);        %span of time for the simulation of the pendulum
[t,results]=ode45(@(t,theta) odefunc(t,theta,b,g,l,m),tspan,theta0);   %calling the function ode45()

%plot for second order ODE
figure(1)
plot(t,results(:,1))             %plotting angular displacement vs time
hold on
plot(t,results(:,2))             %plotting angular velocity vs time
hold off
xlabel('Time')         
ylabel('Anguar_velocity or Angular_displacement')
title('Plot second order ODE')
legend('Angular displacement (radians)','Angular velocity (radians/sec)')

ct=1;                             %counter variable

%simulation of the simple pendulum
for i=1:length(t)
x = l*sin(results(i,1));          %x-coordinate of moving end of the rope
y = -l*cos(results(i,1));         %y-coordinate of moving end of the rope

figure(2)
plot([-0.5 0.5],[0 0],'linewidth',1,'color','k')         %plotting the fixed line
hold on
plot(x,y,'Marker','O','MarkerEdgeColor','red','MarkerFaceColor',[1 .6 .6],'MarkerSize',12)   %plotting the bob
plot([0 x],[0 y],'linewidth',1,'color','b')              %plotting the rope
plot(0,0,'-o','MarkerFaceColor','k','MarkerSize',4)      %plotting the joint b/w rope and fixed line

xlabel('x axis')
ylabel('y axis')
title('Simulation of a simple pendulum')
axis([-3 3 -3 3])
legend('fixed line','bob','rope','connection b/w rope and fixed line')

pause(0.03)                        %pausing the code
M(ct) = getframe(gcf);             % getframe() command captures the current axes on screen as a movie frame and storing it in array M
ct=ct+1;
hold off
end

movie(M)                           % movie() command plays the movie 
videofile=VideoWriter('Simulation of a simple pendulum.avi','Uncompressed AVI');     % making of video file from matlab movie 
open(videofile)                    % open() opens the specified file
writeVideo(videofile,M)            % writeVideo() writes to the video file
close(videofile)                   % close() closes the specified file

Function:

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

theta1=theta(1);
theta2=theta(2);
dtheta1_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);      %second order ODE
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end

OUTPUT:

Plot- Angular displacement/Angular velocity vs time

Youtube link (videofile):

https://youtu.be/Uyhnyx4b-VU

Google drive link (videofile):

https://drive.google.com/file/d/198dQnnfAlxPjXF7yubASYh_NeXmKGN_c/view?usp=sharing

EXPLANATION OF THE CODE AND THE OUTPUT:

In the main code, I have first taken the inputs. Then I called the function ode45() and the values returned by it are stored in the arrays ‘t’ and ‘results’ respectively. Then I plotted the graph of angular displacement/angular velocity vs time. Then comes the simulation of the simple pendulum. Here, first I calculated the coordinates of the moving end of the rope. I plotted the fixed line with black colour. I plotted the red bob at the moving end of the rope. I plotted the rope of blue colour. Then , I plotted the joining of the fixed line and the fixed end of the rope. I captured the current axes on screen as a movie frame and stored it in array M using the getframe() command. The movie() played the movie. VideoWriter() made the video file from matlab movie.

In the function ’odefunc()', I passed the parameters – t,theta,b,g,l,m. Here , I calculated the values of angular displacement and angular velocity values at different values of time.

In the output, I have made a plot showing angular displacement/angular velocity vs time where we can clearly see the damping motion of the pendulum as the Amplitude is continuously decreasing . We can see from this plot that at time t=0, angular displacement=0 rad ,angular velocity=3 rad/sec . In the videofile , we can see the simulation of the simple pendulum. The damping motion of the simple pendulum can also be noticed.

ERROR:

Error 1. The following error was displayed after the execution . The previous frames were also displayed in the videofile.

Error 1

STEPS TAKEN TO OVERCOME THE ERROR:

In the main code , I wrote the line (hold on) before the line no. 34 of the main code thus later I wrote the line (hold on) after line no. 34 as shown in the main code. Next thing , I wrote the line (hold off) before ending the for loop .This removed my error.

RESULT:

A program that will simulate the pendulum motion is created.

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.