Week 3 - Solving second order ODEs
subject:write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping
In the above equation,
g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Write a program in Matlab that will simulate the pendulum motion, just like the one shown in the start of this challenge.
use,
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
When you do this you will get the position of the pendulum as a function of time. Use this information to animate the motion of the pendulum, as shown in the below figure.

Programme:
Above commands helps to remove the variables,workspace window and command window
Inputs:
g = gravity in m/s2 ;=g=9.81;
L = length of the pendulum in m ;l=1;
m = mass of the ball in kg ;m=1;
b=damping coefficient ;b=0.05;
% intial condition
function [dtheta_dt]=ode_func(t,theta,b,g,l,m)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt=theta2;
dtheta2_dt= -(b/m)*dtheta1_dt-(g/l)*sin(theta1);
dtheta_dt= [dtheta1_dt;dtheta2_dt];
end
clear all;
close all;
clc;
b=0.05;
g=9.81;
l=1;
m=1;
ct=1
% intial condition
theta_0=[0;3];
%time points
t_span=linspace(0,20,500);
% solve ODE
[t, results]= ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('velocity')
theta2= results(:,1);
for i=1:length(theta2)
theta_x=theta2(i);
x0=0;
y0=0;
x1=l*sin(theta_x);
y1=-l*cos(theta_x);
figure(2)
plot([-1.5 1.5],[0 0],'color','k','linewidth',10);
hold on
plot([x0 x1],[y0 y1],'color','b','linewidth',4)
hold on
plot(x1,y1,'o','markers',25,'markerfacecolor','r');
hold off
grid on
axis([-1.5 1.5 -1.5 0.5]);
pause(0.0003)
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('pendulum.avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)