Project

Simple Pendulum Animation using MATLAB Second order ODE

Video link: https://youtu.be/mNJAYy9tsPw

The second-order ODE of Simple Pendulum is as folows:

`(d^2theta)/dt^2+b/m.(d theta)/dt+g/lsintheta=0rarreq1`

Where,

b - damping coefficient (kg/sec)

m - mass of bob (kg)

g - acceleration due to gravity (m/sec^2)

l - length of string (m)

For solution, we need to convert the above second order equation into two first order equations, which is as follows:

`theta=theta_1rarreq2`

`(d theta)/dt=(d theta_1)/dt=theta_2rarreq3`

`(d^2theta)/dt^2=(d^2theta_1)/dt^2=d/dt((d theta_1)/dt)=d/dt (theta_2)rarreq4`

substituting equations 2,3 and 4 in equation 1,

`eq1rArr(d theta_2)/dt+b/mtheta_2+g/lsintheta_1=0rarreq5`

from equations 3 and 5, we get,

`(d theta_1)/dt=theta_2rarreq6`

`(d theta_2)/dt=-b/mtheta_2-g/lsintheta_1rarreq7`

equations 6 and 7 togeather can be represented in matrix form as:

`d/dt[[theta_1],[theta_2]]=[[theta_2],[-b/mtheta_2-g/lsintheta_1]]rarreq8`

The required ODE function is as follows:

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

The assumed conditions for this challenge are as follows:

b = 0.05

m = 1

g = 9.81

l = 1

The time duration taken is 20 seconds.

The initial conditions are:

Angular displacement = 0 and angular velocity = 3 rad/sec at time t=0.

The main programme is as follows:

clear all
close all
clc
%inputs

b = 0.05;           %damping coefficient
g = 9.81;           %acceleration due to gravity
l = 1;              %length of string
m = 1;              %mass of bob

%initial condition
theta_0 = [0;3];

%time points
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
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel(\'time (s)\')
ylabel(\'Displacement & Angular Velocity\')
legend(\'Displacement\',\'Angular Velocity\')
grid on;
hold off

%simulation
ct= 1;
for i= 1:length(results(:,1))
    x0= 0;
    y0= 0;
    x1= -l*sin(results(i,1));
    y1= -l*cos(results(i,1));
    
    plot([-0.25,0.25],[0,0],\'linewidth\',2,\'color\',\'b\');
    line([x0 x1],[y0 y1],\'linewidth\',1,\'color\',\'r\');
    hold on;
    
    plot(x1,y1,\'O\',\'markers\',25,\'markerfacecolor\',\'g\');
    grid on;
    axis([-1.5 1.5 -1.5 1.5]);
    pause(0.01);
    hold off;
    M(ct)= getframe(gcf);
    saveas(gcf,sprintf(\'%d.png\',i));
    ct= ct+1;
end

%create video

movie(M)
videofile = VideoWriter(\'simple_pendulum.avi\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)

The time response is as follows:

\"Time

Please find the attachments in .zip file. Also see the Youtube 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.

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.