clear all
close all
clc
%initial values
l=1 %length of pendulum
m=1 %mass of pendulum
b=0.05 %dampimg coefficient
g=9.81 % gravity
theta_0=[0;3]; %initial values of angular displacement and velocity
t_span= linspace(0,20,200); %time limit
%using matlab inbuilt function ode45
[t results]= ode45(@(t,theta) ode_function(t,theta,b,g,l,m),t_span,theta_0);
plot(t,results(:,1)) %ploting graps between time and displacement
hold on
plot(t,results(:,2))
xlabel('plot')
ylabel('time')
ct=1
for i=1:length(results) %using for loop to get different orientations of pendulum
x1=l*sin(results(i,1));% determining co-oedinates for pendulum position
y1=l*cos(results(i,1));
%ploting co-ordinates for pendulum.
plot([-1,1],[0,0],[0,-x1],[0,-y1],'s','markers',15,'markerfacecolor','g')
line([0,-x1],[0,-y1],'linewidth',5)
axis([-2 2 -2 2]);
pause(0.04)
M(ct)=getframe(gcf) % assigning all frame into M
ct=ct+1;
end
% creating movie of M
movie(M);
videofile=VideoWriter('ode_pendulum','uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);
%function which converts second order differential equation to first order
%differentional equation.
function [dtheta_dt]=ode_function(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
%explation:
% First i have assigned initial values for t,theta,b,g,l,m, angular
% displacement and velocity.
%using ode45 inbult function or solver to solve our 2nd order
%differentional equation and assigning the values to t and results then we
%have used plot commands to plot graps between time and displacement.
%Then we have used for loop to generate co-ordinates for each values in the
%results and by using plot command we created different plots and stored
%all the frames in M.
%Creating movie of M
%Writing the function which converts 2nd order diff equation to 1st order
%diff equation,this function is not a main function but it is called in
%main function.
https://youtu.be/Gy7cfPtX0QQ