Project

oscillation of a pendulum and its simulation

AIM:

To write a program on pendulum oscillation and to create an animation of the pendulum motion.

MATLAB CODING:

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
close all
clear all
clc

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

%initial conditions
theta_0 = [0 5];

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

%solving ODE
[t , results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),time_span,theta_0);
figure(1);
plot(t,[results(:,1) results(:,2)])
ylabel(\'plot\')
xlabel(\'time\')

theta = results(:,1);
c = 1;

for i= 1:length(theta)
    Theta = theta(i);
   
    %initial points
    x0 = 0;
    y0 = 0;
    
    %movement of pendulum
    x1 = l*sin(Theta);
    y1 = -l*cos(Theta);
    
    figure (2);
    %Plotting string
    plot([x0 x1], [y0 y1],\'color\', \'g\');
    hold on
    
    %Plotting pendulum bob
    plot(x1,y1,\'O\',\'markersize\',10,\'color\',\'r\',\'MarkerFaceColor\',\'r\');
    hold off
    
    axis([-1 1 -1.5 0]);
    pause(0.002);
    M(c) = getframe(gcf);
    c = c+1;
end
%creating a movie
movie(M);
videofile = VideoWriter(\'pendulum.avi\', \'Uncompressed avi\');
open(videofile)
writeVideo(videofile,M)
close(videofile)

PROCEDURE:

STEP 1:

First of all , the preliminary commands are typed. The commands are as follows,

close all
clear all
clc

STEP 2:

Then the inputs are given.

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

STEP 3:

The initial conditions and time span are also defined

%initial conditions
theta_0 = [0 5];

The above command decribes the initial condition of the pendulum with angular displacement of 0m/s and angular velocity of 5m/s.

%time points
time_span = linspace(0,10,500)

The above command describes the time span starting from 0 to 10secs with 500 values between it.

STEP 4:

[t , results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),time_span,theta_0);

The above command solves the ODE. ode45 is a function which is already inbuilt in MATLAB.

STEP 5:

A seperate program is written to convert second order ODE into first order ODE.

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

STEP 6:

Then a graph is plotted with Xaxis as time and Yaxis as plot.

figure(1);
plot(t,[results(:,1) results(:,2)])
ylabel(\'plot\')
xlabel(\'time\')

STEP 7:

This command assigns the value of both theta and c

theta = results(:,1);
c = 1;

STEP 8:

i= 1:length(theta)
Theta = theta(i);

The above commands increments the values from 1 to length of theta and assigns theta(i) equal to Theta.

STEP 9:

%initial points
x0 = 0;
y0 = 0;

%movement of pendulum
x1 = l*sin(Theta);
y1 = -l*cos(Theta);

The above commands assigns values for x0,yo and x1,y1.

STEP 9:
figure (2);

%Plotting string
plot([x0 x1], [y0 y1],\'color\', \'g\');
hold on

%Plotting pendulum bob
plot(x1,y1,\'O\',\'markersize\',10,\'color\',\'r\',\'MarkerFaceColor\',\'r\');
hold off
The above commands plots both string and pendulum bob of oscillation of the pendulum.

STEP 10:
axis([-1 1 -1.5 0]);

pause(0.002);
M(c) = getframe(gcf);
c = c+1;

The above commands helps in allocating axis and pause time.

STEP 11:
movie(M);

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

The above commands creates an animation file to show the oscillation of the pendulum.

RESULT:

Thus , a program is written to simulate the oscillation of a pendulum and an animation file is created using MATLAB software.

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

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.