Week 3 - Solving second order ODEs
% solvikg the second order ODE
clear all
close all
clc
% inputs
l = 1;
b = 0.05;
m = 1;
g = 9.81;
% initial condition
theta = [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);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Velovity')
legend('Displacement','Angular velocity')
grid on
hold off
displacement = results(:,1);
velocity = results(:,2);
% Coordinates
x0 = 1;
y0 = 1;
ct = 1;
for i = 1 : length(displacement)
% Coordinates
x1 = 1-l*sin(displacement(i));
y1 = 1-l*cos(displacement(i));
figure(2)
plot([0 2],[1 1],'lineWidth',15,'color','b')
hold on
plot([1 x1],[1 y1],[x1 x1],[y1 y1],'-O','markersize',15,'markerfacecolor','r','linewidth',4)
plot(x0,y0,'-O','markersize',3,'markerfacecolor','b')
axis([0 2 -0.5 2])
M(ct) = getframe(gcf);
ct=ct+1;
hold off
end
movie(M)
videofile= VideoWriter('Simple_Pendulum.avi','uncompressed AVI');
open (videofile)
writeVideo (videofile,M)
close (videofile);
1- Given inputs are defined
2- Theta value is given as a column matrix
3- Time span is defined by linspace command between 0 to 20 sec with 500 section
4- Used ode45 funtion & call the corresponding 'Ode_func' in program
6- Used plot commands to plot graph between time & velocity
7- x & y label command used to define axis
8- legend command is used to identify displacement & angular velocity
9- Defined the coordinate x0,y0 & counter
10- For loop is used to simulate simple pendulum
11- Defined the coordinate x1,y1
12- markersize command used to create the bob of the pendulum
13- Defined the scale of the axis using axis command
14- videoWriter command used to create the video file
Error faced during programming
1- An operator was missing & put the operator to rectify program
Link for the animation-
https://drive.google.com/file/d/1WVFBr2BmIES7V3O4T-RecqgF5Y3eteRS/view?usp=sharing