Solving Second Order ODE defining a pendulum
Youtube link: https://youtu.be/XlxJw6TyJ_8
pendulum motion code
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=0.1;
%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);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel(\'Plot\')
xlabel(\'time\')
%animation
figure(2)
theta = results(:,1);
ct = 1;
for i = 1:length(t_span)
% coordinates
x0 = 0;
y0 = 0;
x1 = l*sind(theta(i,1));
y1 = -l*cosd(theta(i,1));
%plotting
plot([x0 x1],[y0 y1],\'-o\',\'linewidth\',7,\'color\',\'b\');
axis([-0.1 0.1 -2 0.5])
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter(\'pendulum_motion.avi\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
ode function code
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
plot generated