Week 3 - Solving second order ODEs
AIM : SECOND ORDER ODE. ODE REPRESENTS EQUATION OF MOTION OF A SIMPLE PENDULUM WITH DAMPING.
PROGRAM :
clear all
close all
clc
b = 0.05;
g = 9.81;
l = 1;
m = 1;
% initial condition
theta_1 = [0,3];
% time points
t_spam = linspace(0,20,100);
% solve ODE
[t, results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_spam,theta_1);
plot (t,results(:,1))
hold on
plot (t,results(:,2))
ylabel('plot')
xlabel('time')
%input
theta1= results(:,1);
% COORDINATES
X0=0;
Y0=0;
L=1;
ct=1
for i=1:length(theta1)
THETA1=theta1(i)
X1=l*sin(THETA1)
Y1=-l*cos(THETA1)
figure(2)
plot([-1 1],[0 0],'linewidth',6,'color','g');
axis ([-2 2 -2 2]);
line([X0 X1],[Y0 Y1],'linewidth',2,'color','b');
hold on
plot(X0,Y0,'O','markersize',3,'markerfacecolor','y');
plot(X1,Y1,'O','markersize',20,'markerfacecolor','r');
title('Damping of simple pendulum');
legend('support rod','length of pendulum','fixed point','Bod of pendulum');
grid on;
hold off;
M(ct)=getframe(gcf)
ct=ct+1
end
movie(M)
videofile = VideoWriter('ode_main.avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
FUNCTION:
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: In our program first we solve 1st order ode after that 2nd order ode. we ctreated the equation 1 & 2 . we got theta 1 & theta 2 value. we use inputs value for b,g,l,m. we took initial condition for pendulum initial displacement 0 & initial velocity3 m/s. After we took time spam buy using linspace command. we solve ode function by using function command.we got d theta 1/dt & d theta 2/dt value. use this function in our ode . for solving ode we use ode 45 inbuilt command. we plot the figure 1. we took inputs theta1 =results(:,1) & coordinates x0 =0 & y0=0. x1 =l*sin(theta1)& y1 = -l*cos(theta1). we negative sign because we want bob in doenwarrd direction. we use plot command & plot x0 x1 & y0 y1 cordinates.we use markersize & markerfacecolor.& making video by using video command. we got our figure2. In our figure2 we show support rod, length of pendulum,fixed point & bod of pendulum by using legend.
RESULT:
FIGURE 1:

https://drive.google.com/file/d/1MA_sA3WntcsxvGAeFwukIKujYC-QTe3T/view?usp=sharing