Week 3 - Solving second order ODEs
MAIN CODE
% Program to describe and animate transient behaviour of a simple pendulum system using ODE at Matlab.
% inputs of simple pendulum
b=0.05;
g=9.81;
l=1;
m=1;
% initial co-ordinate of the pendulum
theta_0 = [0;3];
% To solve ODE, integrate the systen ODE to uptain solution,
t_span = linspace(0,20,500);
% Solve ODE
[t,result] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
% ploting displacement and angular velocity with flow of time
figure(1)
plot(t,result(:,1),'color','r')
hold on
plot(t,result(:,2),'color','b')
xlabel('Time in seconds')
ylabel('Displacement(rad/sec)/Angular velocity(rad/sec)')
legend('Displavement','Angular velocity')
title('Simple pendulum')
% animation of simple pendulum
ct = 1;
disp_theta = result(:,1);
for i = 1:length(disp_theta)
% initial value
x0=0;
y0=0;
theta_displacement = disp_theta(i);
x1 = l*sin(theta_displacement);
y1 = l*(-cos(theta_displacement));
% ploting and animation
figure(2)
plot([-1 1],[0 0],'linewidth',3,'color','r')
hold on
line([x0 x1],[y0 y1],'linewidth',2,'color','b')
plot(x0,y0,'marker','o','markersize',5,'markerfacecolor','r')
plot(x1,y1,'marker','o','markersize',25,'markerfacecolor','r')
axis([-1.5 1.5 -1.5 1])
pause(0.003)
hold off
M(ct) = getframe(gcf);
ct =ct+1;
end
movie(M)
videofile=VideoWriter('Simple_Pendulum.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
FUNCTION CODE
% function program for ODE input and equation solver, compute solution vector
function[dtheta_dt]=ode_func(t,theta,b,g,l,m);
% theta is solution array, which have theta1,theta2 breaking equation
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2 - (g/l)*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt];
OUTPUT SCREENSHOT
OUTPUT PLOT
OUTPUT OF ALL POINT BALL MOVEMENT
PENDULUM BALL MOVEMENT ANIMATION YOUTUBE LINK
PROJECT LINK
https://skill-lync.com/projects/motion-of-simple-pendulum-and-solving-second-order-ode