Second order of ODE for simple pendulum
clear all
close all
clc
%Essential Inputs
b = 0.2;
g = 9.81;
l = 1.5;
m = 0.35;
%Initial condition
theta_0 = [0;5];
%time points
t_span = linspace(0,10,500);
%Solve ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%to plot the graphs according to the states mentioned
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel(\'plot\')
xlabel(\'time\')
theta2 = results(:,1);
ct = 1;
for i = 1:length(theta2);
theta3 = theta2(i);
x0 = 0;
y0 = 0;
x1 = 1*sin(theta3);
y1 = -1*cos(theta3);
figure(3)
plot([-1.1 1.1],[0 0],\'linewidth\',2);
hold on
plot([x0 x1],[y0 y1],\'color\',\'r\',\'linewidth\',2)
hold on
plot(x1,y1,\'o\',\'markers\',20,\'markerfacecolor\',\'b\')
hold off
grid on
axis([-1.5 1.5 -1.5 0.5])
pause(0.003)
M(ct) = getframe(gcf);
ct = ct+1
end
%to make a video file
movie(M)
videofile = VideoWriter(\'Pendulum.avi\',\'Uncompressed avi\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
%sub function for ode_func
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
Problems faced + solution for it :
- while creating the video file, in-appropriate use of lower case letters
- plotting axis inputs for animation of the pendulum, reffered to previous porgram of drag coeff.
- applying markers and the shape of the marker