Project

simple pendulum

MOTION OF SIMPLE PENDULUM(ode45) USING MATLAB CODING

TO BE PERFORMED:

To write a program in Matlab to simulate transient behavior of a simple pendulum and create its animation
Transient behaviors of simple pendulum.
Animation of same.
INTRODUCTION:

  • The pendulum is a simple mechanical system that follows a differential equation.
  • The pendulum is initially at rest in a vertical position. When the pendulum is displaced by an angle θ and released, the force of gravity pulls it back towards its resting position.
  • Its momentum causes it to overshoot and come to an angle −θ (if there are no frictional forces), and so on. The restoring force along the motion of the pendulum due to gravity is −mgsinθ.

CODE:
Main:
close all
clear all
clc
%INPUTS
l= 1; %length
b= 0.05;%damping coefficient
g= 9.81%Accelaration due to gravity
m= 1; %mass of the pendulum
%counter variables
ct= 1;
%time interval
t_span = linspace(0,20,500);
%initial conditions
theta_0= [0,3];
%calling the ode function
[t,Results]=ode45(@(t,theta) ode_pendulum(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
hold on ;
plot(t, Results(:,1),\'color\',\'red\')
plot(t, Results(:,2),\'color\',\'blue\')
hold off;
xlabel(\'time(s)\');
ylabel(\'plot\');
legend(\'Angular Dispalcement\',\'Angular velocity\')
title(\'Angular displacement and Angular velocity\')
axis([0 20 -5 5])
%Animations
%creating plots which shows position of pendulum at difftrent times
figure(2)
theta_m=Results(:,1);
for i = 1:length(theta_m)
theta_y=theta_m(i);
x0 = 1;
y0 = 3;
x1 = x0+3*sin(theta_y);
y1 = y0-3*cos(theta_y);
%plotting the pendulum
plot([-1 3],[3 3],\'color\',\'r\',\'linewidth\',4);
hold on
plot([x0 x1],[y0 y1],\'color\',\'black\',\'linewidth\',3)
hold on
plot(x1,y1,\'o\',\'markers\',20,\'markerfacecolor\',\'b\')
hold off
grid on
axis ([-3 5 -3 4])
title(\'simple pendulum\')
pause(0.0012)
M(ct) = getframe(gcf);
ct =ct+1;
end
movie(M)
videofile=VideoWriter(\'simple pendulum animation.avi\',\'uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Ode Function:
function [dtheta_dt]=ode_pendulum(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

PROCEDURE:

  • Program should be started with close all clear all & clc.
  • In order to to solve 2nd order ode we first have to convert into 1st order ode and the results are stored in 2 different columns.
  • We call this function as ode 45 it is already had been defined in matlab.
  • The input variables should be defined such as time, angular displacement, angular velocity, along with other variables like l, g, b,m.
  • We can generate the graph separately for both the parameters wrt time using plot command.
  • I had used for loop to plot every position of pendulum in given time interval when ever the for loop increments the value of the pointer ,we obtain the successive displacement value of the pendulum.
  • We plot [x,y] and assign a marker as per requirement to represent the bob of the pendulum., the plot is stored in counter and then the counter is incremented and the loop is closed
  • I had used get frame command and stored in a variable named as M(ct) ,we can take output of these frame from M using the movie command and video Writer command ,so that it can be able to make a video file and played by any convenient media players.

OUTPUT:

\"\"

FIG: Output1

\"\"

output2

ERRORS:

I had faced several errors in calling ode 45 function in code but at last I rectified it by my own.

YOUTUBE LINK:

https://youtu.be/Ewc5z19C62w

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.