Challenge

Week 3 - Solving second order ODEs

Aim:- Write a program that solves the ODE's for a simple pendulum mathematical model and simulates the pendulum motion.

Governing equation:-

where, 

b=damping coefficient

m=mass of body

g=acceleration due to gravity 

L=length of pendulum

Simple pendulum:-

A simple pendulum is a mechanical arrangement that demonstrates periodic motion. The simple pendulum comprises a small bob of mass ‘m’ suspended by a thin string secured to a platform at its upper end of length L.

The simple pendulum is a mechanical system that sways or moves in an oscillatory motion. This motion occurs in a vertical plane and is mainly driven by the gravitational force. Interestingly, the bob that is suspended at the end of a thread is very light somewhat we can say it is even massless. The period of a simple pendulum can be made extended by increasing the length of the string while taking the measurements from the point of suspension to the middle of the bob. However, it should be noted that if the mass of the bob is changed it will the period remain unchanged. The period is influenced mainly by the position of the pendulum about Earth as the strength of a gravitational field is not uniform everywhere.

Important terms:-

  • The oscillatory motion of a simple pendulum: Oscillatory motion is defined as the to and fro motion of the pendulum in a periodic fashion and the center point of oscillation known as equilibrium position.
  • The time period of a simple pendulum: It is defined as the time taken by the pendulum to finish one full oscillation and is denoted by “T”.
  • The amplitude of simple pendulum: It is defined as the distance traveled by the pendulum from the equilibrium position to one side.
  • Length of a simple pendulum: It is defined as the distance between the point of suspension to the center of the bob and is denoted by “l”.

Assumption:-

  • There is negligible friction from the air and the system
  • The arm of the pendulum does not bend or compress and is massless
  • The pendulum swings in a perfect plane
  • Gravity remains constant

 

Workflow:-

  • Matlab can solve linear differential equations much more easily. Hence, we will convert the pendulum equation to linear form and then define it in Matlab code.
  • When we convert the pendulum's second order differential equation to linear form, we get two-equation 1-2. 

Matlab code:-

  1.  Code function of ode45 for main function:-
function[dtheta_dt]=ode_func(t,theta,b,g,l,m)

theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta(2);
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
  1. Main program:-
%Second ODE
clear all 
close all
clc

b=0.05;
g=9.81;
l=1;
m=1;

%initial condition
theta_0 = [0;5];

%times points
t_span = linspace(0,20,200);

%solve ODE
[t, results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, theta_0);

%plotting the graph
figure(1)

subplot(2,1,1)
p2=plot(t,results(:,1),'linewidth',2,'color','b')
ylabel('theta')
xlabel('time')
title('Angular Displacement(theta) v/s Time')
hold on
subplot(2,1,2)
p2=plot(t,results(:,2),'linewidth',2,'color','r')
ylabel('velocity')
xlabel('time')
title('Angular velocity v/s Time')
legend('angular displacement','angular velocity')

%pendulum simulation using ode 
theta=results (:,1);
ct=1

%creating a for loop for oscillating the pendulum 
for i=1:length (results)
    THETA=results(i,1)

%initial boundary condition 
x0=0;
y0=0;

%defining x & y coordinates
x1=1*sin(THETA);
y1=-1*cos(THETA);

  
  figure(2)

  %plot fixed link 
  plot([x0 x1],[y0 y1],'linewidth',4);
  hold on
  plot(x1,y1,'-o','markersize',30,'markeredgecolor','g','markerfacecolor',[0.6 0.6 0.6]);
  plot(x0,y0,'-*','markersize',3,'markeredgecolor','g','markerfacecolor',[0.6 0.6 0.6]);
  axis([-2 2 -2 0])
  
  M(ct)=getframe(gcf);
  ct=ct+1;
  hold off
end
movie(M)
videofile=VideoWriter('second_ode_pendulum.avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Result output:-

  1. Graph of Angular displacement v/s Time & Angular velocity v/s Time

  1. Position of pendulum:-

Annimation link:-

https://drive.google.com/file/d/1HwhISF-S3GGumYRBpEChuqepf4bOhslA/view?usp=sharing

Conclusion:-

  •  Animation of a simple pendulum 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.