Week 3 - Solving second order ODEs
A program to solve the following ODE

Written Script :-
script to produce daming graph:-
clear all
close all
clc
b = 0.05;
g = 9.81;
l = 1;
m = 1;
% initial conditions
theta_0 = [0;3];
% time points
t_span = linspace(0,20,100);
% solve ODE
[t, results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, theta_0);
outcome = results(:,1);
% Plotting
figure(1)
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')
Script to produce Animation:-
clear all
close all
clc
b = 0.05;
g = 9.81;
l = 1;
m = 1;
% initial conditions
theta_0 = [0;3];
% time points
t_span = linspace(0,20,100);
% solve ODE
[t, results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, theta_0);
outcome = results(:,1);
% Contour command
ct = 1;
for i = 1:length(outcome)
pos = outcome(i);
% Origin points
x0 = 0;
y0 = 0;
% max positions
x1 = l*sin(pos);
y1 = -l*cos(pos);
% plotting
plot([x0 x1],[y0 y1],x1,y1,'g.','markersize',15,'linewidth',2)
axis([-1.5 1.5 -1.5 1.5])
hold off
grid on
M(ct) = getframe(gcf);
ct = ct+1;
end
% Animation
movie(M)
videofile = VideoWriter('second_order_pendulum');
open(videofile)
writeVideo(videofile,M)
close(videofile)