Week 3 - Solving second order ODEs
AIM:
To solve second order ordinary differential equation using matlab.
THEORY:
ODINARY DIFFERENTIAL EQUATION(ODEs)- An ordinary differential equations is a functions of containg one or more of one independent variables and derivatives of those functions.The term ordinary is used in contrast with the partial differential equation which maybe respect to the more than a one variable.
SIMPLE PENDULUM-A simple pendulum is idealized in a simple mathematical model of a pendulum.This a weight(or a bob) on the end massless cord suspended from a pivot,without fiction.When given a initial push,it will swing back and forth with a constant amplitude.Real pendulum are subjected to friction and air drag,so the amplitude of their swing will declines.

The point from which the bob is suspended is known as point of suspension.At the point the weight of the bob act vertically downwards and the tension acts vertically upwards.so, the bob comes under equllibrium position and the bob starts oscillating if even small external force is applied.
GOVERNING EQUATIONS:
θ-is made by the movable link with vertical axis
L-is length of the movable rod
m-is the mass of the bob
b-is the damping co-efficient of the system
FIXED PARAMETERS:
Length (l)=1 meter;
Mass (m)=1kg;
Accleration due to gravity (g)=9.81m/s^2;
For time peeriod =0-20sec;
Angular displacement=0;
Angular velocity=3rad/sec at time t=0;
FUNCTION CODE:
function[dtheta_dt] = ode_function(t,theta,b,g,l,m)
theta1=theta(1)
theta2=theta(2)
dtheta1_dt= theta2
dtheta2_dt= -(b/m)*theta2 - (g/1)*sin(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
EXPLANATION:
- In this program,the second order ode is simplified to the first order ode by introducing the term theta1 and theta2.
- Then theta1 and theta2 are differentiated and dtheta_dt gives the column matrix.
MAIN PROGRAM:
%second order
close all
clear all
clc
%inputs
%fixed parameters
b=0.05; %damping coefficient
g=9.81; %gravity in m/s^2
l=1; %lenght of pendulum in meter
m=1; %mass of bob in kg
%initial conditions
%initial displacement of the pendulum=0
%initial velocity=5m/s
theta_0=[0;3];
%time points
t_span=linspace(0,20,500);
%solve ode
%ODE inbuilt solver
[t, results] = ode45(@ (t,theta) ode_function(t,theta,b,g,l,m), t_span, theta_0);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
grid on
title('motion of a pendulum')
xlabel('time')
ylabel('plot')
legend('displacement','angular velocity')
grid on
hold off
displacement = results(:,1);
velocity = results(:,2);
%fixed co-ordinates
x0=1;
y0=1;
ct=1
for i= 1:length(displacement)
%co-ordinates
x1=1-l*sin(displacement(i));
y1=1-l*cos(displacement(i));
figure(2)
plot([0 2],[1 1],'linewidth',15,'color','b')
hold on
plot([1 x1],[1 y1],[x1 x1],[y1 y1],'-o','MarkerSize',25,'MarkerFaceColor','r','LineWidth',4 )
plot(x0,y0,'-o','MarkerSize',3,'MarkerFaceColor','b')
axis([0 2 -0.5 2])
M(ct)= getframe(gcf); %to change the frame of the movie
ct=ct+1;
hold off
end
movie(M) %for creatind animation video
Videofile=VideoWriter('ode_main.avi','Uncompressed AVI')
open(Videofile) %opening the while running the codes
writeVideo(Videofile,M) %for writing the videofile
close(Videofile) %for closing the videofile after the code
EXPLANATION:
- In this program,the fixed parameters are length,damping co-efficient,accleration due togravity,mass of the bob.
- Time and angle are the given initial conditions.
- In the tspan command,200 values are taken inbetween 0-20 times a period.
- Using the code ode45,the ODE function is introduced.
- Using the plot command,the velocity and displacement are plotted in y-axis against the time x-axis.
- Using the for loop command, the mass at various point and various time are marked and are plotted in the graph.
- Using a movie command, the plot are saved as animation in Uncompressed AVI file.
RESULTS:
PLOT:


ANIMATION:
LINK:
CONCLUSION:
- The graph of displacement and angular velocity is plotted in velocity v/s in Time diagram.
- Animation for the simple pendulum is created.