Week 3 - Solving second order ODEs
AIM
TO determine the oridenary differntial equation and to create the pendulam animation
FORMULA USED
ODE_FUNCTION EXPLANATION
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/1)*sin(theta1);
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end
BODY OF THE CONTENT
- we start with clear all close all and clc command
- and finding the intial condition theta_0 = [0;3]
- and t_span = linspace of 0 to 500 into 20 values
- then solve the ode [t,results] by using ode_function(t,theta,b,g,l,m)
- we make a plot using plot command (t,results)
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=0.1;
t = 0
% initial condition
theta_0 = [0;3];
% time points
t_span = linspace(0,20,500);
% solve ODE
[t, results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, theta_0);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')

ct = 1
theta = results(:,1)
for i = 1:length(theta)
x1 = 0
y1 = 2
x2 = -1*sin(theta(i))
y2 = -1*cos(theta(i))
figure(2)
plot([-1.5,1.5],[2,2])
hold on
plot([x1,x2],[y1,y2])
plot(x2,y2,'o',MarkerSize = 30,MarkerFaceColor='black',MarkerEdgeColor='black')
hold off
xlim([-2.5,2.5])
ylim([-2.5,2.5])
M(ct) = getframe(gcf);
ct = ct+1;
end
%creating animation
movie(M)
vediofile = VideoWriter('pendulum animation','Uncompressed AVI');
open(vediofile)
writeVideo(vediofile,M)
close(vediofile)