Project

Simulation of simple pendulum using Matlab

Objective:

Write a program to simulate the transient behaviour of a simple pendulum and to create an animation of it\'s motion. The ODE represents the equation of motion of a simple pendulum with damping.

The second order differential equation is as follows:

where,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

All these values will be employed as inputs.

Procedure:

1. Firstly the define the inputs to the equation ( mass of pendulum, value of g, length of string and damping cofficient)

2. Define ode_func which contains the definition of the differential equation to be solved.

3. To solve the differential equation we will use ode45 function from Matlab

4. Plot the results and create a video to capture the movement of the pendulum.

Matlab Code

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,100);

%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.03);
    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)

Results

\"\"

Link to the video

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

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.