Week 3 - Solving second order ODEs
Function:
function [dtheta_dt]=ode_function(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
Program to simulate trasient behaviour of simple pendulum:
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=1;
% initial condition
theta_0=[0;3];
% time points;
t_span=linspace(0,20,500);
% solve ode
[t,results]=ode45(@(t,theta)ode_function(t,theta,b,g,l,m),t_span,theta_0);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('angular displacement vs angular velocity')
legend('angular displacement','angular velocity')
hold off
grid on
% ploting pendulum
ct=1;
for i=1:length(t_span)
x_base=0;
y_base=0;
x_end=l*sin(results(i,1));
y_end=-l*cos(results(i,1));
plot([x_base x_end],[y_base y_end],[-0.5 0.5],[0 0],'linewidth',3)
hold on
plot(x_end,y_end,'marker','o','markersize',30,'markerfacecolor','b')
axis([-1.5 1.5 -1.5 0.5])
grid on
pause(0.01)
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
% video file
movie(M);
videofile=Videowriter('ode_pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
explanation:
1.Initially given all inputs like bore,gravity ,length,mass and angular velocity.
2.Use ode45 command to solve the ode equation and call the function of ode.
3.next plot the graph of angular displacement and angular velocity.
4.Plotting figure of pendulum we use the command for creating link
x_end=l*sin(results(i,1));
y_end=-l*cos(results(i,1));
5.For plooting base the command is,
plot([x_base x_end],[y_base y_end],[-0.5 0.5],[0 0],'linewidth',3)
6.And for pendulum,
(x_end,y_end,'marker','0','markersize',30,'markerfacecolor','g' )
7.Fixing the axis x=[-1.5,1.5]
y=[-1.5,1.5].
and time interval between frame is 0.01 seconds.
8.next is ,use videowriter command for save the animation file.
Errors:

In here 'end' command is missed.And this solved by adding that command.
OUTPUT:
Plots:
Angular velocity and displacement vs time;

Pendulum:

YOUTUBE LINK:
Conclusion:
THe transient behaviour of pendulum motion with respect to time is successfully run by using ODE.