Challenge

Week 3 - Solving second order ODEs

AIM : 

  • To create an animation of simple pendulum by using second order differential equation by using ode solver function.
  • For simulation the second order equation of the simple pendulum is
  • To create a plot between angular velocity and displacement of simple pendulum by using ode45.

CONCEPT : 

  • For a simple pendulum with a damping factor, the equation of simple pendulum is

               where 

                        b = damping coefficient (N-m/s)

                        m = mass of the bob (kg)

                        g = gravitational acceleration (m/s)

                        l = length of the string to connect bob (m)

  • The above equation is having a differential power of order 2. To use ode45 solver should require simple differential equaton to solve the higher order differential equations.

OBJECTIVE :

  • By using ode45 solver which is an inbuilt solver in the MATLAB. The ode45 solver should require simple differential equation to solve any kind of higher order differential equation.
  • By taking constants we can solve the higher order differential equation to a simple ordinary differential equation.

         Let us consider

          Θ = Θ1  ------ (1)

          dΘ/dt = dΘ1/dt = Θ2 -----(2)

          d²Θ/dt² = dΘ2/dt ------(3)

  • By using these constants, the second order equation will become as;

              dΘ2/dt² = -b/m * Θ2 - (g/m)* sinΘ

  • The simple differential equations are written in the form of a matrix
  • By using the above equation and the ode45 solver we can animate the simple pendulum in the MATLAB by writing a code .

PROGRAM FOR SIMPLE PENDULUM :

% program for simple pendulum using Ode45.
clear all
close all
clc

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

% initial condition
theta_0 = [0;5];

% time points
t_span = linspace(0,10,500);

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

% plotting of angular velocity and displacement
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
grid on
title('motion of pendulum')
ylabel('angular velocity(rad/s)/displacement(rad)')
xlabel('time in sec')
legend('displacement','angular velocity')

angle = results(:,1);

% count command for iterations
ct = 1;

for i = 1 : length(angle)
    omega = angle(i);
    figure(2)
    title('motion of pendulum')

    % fixed coordinates of one end of simple pendulum
    x0 = 0;
    y0 = 0;

    % moving coordinates of the bob
    x1 = l*cos((3*pi/2)+omega);
    y1 = l*sin((3*pi/2)+omega);

    % plotting of the bob motion
    plot([x0 x1],[y0 y1],'linewidth',5,'color','k');
    hold on;
    plot(x1,y1,'o','markers',20,'markerfacecolor','r');
    hold on;
    plot([-3 3],[0 0],'linewidth',3,'color','b');
    hold off
    axis([-3 3 -2 2])
    grid on
    pause(0)
    M(ct) = getframe(gcf);
    ct = ct+1;
end

% to create a animation of the simple pendulum
movie(M)
videofile = Videowrter('simple_pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

ODE FUNCTION PROGRAM :

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

CODE EXPLANATION :

  • ODE45 function is which solves the simple differential equations with a certain specified command and is a function of MATLAB that solves ordinary or partial differential equations using a numerical method. In this problem the highest order is 2. So we have to take two constant values so that the equation is chnaged to simple differential equation.

OUTPUT :

  • A plot is drawn between the angular acceleration and displacement of the motion of simple pendulum.

CONCLUSION :

  • We have animated the simple pendulum using the ode45 solver where the second order is reduced to a simple differential equation where it is easy to solve the given equation.
  • The animation is made using the different points of the bob and the moton of the simple pendulum is created.

ANIMATION OF SIMPLE PENDULUM LINK :

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.