Week 3 - Solving second order ODEs
AIM:
To write a program that solves the ODE that represents the equation of motion of a simple pendulum with damping.
To animate the motion of the pendulum,
LANGUAGE: Matlab
SOFTWARE: Matlab R2019a
SUMMARY:
Concepts & Definition
Formulas used
Code and explanation
Remarks
Conclusion
References
Concepts & Definition
An ordinary differential equation (ODE) is a differential equation containing one or more functions of one independent variable and the derivatives of those functions.The term ordinary is used in contrast with the term partial differential equation which may be with respect to more than one independent variable.
Here is the ordinary differntial equation that we will be solving:
`d^2(theta)/dt^2+b/md(theta)/dt+g/L*sin(theta)=0`
Formulas used

Consider the previous diagram
`F_g=m*g`
`x_f=l*sin(theta)`
`y_f=-l*cos(theta)`
Where `F_g=`Weigth in Newton
`x_f=`length of the projection of the pendulum on X-axis
`y_f=` length of the projection of the pendulum on y-axis
`m=`mass of the pendulum in Kg
`b=`damping coefficient
`g=`gravitation acceleration
Code and explanation
For the function:
%function of ode solving
function [dtheta_dt]= ode_func(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
Main code
close all %to clear the previous work data
clear all % to close the previous work graphs
clc % to clear the previous work command
b=0.05;
g=9.81;
l=1;
m=1;
%initial condition
theta_0=[0;5];
%time points
t_span=linspace(0,20,300);
%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))
%labelling
xlabel('time')
ylabel('Angular displacement & Angular velocity')
%pendulum oscillation
ct=1;
for i=1:length(t_span)
%THETA1=theta_0(i);
x0=0;
y0=0;
xf=l*sind(results(i,1));
yf=-l*cosd(results(i,1));
%PLOTTING or oscillation of the pendulum
figure(2)
plot([x0 xf],[y0 yf],[-0.5 0.5],[0 0],'linewidth',3)
axis([-2 2 -2 2])
hold on % to hold on the previous plot
%plotting and markering in green color
plot(xf,yf,'marker','o','markerSize',30,'markerFacecolor','g')
axis([-2 2 -2 0.5])
grid on
pause(0.001)
M(ct)=getframe(gcf);
ct=ct+1;
hold off
end
%Creation of the animation and the backup of the video
movie(M)
videofile = VideoWriter('pendulum_oscill.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Output:
Plot:

Simulation Video
Errors faced
Putting figure(2) out of for loop

So i could not get the plot and the animation separately
To overcome this, i put figure(2) in the for loop.
Remarks:
Here we have done the simulation of pendulum and it is simple but in real life it can be more complex due to other parameters of the environment but the principle is the same.
Conclusion
Differential Equation applications have significance in both academic and real life. An equation denotes the relation between two quantity or two functions or two variables or set of variables or between two functions. Differential equation denotes the relationship between a function and its derivatives, with some set of formulas. There are many examples, which signifies the use of these equations. Ordinary differential equations applications in real life are used to calculate the movement or flow of electricity, motion of an object (pendulum)
References
https://byjus.com/maths/differential-equations-applications/
https://www.toppr.com/guides/physics-formulas/pendulum-formula/
https://en.wikipedia.org/wiki/Ordinary_differential_equation