Week 3 - Solving second order ODEs
the challenge is to write a program to simulate the motion of the pendulum
which has initial condition of at time t=0,angular velocity=3,with a time period of(0,20)sec
the initial conditions are
length (l)=1
mass(m)=1
gravity constant earth (9.81)
damping coefficient=0.05
clear all
close all
clc
%to find the motion of pendulum with respect to time
%input b=damping coefficient ,g=grvity constant, l=length of the string,
%m=pendulum mass,d=ball diameter
b=0.05;
g=9.81;
l=1;
m=1;
theta_0=[0;3];
p_swing=linspace(0,20,80);
[t,results]=ode45(@(t,theta)swing_function(t,theta,b,g,l,m),p_swing,theta_0);
ct=1
figure(1)
plot(t,results(:,1));
hold on
plot(t,results(:,2));
hold on
xlabel('time')
ylabel('plot')
theta2=results(:,1);
for i=1:length(theta2)
angle=theta2(i);
x0=0;
y0=0;
x1=1*sin(angle);
y1=-1*cos(angle);
figure(2)
plot([-1,1],[0,0])
hold on
plot([x0 x1],[y0 y1])
hold on
plot(x1,y1,'o','markers',15,'markerfacecolor','r')
hold off
axis([-1.5 1.5 -1.5 0.5])
pause(0.03)
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('pendulum_swing.avi'),('uncompressed avi');
open(videofile);
writeVideo(videofile,M)
close(videofile)
plot:fig(1) is velocity and displacement with respect o time

the function loop has to be created tp solve the second order ODE equation
function[dtheta_d1]=swing_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_d1=[dtheta1_dt;dtheta2_dt];
end
plot : fig(2) pendulum motion

errors:
the variable is results

v should be caps
video file for the pendulum animation is created and attached below