Week 3 - Solving second order ODEs
Motion of damped simple pendulum using MATLAB
This is a report based o my project which is aimed to run a code to stimulate the motion of a damped simple pendulum using matlab.
The motion of a damped simple pendulum obeys the second order differential equation
where theeta is the angle of the string frrom the mean position of string.
So, to represent the motion of simple pendlum, the ode has to solved. So a function is used to solve the above ode. And the function is exported to our code and theinbuilt function ode45 is used to get the value of theeta.
CODE:
clear all
close all
clc
%inputs
g=9.81;
b=0.05;
l=1;
m=0.1;
%contour variable
ct=1;
%initial conditions
theeta_0=[0;3]
%time_points
t_span=linspace(0,20,500);
%to solve ode
[t,results]=ode45(@(t,theeta) untitled2(t,theeta,b,g,l,m),t_span,theeta_0)
%plot
%displacement and velocity vs time
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
%to get motion of simple pendulum
x0=0;
y0=0;
a=results(:,1);
for i=1:length(t)
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1));
figure(2)
plot([-2.5 2.5],[0 0])
hold on
plot([x0 x1],[y0 y1],'linewidth',3,'color','r')
plot(x1,y1,'-o','Markersize',20,'MarkerFaceColor',[0 0.3 0.5],'MarkerEdgeColor','r')
axis([-1 1 -1 1])
pause(0.01)
hold off
M(ct) = getframe(gcf);
ct = ct+1;
end
%to save animation
movie(M)
videofile = VideoWriter('motion of pendulum3.avi','Uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
The code for the function to solve ode is
function [dtheta_dt] = untitled2(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
RESULTS:
the plot of velocity and displacement vs time is
And the figure obtined is
The youtube link for animated video ishttps://youtu.be/ipNrG--NbeA