Challenge

Week 3 - Solving second order ODEs

AIM:
(1).To write a matlab program  that solves ODE which represents the equation of motion of a simple pendulum with damping.
                      d^2/dt^2 +b/m*d(theta)/dt + g/l*sin(theta) = 0
(2).To write MATLAB program that will stimulate the pendulum motions.
THEORY:
In mathematics , an ordinary differential equation ( or ODE) is a relation that contains functions of only one independent variable, and one or more of its derivatives with respect to that variable.

A simple example is Newtons second law of motion ,which leads to the differential equation.
                                     (D^2(x)/dt^2)*m = f(x)
Foe the motion of a particle of mass M. In general ,the force f depends upon the position of the particle X , and thus the unknown variable X appears on both sides of the differential equation , as is indicated in the  rotation f(x).
Second order constant coefficient differential equations can be used to model spring mass systems .
 An examination of the forces on spring mass systems results in differential equation of the form
                                     Mx + bx + kx = f(t)
Where m represents the mass ,b is coefficient of Damping force ,k is the spring constant ,and f(t) represents  any net external forces on the system.
Similarly:
The way pendulum moves depends on the Newtons second law .When this law is written down , we get a second order ordinary Differential Equation that describes the position of the “ball” w.r.t time .
                               d^2/dt^2 +b/m*d(theta)/dt + g/l*sin(theta) = 0
In the above equation ,
G = gravity in m/s^2
L = length of the pendulum im m,
M = mass of the ball in kg .
B = damping coefficient,

SOLVING 2nd ORDER ODE USING MATLAB:
A typical approach to solving higher – order ordinary differential equations is to convert them to systems of first order diffrential  equations ,and then solve those systems.

ODE of damped pendulum is 2nd order ordinary differential equations , so we have to convert it into first order ODE>
                               d^2/dt^2 +b/m*d(theta)/dt + g/l*sin(theta) = 0
In the a bove equation , 
 G = gravity in m/s^2
L = length of the pendulum im m,
M = mass of the ball in kg .
B = damping coefficient,
Theta = theta1 (Equation 1)
From Equation 1:
D(theta)/dt = d(theta1)/dt = theta 2(equation2)
D^2(theta1)/dt^2 = d/dt(d(theta1)/dt) = d(theta2)/dt (equation3)
From equation number 2,3:
D(theta1)/dt = (theta2) (equation4)
D^2(theta1)/dt^2 + b/m*d(theta1)/dt+g/l sin(theta) = 0
D(theta2)/dt + b/m*(theta2) + g/l*sin(theta1) = 0
So,
D(theta2)/dt = -b/m*(theta2) – g/l*sin(theta 1) (equation 5)

From equation number 4 and 5:
Writing equation in matrix format
d/dt[theta1,theta2] = [ (theta2 * -b/m*(theta2)-g/2sin]
After conversion use the matlab solver ode45 to solve the system .
The solver ode45 impliments the Runge-kutta(4,5) method .Such method is suited for solving ordinary differentrial equations by predictions.
[t,y] = ode45(@(t,y)odefunc(t,y),tspan(0), 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 column vector t.

OBJECTIVES:
1.	Solve ODE which represents equation motion of simple pendulum with damping with help of MATLAB code.
2.	Understanding of ode45 in matlab .
3.	Plotting the graph of variation of angular plot of for motion of pendulum between motion between 0-20sec,for angular displacement = 0,angular velocity = 3rad/sec at time t= 0.

BODY OF CONTENT:
b = 0.05;
g = 9.81;
l = 1;
m =1;

%initial condition 
theta_0 = [0;3];

%time points
t_span = linspace(0,20,150);

%solve ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

%plotting figure 1

plot(t,results(:,1))
hold on 
plot(t,results(:,2))
ylabel('plot')
xlabel('time')

grid on
hold off



%animations
ct = 1;
for i=1:length(t)
   x0 = 0;
   y0 = 0;
        x1 = l*sind(results(i,1));
        y1 = l*cosd(results(i,1));
 %plotting figure 2

        plot(x1,y1,'.','markersize',50,'color','b'); 
        line([-1 1],[0 0],'linewidth',5,'color','g');
        hold on;
        plot([x0 x1],[y0 y1],'linewidth',3,'color','r');
        axis([-2 2 -2 2])
        hold off;
        
        grid on;
        M(ct) = getframe(gcf);
        ct = ct+1;
end

movie(M);
videofile = videowriter('simple_pendulum1.avi','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);
In the above Equation the ”l*cosd(results(i,1))” the pendulum in opposite direction ,so here we can be edit some programs ,
Here the correction of programs below,
                         b = 0.05;
g = 9.81;
l = 1;
m =1;

%initial condition 
theta_0 = [0;3];

%time points
t_span = linspace(0,20,150);

%solve ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

%plotting figure 1

plot(t,results(:,1))
hold on 
plot(t,results(:,2))
ylabel('plot')
xlabel('time')

grid on
hold off



%animations
ct = 1;
for i=1:length(t)
   x0 = 0;
   y0 = 0;
        x1 = l*sind(results(i,1));
        y1 = -l*cosd(results(i,1));
 %plotting figure 2

        plot(x1,y1,'.','markersize',50,'color','b'); 
        line([-1 1],[0 0],'linewidth',5,'color','g');
        hold on;
        plot([x0 x1],[y0 y1],'linewidth',3,'color','r');
        axis([-2 2 -2 2])
        hold off;
        
        grid on;
        M(ct) = getframe(gcf);
        ct = ct+1;
end

movie(M);
videofile = videowriter('simple_pendulum1.avi','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);

CODE REGARDING ODE_FUNC;
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)*sind(theta1);
     dtheta_dt = [dtheta1_dt;dtheta2_dt];
end    


CONCLUSION:
1.	ODE has been solved using ode45 in MATLAB.
2.	Motion of simple pendulum has been animated .
3.	It was observed that as damping co-efficient increases in motion of pendulum gets damped faster.
4.	And from the above mentioned examples we can understand that damping has both advantages and disadvantages. 
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.