Project

A program to simulate the transient behavior of a simple pendulum and to create an animation of it s motion

AIM: Write a program to simulate the transient behaviour of a simple pendulum and to create an animation of it\'s motion.

GOVERNING EQUATION:

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

INTRODUCTION:

In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum.

Simple Pendulum consists of a mass (m) hanging from a string of length (l) and fixed at a pivot point. When displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion.

The way the pendulum moves depends on the Newtons second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the \"ball\" w.r.t time.

In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

use,

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

OBJECTIVE:

1. To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.

2.Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.

CONTENT:

(a) First we create function for ODE using function command. save as same name as that of function.

(b) avoid using clear all ,close all,clc in function program otherwise it wont execute.

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

(c) start with actual program that is animation of Simple pendulum.

%A program to simulate the transient behaviour of a simple
%pendulum and to create an animation of it\'s motion
clear all
close all
clc

%input
b =0.05; 
g =9.81;     %m/s^2
l =1;        % m
m =1;        % kg

%initial condition
theta_0 = [0;3];               % displacement for 0 to 3sec
t_span = linspace(0,10,500);

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

%plotting of simple pendulum
figure(1)                             %plot first figure 
hold on
plot(t,results(:,1),\'color\',\'r\',\'linewidth\',2)
plot(t,results(:,2),\'color\',\'b\',\'linewidth\',2)
hold off
ylabel(\'plot\')
xlabel(\'time\')
title(\'ANGULAR DISPLACEMENT VS ANGULAR VELOCITY\')
legend(\'displacement\',\'Velocity\')
grid on

%plotting second graph to provide animation
figure(2)
angular_displacement = results(:,1);
angular_velocity = results(:,2);
ct =1;

% For different position of pendulum  we use for loop 
for i = 1:length(angular_displacement)
    x0 = 0;                %starting position of pendulum
    y0 = 0;
    
    x1 = l*cos(angular_displacement(i)-pi/2);     %ending position of pendulum
    y1 = l*sin(angular_displacement(i)-pi/2);
    
    %for upper base support of pendulum
    plot([-1 1],[0.075 0.075],\'linewidth\',20,\'color\',\'r\')
    hold on
    %for hagging string of pendulum
    plot([x0,x1],[y0 y1],\'linewidth\',5,\'color\',\'k\')
    
    % creating mass of pendulum we use marker command
    plot(x1,y1,\'-o\',\'markersize\',35\',\'markerfacecolor\',\'m\',\'markeredgecolor\',\'k\')
    plot(x0,y0,\'-o\',\'markersize\',5\',\'markerfacecolor\',\'b\')
    axis([-1.5 1.5 -1.5 1]);
    
    dis = angular_displacement(i);
    vel = angular_velocity(i);
    pause(0.03);
    
    %for animation o pendulum
    M(ct) = getframe(gcf);
    ct = ct+1;
    hold off
end    
%Export the Pendulum animation movie
movie(M)
movie2avi(M,\'Simple_pendulum_motion.avi\');

(d) for Mass represent in graph we use markersize () command to define size of the ball.

(e) markerfacecolor() use to fill the ball with color.

(f) markeredgecolor() use to shoe border of ball in 2D.

output :

ERROR:

1. I forgot to give bracket \")\" in line number 38 and 39.

2. I forgot to close the for loop by END statement.

3. In my MATLAB r2010a \"VideoWriter\" command is not working due to license error.

OVERCOME:

I overcome the error by giving END and bracket in respective line.

I overcome exporting video by command movie2avi.

CONCLUSION:

we successfully learned and execute function command and use in main program.we learned marker command in precisely new way. Simulate dynamic system of pendulum.

YOUTUBE VIDEO LINK:

https://youtu.be/oEo69ZaZqJs

THANK YOU.

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.