Transient behaviour of a simple pendulum
%step 1
%Clearing previous workspace & sessions
clear all
close all
clc
%Step 2
%Inputs
b=0.05;
g=9.81;
l=1;
m=1;
%Step 3
%GIving Initial Conditions
theta_ic =[0;3];
%Step4
%Defining time interval with linspace command
t_span = linspace(0,10,500);
%Step 5
%Solve ODE & Calling the fucntion ode_func
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_ic);
%Step 6
figure(1)
hold on
plot(t,results(:,1),'color','r','linewidth',2)
plot(t,results(:,2),'color','b','linewidth',2)
legend('Angular displacement','Angular Velocity')
hold off
xlabel('Time')
ylabel('Plot')
%Step 7
displacement = results(:,1);
velocity = results(:,2);
ct =1;
%Step 8
for i=1:length(displacement)
%Step 9
X0 =0;
Y0=0;
X1 = 1*cos(displacement(i)-pi/2);
Y1 = 1*sin(displacement(i)-pi/2);
%Step 10
plot([-1 1],[0.75 0.75],'linewidth',17,'color','b')
hold on
%Step 11
plot([X0 X1],[Y0 Y1],'linewidth',3,'color','b');
%Step 12
plot(X1,Y1,'-o','markersize',30','markerfacecolor','r','markeredgecolor','black');
plot(X0,Y0,'-o','markersize',5','markerfacecolor','black');
axis([-1.5 1.5 -1.5 1]);
%Step 13
displ =displacement(i);
velo = velocity(i);
txt1=['Angular displacement:',num2str(displ),'rad'];
text(-0.5 ,0.75,txt1,'color','r');
txt2 =['Angular Velocity:',num2str(velo),'rad/s'];
text(-0.5,0.5,txt2,'color','b');
pause(0.002)
%Step 14
P(ct) = getframe(gcf);
ct=ct+1;
hold off
end
%Step 15
movie(P);
videofile =VideoWriter('pendulum.avi','uncompressed AVI');
open(videofile);
writeVideo(videofile,P);
close(videofile);
%Function for 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
Stepwise Explanation:
- Clearing previous workspaces and closing all plots.
- Defining given inputs.
- Giving initial conditions of angular displacement and velocity
- Defining interval with linspace command
- Calling the ode_func and solving the ODE
- Graph plotting which show variation of angular displacement and velocity.
- Defining array for angular displacement & velocity.
- Constructing the ‘for‘ loop to depict different position of the pendulum.
- Calculating the starting and ending position of the pendulum
- Creating the support for the pendulum
- Drawing the pendulum rop using co-ordinates.
- Creating the pendulum ball using the markersize & markerfacecolor command
- Writing the corresponding value of angular displacement and velocity.
- Defining the labels for the animation
- Commands for creating the animation
- Creating the ode45 function for the calculation of angular displacement and velocity.