Week 3 - Solving second order ODEs
clear all- used for clear all the data stored in previous workspace
close all- used for colse the all the figures and plots of before program
clc-use for clear the command window
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
theta- is the angle made by the pendullam moves from staring position to extream position , when external force is applied
t_span- used for time take to complete the oscillations from 0 second to 20 seconds
for creating a movie of the result a counter variable is itroduced before the first for loop as "ct"
M % for capture the current frame for every time untill the for loop ends
ct+1 % used for increment of frame addend to the variable "ct"
movie(M) %used for cteating a movie of M
VideoWriter %command is used for write the videofile int formate of .avi
open(videofile) %is used of open that videofile
Writevideo %used for write the video file with plot frames
close(videofile) %used for clos the video file
for running the program we can us F5
clear all
close all
clc
%inputs
d=0.05;
g=9.81;
l=1;
m=0.1;
%inital condition
theta_0=[0;5];
%time points
t_span=linspace(0,20,100);
%solve ODE
[t, results]=ode45(@(t,theta) ode_func(t,theta,d,l,g,m),t_span, theta_0);
figure(1)
hold on
plot(t,results(:,1),'linewidth',5,'color','r'); % plotiing of time
plot(t,results(:,2),'linewidth',5,'Color','g'); % plotting of angular displacement
xlabel('time');
ylabel('displacement');
legend('time','angular displacement');
grid on
ct=1; % counter value staring from 1
for i=1:length(results)
x0=0; % i
y0=0;
x1=-l.*sin(results(i,1));
y1=-l.*cos(results(i,1));
figure(2)
plot([-1 1],[0 0],'LineWidth',3,'color','y');
hold on
plot([x0 x1],[y0 y1],'linewidth',5,'color','b');
plot(x0,y0,'-b','MarkerSize',2,'Color','k');
plot(x1,y1,'o','MarkerSize',25,'MarkerFaceColor','r');
legend('Oscillations of simple pendulam')
hold off
axis([-2 2 -2 2])
pause(0.03)
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('simple_pendullam.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
function[dtheta_dt]=ode_func(t,theta, d,l,g,m)
theta1=theta(1);
theta2=theta(2);
dtheta1_dt=theta2;
dtheta2_dt=-(d/m)*(theta2)-(g/l)*(sin(theta1));
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end

