Week 3 - Solving second order ODEs
Aim : To code the simple pendulum that will simulate the pendulum motion using Sceond order ODEs.
Objective : Write a program that will simulate the pendulum motion and to animate plot for simple Pendulum using second order ODEs.
close all
clear all
clc
% inputs
b = 0.05;
g = 9.81;
m = 0.1;
l = 1;
% inputs for 5 oscillations
theta_0 = [0;5];
tspan = linspace(0,10,200);
[t,results] = ode45(@(t,theta)ode_func(t,theta,b,g,l,m),tspan,theta_0);
% plotting for oscillations
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('plot')
ylabel('time')
theta = (results(:,1))
% starting the counter from 1
ct = 1;
% inputs for [(xo y0)(x1 y1)]
for i = 1:length(theta)
THETA1 = theta(i);
x0 = 0;
y0 = 0;
x1 = l*sin(THETA1);
y1 =-l*cos(THETA1);
% plotting inputs for [(xo y0)(x1 y1)]
figure(2)
plot([x0 x1],[y0 y1],'linewidth',3,'color','r')
hold on
% plotting simple pendulum
plot(x1,y1,'o','markersize',30)
axis([-1 1 -1.5 1.5])
hold off
M(ct) = getframe(gcf)
% adding the counter
ct = ct+1;
end
% creating video file for simple pendulum motion
movie (M)
videofile = VideoWriter('pendulum.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Conclusion : The obtained plot attached below and animated videofile for simple Pendulum motion using second order ODEs are attached in google drive shown .
https://drive.google.com/file/d/1S0So4Fs0G_Ot_1sWB0y8b1gOUatPqs3O/view?usp=sharing``