Week 3 - Solving second order ODEs
AIM:-
The aim of this project is to solve an ODE by using Matlab.To solve the motion of the simple pendulum.
Theory:-
A simple pendulum consists of a mass m hanging from a string of length L and fixed at a pivot point P. When displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion. The forces which are acting on the mass are shown in the figure. The tangential component of the gravitational force, mg sin θ, always acts towards the mean position θ = 0 opposite to the displacement, restoring force acting tangent to the arc.

program:-
clear all
close all
clc
%inputs
b=0.05;
g=9.81;
l=1;
m=1;
%Initial conditions
theta_0 = [0;3];
%time points
t_span = linspace(0,20,400);
%solve ode
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
fig = 1;
for i = 1:length(t)
%initial position of the pendulum
x0 = 0;
y0 = 0;
%final position
x1 = 1*sin(results(i,1));
y1 = -1*cos(results(i,1));
%plotting
figure(2)
%base line
plot([-1,1],[0,0],'linewidth','6','color','k');
%pendulum ball
line([x0 x1],[y0 y1],'linewidth','4','color','b');
hold on;
%pendulum ball
plot(x1,y1,'o','markers',25,'markerfacecolor','g');
grid on
axis([-2 2 -2 2]);
pause w(0.01);
hold off
M(ct)=getframe(gcf);
ct=ct+1;
end
%for generatimg video file in avi format
movie(M)
videofile = VideoWriter('second_order.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
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
function [dtheta_dt] = ode_func(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
Explanation:-
1) To solve an ODE an ode_func command is used which accepts the values of t ,thetha , b , m , t_span , theta_o , so a function called ode_function is declared
2)now to the main code in the starting the input variables likeb,g,l,m are declared.
3)as we are solving higher order equations the ODE needs to be split first
4)to obtain solution we need to integrate the ode which requires vlaues for time therefore the points are generathed
5)ode45 is a defaukt solver in matlab and it accepts the values ode theta .t_span is the time span and it is the initial condition consisting velocity and displacement of the array
6)the graphs are plotted and the hold command is used to generate a second plot the generated plot shows displacement and time
Conclusion:-
The governing equation for simple pendulum is solved and the results showing position and angular velocity is plotted.animation is made using matlab coding