Week 3 - Solving second order ODEs
https://drive.google.com/file/d/1S60ZmNiD8ul9dfXCzIlFe7bKdn3-Ecla/view?usp=sharing
clear all
close all
clc
%given
b=0.05;%damping co-efficient
g=9.81; %gravity m/s^2
l=1;% length in m
m=0.1;% mass in kg and 0.1 for better movement
%initial condition,1st column expalins velocity and second column expalins
%displacement
theta_0=[0;5];
%time span , 0 to 10 with 200 intervals to get the oscillation for some
%time
t_span= linspace(0,10,200);
%solve ode, it is a common syntax for ode 45
[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')
% we assume theta as a new variable for results(:,1)
theta=results(:,1);
ct=1;
for i = 1:length(theta)
THETA=theta(i)
x0 = 0;
y0 = 0;
x1 = l*sin(THETA);
y1 = -l*cos(THETA); % from trignometric quadrants,we got -ve
plot([-0.5,0.5],[0,0],'linewidth',3,'color','r'); % this line is for upper portion to where the string is tied up
line([x0 x1],[y0 y1],'linewidth',2,'color','r');% this line is for string of the pendulum
hold on;
plot(x1,y1,'o','markers',30,'markerfacecolor','g');% this is for the bob
grid on ;
hold off; % to get the clear cut animation
axis([-2 2 -1.5 1.5]);
pause(0.03);
M(ct) = getframe(gcf);
ct = ct + 1; % increment should be placed after get frame command
end
%constant syntax for creating a movie/video
movie(M)
videofile = VideoWriter('simple_pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)