Solving Second order ODEs To oscillate a Simple Pendulum
AIM: To solve the second order ODE and to simulate the Motion of Simple Pendulum using Matlab.
Here, We are using equation for motion of a simple pendulum with damping
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.
CODING:
Main Program:
clear all
close all
clc
%inputs
b = 0.05;
g = 9.81;
m = 1;
l = 1;
%initial conditions
theta_0 = [0;3];
%time points
t_span = linspace(0,10,800);
%solve ODE
[t,result] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t, result(:,1))
hold on
plot(t,result(:,2))
xlabel(\'time\')
ylabel(\'Angular Displacement vs Velocity\')
%simple pendulum simulation using ode
ct = 1
for i=1:length(result)
THETA = result(i,1);
%boundary conditon
x0=0;
y0=0;
x1= l*sin(THETA);
y1= -l*cos(THETA);
figure(2)
plot([-0.6 0.6],[0 0],\'linewidth\',5,\'color\',\'b\')
axis([-2 2 -2 2])
hold on
plot([x0 x1],[y0 y1],\'linewidth\',2,\'color\',\'r\')
plot(x1,y1,\'o\',\'markersize\',30,\'markerfacecolor\',\'r\',\'markeredgecolor\',\'b\')
pause(0.03)
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M);
videofile = VideoWriter(\'simple_pendulum.avi\',\'uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Function Program:
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
Explanation of code:
STEP1: Clear all,close all,clc commands are used to clear all previous files and prvious content in workspace.
STEP2: Input Parameters,
L=1 metre,
m=1 kg,
b=0.05,
g=9.81 m/s2.
STEP3: Initial conditions
theta_0 = [0;3];
angular displacement=0,
angular velocity=3 rad/sec,
STEP4: Time Points
t_span = linspace(0,10,800);
- t_span is time array
we are assuming 0s - 10s
STEP5: Solving ODE
[t,result] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
- ode45 is inbuilt solvent in matlab
- ode is function of t, theta
- ode function ode_func(t,theta,b,g,l,m)
Here we have different program for ode_function
function [dtheta_dt] = ode_func(t,theta,b,g,l,m)
`theta1 = theta(1)`
`theta2 = theta(2)`
`(d theta) /dt= theta2`
`(d theta2)/dt = -(b/m)*theta2-(g/l)*sin theta1`
`(d theta)/dt = [(d theta1)/dt; (d theta2)/dt]`
STEP6: Ploting figure1
- plot(t, result(:,1))
- hold on
- plot(t,result(:,2))
- xlabel(\'time\')
- ylabel(\'Angular Displacement vs Velocity\')
OUTPUT FOR ANGULAR DISPLACEMENT VS ANGULAR VELOCITY
STEP7: Pendulum simulation using ODE
- counter ct=1, counter is used to initialise the value
here for loop is used
- for i=1:length(result) value of i is going to change form 1 to length(result)
where as length(result) is number of values in result
- THETA=result(i,1) were THETA gets values form result(i,1)
STEP8: Boundary Conditon
- x0=0;
- y0=0;
- x1= l*sin(THETA);
- y1= -l*cos(THETA);
STEP9: Ploting figure2
- plot([-0.6 0.6],[0 0],\'linewidth\',5,\'color\',\'b\'), it is use to plot the line with line width of 5, and colour of blue.
- axis([-2 2 -2 2]), it defines the scale of plot -2 to 2 in x axis, 2 to 2 in y axis.
- hold on
- plot([x0 x1],[y0 y1],\'linewidth\',2,\'color\',\'r\')
- plot(x1,y1,\'o\',\'markersize\',30,\'markerfacecolor\',\'r\',\'markeredgecolor\',\'b\'), Marker is used to creat sphere.
- pause(0.03), pause each plot by 0.03 seconds.
- hold off
- M(ct)=getframe(gcf), it is used to frame the plot and saved as animation.
- ct=ct+1; counter+1 is used for incremental value.
STEP10: Saving the file
- movie(M);
- videofile = VideoWriter(\'ode_pendulum.avi\',\'uncompressed AVI\');
- open(videofile)
- writeVideo(videofile,M)
- close(videofile)
OUTPUT FOR SIMPLE PENDULUM
Animation file