Challenge

Solving second order ODEs

AIM: Solving second order ODE of simple pendulum and create its animation using MATLAB.

THEORY: The Differential equations play a vital role in various engineering applications and science. They are extremely helpful in solving complex mathematical problems in almost every domain of Engineering, Science and Mathematics etc .Many physics problems can be easily solved using differential equations.

The oscillation of simple pendulum equation can be written in second order differential equation. With the help of the equation we can plot the graphs

  • Displacement vs time
  • Velocity vs time
  • Acceleration vs time

GOVERNING EQUARIONS & GRAPHS:

g = acceleration due to gravity

L = length of the pendulum

m = mass of the ball

b=damping coefficient

MATLAB use ODE45 command to solve the ODE. We should convert second order equation into first order equation accordingly

CODE

MAIN PROGRAMME

clear all 
close all 
clc

%inputs
b=0.05;      %damping co-efficient
l=1;         %Length of the Pendulum 
m=1;         %mass of the ball 
g=9.81;      %accleration due to gravity 

%initial vaule

%anglar displacement is zero and initial angular velocity is 3 rad/s
theta_0 =[0;3];

%time points
%total time for motion is 20 sec
t_span = linspace(0,20,600);              

%solve ODE
%ode45 to solve ODE with using ode_func

[t, results]= ode45(@(t,theta) ode_fucn(t,theta,b,g,l,m),t_span,theta_0); 
                                                                   
hold on
plot(t,results(:,1))
plot(t,results(:,2))

xlabel('Time')
ylabel('Anglar displacement & Angular velocity')

legend('Angular displacement','Angular velocity')
title('Transient behaviour of Simple Pendulum')
hold off

figure(1)
ct = 1
for i = 1:length(t_span)
    
    x0 = 0;
    y0 = 0;
    
    x1 = 1*sin(results(i,1));
    y1 = -1*cos(results(i,1));
    
   
    plot([-1 1],[0 0],'linewidth',8,'color','k')
    hold on
    plot([0 0],[0 -1.2],'linestyle','--','color','k')
    
    plot([x0 x1],[y0 y1],'linewidth',5,'color','r')
    plot(x1,y1,'marker','o','markerFacecolor','b','markersize',30)
    title('Simple Pendulum motion')
    
    axis([-1.5 1.5 -1.5 0.2])
    pause(0.03)
   
    
    M(ct)= getframe(gcf);
    hold off
    ct=ct+1;
    
end

movie(M)
Videofile= VideoWriter('Simple_Pendulam.avi','Uncompressed AVI');
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)

SUB FUNCTION

function [dtheta_dt] = ode_fucn(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

Code explanation:

  • We will be plotting the behaviour of the simple pendulum and collecting all the figures using ‘getframe’ command for animation.
  • Plotting the graph of displacement vs theta and velocity vs theta
  • Providing the necessary inputs

b= damping coefficient

m=mass of the ball

l=length of the string

g= acceleration due to gravity

  • Giving theta value in the matrix form i.e angular displacement and angular velocity.
  • Using ‘linspace’ command to create all the angles required
  • Using ‘ode45’ command to solve the first ODE with the help of the sub function.
  • So, by providing the set of inputs we got the different values of displacement and velocity.
  • Plotting the graph of the Displacement vs time and Velocity vs time
  • Using different colours for two curves to differentiate them.
  • Now creating the animation of oscillation of pendullum
  • Setting the pivot line and the thread is on.
  • We have to point the ball to the animation so we use the marker to edit the theme.
  • Setting the range of values for X and Y axis
  • Creating a new M variable to keep the present frame.
  • Creating the movie that is used to save the frame.
  • Animation is created.

ERROR:

Small spelling error in sub function leading to ode_func error

RESULTS

SNAPSHOT OF SIMPLE PENDULLUM

LINK TO VIEW THE ANIMATION OF OSCILLATION OF SIMPLE PENDULUM

https://youtu.be/wjWIdRa3MY0

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.