Week 3 - Solving second order ODEs
AIM:- To solve the second order ODE to simulate the motion of a simple pendulum & create a YouTube animation video of its motion.
The equation of motion of a pendulum is as follows :-
PROCEDURE:-
1.First we create a function to solve the second order ODE.
2.We have to break the second order ODE into first order ODE.
3.We use ode45 solver available in MATLAB to solve ODE function.
[t,y]=ode45(odefun,tspan,y0), where tspan=[t0 tf],integrates the system of differential equations y’=f(t,y) from t0 to tf with initial conditions y0.
Each row in the solution array y corresponds to a value returned in a column vector t.
4.Then we plot the results theta1 & theta2 against t will give a plot of displacement Vs time & velocity Vs time.
5.Then we will fix the initial coordinates as 0,0 of the originating point of the pendulum.
6.Then values of the end coordinates are varied as per the values obtained by solving the ODE through ode45.
7.Now we will use the marker command to create a ball
8.At last we will create an animation & post the video on You tube.
MATLAB CODE FOR FINDING THE SOLUTION OF ODE & MAKING A ANIMATION VIDEO
clear all
close all
clc
% Inputs
%Gravity in m/s^2
g=9.81;
% length of pendulam in metres
l=1;
%mass of the pendulam in kg
m=1;
%damping coefficient
b=0.05
%angular displacement =0
%angular velocity=3 rad/sec
% length of string = 1m
% size of ball= 0.2m
%initial condition
theta_0=[0;3];
%time points
t_span=linspace(0,20,300);
%solving ODE
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('Plot')
xlabel('Time')
legend('Time Vs Displacement','Time Vs Velocity')
angle=results(:,1);
ct=1
for i=1:length(angle)
beta=angle(i)
x0=0;
y0=0;
x1=-l*sin(beta);
y1=-l*cos(beta);
figure(2)
plot([x0 x1],[y0 y1],'-o','MarkerSize',10,'MarkerEdgeColor','red',’MarkerSize’,20)
axis([-1 1 -1.3 0])
M(ct)=getframe(gcf)
ct=ct+1
end
Movie(M)
videofile=VideoWriter('Motion_of_a_pendulam.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
THE FUNCTION
function [dtheta_dt]=ode_func(t,theta,b,g,l,m)
theta1=theta(1)
theta2=theta(2)
dtheta_dt=theta2;
dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt=[dtheta_dt;dtheta2_dt]
end
Plot showing "Magnitude of Displacement & Velocity" wrt Time

Animation Screenshot of motion of Pendulam

Error in command of ode 45,( was missed before the function call in the command which was corrected using doc ode45

Error in writevideo command,'writeVideo'was corrected by using doc writevideo command in command window

YOUTUBE VIDEO LINK FOR PENDULAM ANIMATION