Week 3 - Solving second order ODEs
INTRODUCTION
The simple pendulum is another mechanical system that moves in an oscillatory motion. It consists of a point mass 'm' suspended by means of light inextensible string of length L from a fixed support. The motion occurs in a vertical plane and is driven by a gravitational force.

fig 1-shows the basic movement of a simple pendulaum.

fif 2-shows the second order differential equation of a pendulum.
SAMPLE PROGRAM
%SOLVING SECOND ORDER DIFFERENTIAL EQUATION
clear all
close all
clc
%inputs
%damping cofficent=b
b=0.05;
%gravity
g=9.81;
%length of pendulum
l=1;
%mass
m=1;
%initial condition
%initial displacement=0
%initial velocity=3m/s
theta_0=[0 3];
%time_intervals
t_span=linspace(0,20,100);
%solving ODE
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
outcome=results(:,1);
%counter
ct=1;
for i=1:length(outcome)
pos=outcome(i);
%start points
x0=0;
y0=0;
%end points
x1=l*sin(pos);
y1=-1*cos(pos);
%plotting
figure(1)
plot([x0 x1],[y0 y1])
hold on
plot(x1,y1,'g.','markersize',15,'linewidth',2)
hold off
axis([-1.5 1.5 -1.5 1.5])
grid on
M(ct)=getframe(gcf);
ct=ct+1;
end
Videofile = VideoWriter('second_order_differntial.avi');
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)
%damping graph
%plotting damping
figure(2)
plot(t,results(:,1))
hold on;
xlabel('time')
plot(t,results(:,2))
hold off;
axis([0 22 -3.5 3.5])
ylabel('plot')
legend('Angular displacement/time','Angular velocity/time')
FUNCTION CALLING
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
CODE EXPLANATION
We start with clear all and close all commands
Clear all- removes data from the workspace , freeing up the system memory
Close all-deletes the current figure or the specified figure
First we start with defining the basic parameters required such as gravity, mass ,mass of the ball, damping coefficient, length of the pendulum, time interval, angular displacement and angular velocity. Then the initial conditions for solving first order differential equation is required,so we provide value for displacement and velocity, initial displacement of the pendulum is taken a 0 and initial velocity as 3 and theta_0 as [0 3].
Time interval is the time span for which our pendulum shows transient motion and generating results by using command ODE 45 which is an inbuilt function for solving differential equations outcome is nothing but the results following first column. ODE 45 command here is hyperlinked code written to solve differential equation. The same code is written above.
We use a counter command which is used to change loop irretation after every motion X is equal to zero and Y is equal to zero depicts its initial position of the pendulum bob and using trigonometry functions we can generate transient motion of the pendulum and maximum positions of the pendulum motion using the marker of or viscircles command we can attach a bob to pendulum string.
At the end an animation is required for the results of the motion of the simple pendulum so we use an animation command and make a video of the same the code of which is provided above.
RESULTS/OUTPUTS
*The graph showing angular "displacement/time" and "angular velocity/time" is provide below.
*The link for the animation of the motion of the simple pendulum is given.
https://youtu.be/QkP9P-Q1M9A