Project

Simulation of a simple pendulum using MATLAB

AIM :

To write a program to simulate the transient behaviour of a simple pendulum and to create an animation of it's motion.

THEORY :

ODE is used to describe the transient behavior of a system. The way the pendulum moves depends on the Newtons second law. When this law is written down, we get a second order Ordinay Differential Equation that describes the position of the "ball" w.r.t time. The mathematical expression for the differential equation is -

`(d^2 θ)/dt^2 + (b/m).dθ/dt + (g/l).sinθ = 0`

here,

b = damping coefficient (Ns/m)

m = mass of bob (kg)

g = gravitational acceleration (m/s^2)

l = length of pedulum string (m)

CODE :

clear all
close all
clc

% inputs
b = 0.05;                       % damping coefficient (N.s/m)
g = 9.81;                       % gravitational accn. (m/s^2)
l = 1;                          % length of string (m)
m = 1;                          % mass of ball (kg)

theta_0 = [0;3];

t_span = linspace(0,20,500);

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

x0 = 0;
y0 = 0;             

ctr = 1;
for i = 1:length(results)
    y1 = -l*cos(results(i,1));
    x1 = l*sin(results(i,1));
    
    hold off;
    plot([x0, x1],[y0, y1],'color','g','linewidth',2);
    
    hold on;
    plot(x1,y1,'o','markersize',20,'markerfacecolor','r');
    
    axis([-l*1.1 l*1.1 -l*1.5 0.5]);    
    title('Pendulum');
    xlabel('X-axis');
    ylabel('Y-axis');
    pause(0.01);
    
    M(ctr) = getframe(gcf);
    ctr = ctr + 1;
end

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

STEP-WISE EXPLANATION OF CODE :

1. clear all : clears the workspace data

close all : clears all the figures and plots

clc : clears the command window

2. The inout values are specified for b,g,l,m.

3. theta_0 represents a column matrix consisting of initial values for displacement and angular speed at t =0.

4. ode45 is an inbuilt matlab solver

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

5. Since the upper end of pendulum is fixed, it's position is assumed to be at origin.

6. The for loop is initiated to get all the individual frames of the pedulum motion. These frames are clubbed together in M to create a movie using the getframe command.

7. A videofile is created to view the animation of the pendulum.

OUTPUT :

Please refer to the below mentioned link to see the animation.

https://www.youtube.com/watch?v=yB-XKY3c0x0

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.