Week 3 - Solving second order ODEs
SIMPLE PENDULUM
MAIN PROGRAM PART
clear all
close all
clc
b=0.02;
g=9.81;
l=1;
m=0.1;
% initial condition
theta_0=[0;5];
% time points
t_span=linspace(0,100,500);
% solve ODE
[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('Amplitude')
xlabel('time')
ct=1
theta_1=results(:,1);
for i=1:length(theta_1)
% angle=theta_1(i);
figure(4)
title('siple pendulum animation')
x0 = 1;
y0 = 1;
%x1 = 1*cos(((3*pi)/2)+angle);
%y1 = 1*sin(((3*pi)/2)+angle);
x1=1-l*sin(theta_1(i));
y1=1-1*cos(theta_1(i));
plot([x0 x1],[y0,y1],'linewidth',3)
hold on
plot([x1 x1],[y1 y1],'o','MarkerSize',50)
hold on
plot([0,2],[1,1],'linewidth',5)
hold off
legend('Simple Pendulum')
axis([0 2 -0.5 1.5]);
pause(0.09)
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter('pendulum_animation.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
FUNCTION PART
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
ANIMATION VIDEO LINK
EXPLAINATION PART:
Line 1 to 3
clear all ,close all and clc command is being used for clearing the screen and for avoiding the overlap of program output
line 4 to 7
input parametres of the pendulum
line 11
linspace command is used for time span of oscillation of pendulum from 0 to 100 sec with 500 intervals
line 13
ode45 special command is used in accordance with the equation from the function part
line 14 and 16
plot command is used for plotting the graph showing the dapping effect and non dampping effect of pendulum
line 17 and 18
label command is being used for labelling the x and y axis
line 19
count command is provided for continuos repeatation of the iteration for making the program as an animated one.
line 21
for loop is used for repeating the values
line 25 to 30
position of the pendulum is provided
line 32 to 36
plot command is given
line 38
legend command is provided for giving the title
line 39
axis command is given for setting the limit of the graph
line 44 to 48
for making the animation in a video format
error faced
video making and animation presentaion was bit confusing but with the help of the technical support team it was sorted.