Project

Solving 2nd Order ODE for simple Pendulum motion using MATLAB

Introduction;

A simple pendulum consists of a mass m hanging from a string of length L and fixed at a pivot point P. When displaced to an initial angle (theta) and released, the pendulum will swing back and forth with periodic motion. By applying Newton's secont law for rotational systems, the equation of motion for the pendulum may be obtained..

L ; length of string

m ; mass of bob

c ; damping coefficient`d^2θ/dt^2+(c/m)dθ/dt+(g/L)sinθ=0`

g ; acceleration due to gravity

`d^2θ/dt^2+(c/m)dθ/dt+(g/L)sinθ=0`

The above differential equation can be solved using ODE45 solver to find the variation of angular displacement & angular velocity with respect to the time interval.

Matlab Code for solving simple pendulum ODE

clear all
close all
clc

% intial conditions.
% let d(theta)/dt = v
% y = [theta;v]

% intial vel- 3 rad/sec & angular displacement=0
y0 = [0;3];

% ctr for making the videofile frames
ctr = 0;

L=1;

%motion of pendulum - 0-20 sec
tspan = linspace(0,20,500);

% solving using ODE45 by calling the function ode_pendu
[tsol,ysol] = ode45(@(t,y) ode_pendu(t,y),tspan,y0);
plot(tsol,ysol(:,1))
hold on
plot(tsol,ysol(:,2))
xlabel('time (sec)')
ylabel('Angular Vel. & Displacement')
legend('change in displacement','change in angular velocity')

% for animation of the pendulum during the time interval 0-20sec
for i = 1:length(ysol(:,1))
x0 = 0;
y0 = 0;
a = ysol(i,1);
x1 = L*sin(a);
y1 = -L*cos(a);
figure(2)
plot([-1.1 1.1], [0,0],'linewidth',2)
hold on
plot([x0,x1],[y0,y1],'color','r','linewidth',2)
plot(x1,y1,'marker','o','MarkerSize',25,'markerfacecolor','b')
hold off
grid on
axis ([-1.5 1.5 -1.5 0.5])
pause(0.005)
ctr=ctr+1;
m(ctr) = getframe(gcf);
end

movie(m)
videofile = Videowriter('pendulum_ani.avi', 'uncompressed AVI')
open(videofile);
writeVideo(videofile,m);
close(videofile);

\\ FUNCTION

function [fval] = ode_pendu(t,y)
b = 0.05;
m = 1;
g =9.81;
L = 1;
theta = y(1);
v = y(2);
fval(1,1) = v;
fval(2,1) = -(b/m)*v-(g/L)*sin(theta);
end

OUTPUTS;

Inference; The above plot shows the variation of angular velocity & displacement for a time interval of 0-20sec for a specific value of damping coeffienct 'c'. We understand that on increasing the value of damping coefficient the pendulum comes to rest at a faster rate.

Link for Animation;

https://drive.google.com/open?id=1Mn8lrBYmSDNFQptZGocYPD1ih6qj16dp

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.