Challenge

Week 3 - Solving second order ODEs

                Solving Second-Order ODEs for Simple pendulum

Simple Pendulum - 

It is a mechanical system that moves in an oscillatory motion. It consists of a bob mass 'm' suspended by the light inextensible string of length ' l ' from a fixed support. The motion occurs in a vertical plane and is driven by a gravitational force. 

Time taken by a pendulum to complete one full oscillation is called "Time Period" - 'T"
        i.e  T = 2π/ω0 

For every value of theta and rate of theta at which changing of a simple pendulum can be determined or solved by using the set of 'ordinary differential equation'

Ordinary Differential Equation -

An Ordinary differential equation is an equation that consists of unknown independent variables and one or more of its derivatives with respect to variables.

Order - The order of the ODE is the highest derivatives in the equation.


Second-Order ODE of Simple pendulum -

The position of theta with respect to the time of a simple pendulum can be determined by using a second-order differential equation using MATLAB. To determine first, we need to break the highest order equation to first-order equation i.e as follows,

d^2ø/dt^2 + b/m dø/dt + g/l sinø = 0     [ ø - theta]
ø = ø1                                   [Assumption]

b -  damping  coefficient

m -  mass

g -  gravity

l -  length

dø/dt  =  dø 1/dt = ø 2

d^2ø/dt^2 = d^2ø 1/dt^2= d/dt(dø 1/dt) = dø 2/dt


d^2ø 1/dt + b/mø 1+g/lsinø 1 = 0

dø 2/dt + b/mø 2 + g/l sinø 1 = 0

dø 1/dt = ø 2   &  dø 2/dt = -b/mø 2 - g/l sinø 1



d/dt[ø 1   ø 2]  =  [ø 2
 
                    [b/mø 2     -g/lsinø 1]


Program for solving second order differential equation for simple pendulum-

clear all
close all
clc

%Inputs
b = 0.05;     % damping coefficient
g = 9.81;     % gravity
l = 1;        % length of the pendulum
m = 1;        % mass of the pendulum

% Initial values
theta_0 = [0;3];

% Time span
t_span = linspace(0,20,500);

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

% Plotting
figure(1)
plot(t,results(:,1),'linewidth',2)
hold on
plot(t,results(:,2),'linewidth',2)
ylabel('Angular Velocity & Displacement')
xlabel('time in sec')

figure(2)
plot(results(:,1),results(:,2))
xlabel('Angular Displacement in rad')
ylabel('Angular Velocity - rad/sec')

theta2 = results(:,1);
ct = 1
for i = 1:length(theta2)
    theta3 = theta2(i);
    
    x0 = 0;
    y0 = 0;
    
    x1 = l*sin(theta3);
    y1 = -l*cos(theta3);
    
    figure(3)
    plot([-1 1],[0 0],'linewidth',2)
    hold on
    plot([x0 x1],[y0 y1],'linewidth',3,'color','r')
    hold on
    plot(x1,y1,'o','markers',20,'markerfacecolor','b')
    hold off
    grid on
    axis([-1.5 1.5 -1.5 0.5])
    pause(0.003)
    M(ct) = getframe(gcf)
    ct = ct +1;
end

movie(M)

Videofile = VideoWriter('Simple_pendulum.avi','Uncompressed AVI')
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)


An explanation for the program-

1. We have taken basics inputs for the program i.e Damping coefficient, gravity, length, and mass with an initial value of angular displacement and angular velocity.
2.Timespan has been taken for 0 to 20 with time steps for 500.
3. To solve the equation we used ode45 to define the unknown variables and called' Ode_function'for further solving the equation.
4.In fig(1) and fig(2) we have plotted angular displacement & velocity with respect to time and angular displacement v/s angular velocity.
5.For every value of theta2 i.e theta3(angular displacement) bob is going to change the same has been repeated using for loop and plotted in fig(3).
6.All the plot frames have been captured into a video file by creating a movie for animation of the plots.

 ​

https://drive.google.com/file/d/1rM3PCeejU90rVyVZGBK22vd9uDDqRK2K/view?usp=sharing

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.