Project

Solving 2nd order ODE :- Simple Pendulum using MATLAB

Transient Behavior of Second Order ODE Simple Pendulum

Aim:- To write a program for stimulating transient behavior of simple pendulum and create animation of the motion.

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. By applying Newton's secont law for rotational systems, the equation of motion for the pendulum may be obtained

,

and rearranged as

.


If the amplitude of angular displacement is small enough that the small angle approximation () holds true, then the equation of motion reduces to the equation of simple harmonic motion

.


The simple harmonic solution is


with being the natural frequency of the motion.

The Second order differential equation is:-

d2y/dx2 + p*dy/dx + q*y = 0

Objectives:-

  • To write a program for second order ODE to show simple hamonic motion.
  • To show damping oscillation of simple pendulum in the form of graphical representation.
  • To animate motion of simple pendulum.

Program:-

  1. Give input such as b,g,l,m,theta_0
  2. Time interval is obtained from t_span
  3. Graph is plotted of time intervals
  4. Function is created in another file
  5. function is called in main program for creating frames of simple pendulum motion.
  6. Second order ODE is used for plotting different frames.
  7. Lines of pendulum is plotted with different colour.
  8. Video file of animation is created in AVI format by combining different frames

Main Code:-

% Program to solve second order differential equation%

% Inputs
b=0.05;
g=9.81;
l=1;
m=0.1;

% initial condition
theta_0=[0;5];

% time points
t_span=linspace(0,10,500);

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

% Plotting Graph
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')

% Iteration using for loop to create animation of Simple Pendulum
ct=1;
theta_1=results(:,1);
for i=1:length(results(:,1))
    % initial co-ordinates
    x_start=0;
    y_start=0;
    theta_2=theta_1(i);
    
    % final co-ordinates
    x_end=1*sin(theta_2);
    y_end=-1*cos(theta_2);
    
    % plotting for animartion 
    figure(2)
    plot([-1 1],[0 0],'linewidth',3,'color','black')                 %base of pendulum
    hold on
    line([x_start x_end],[y_start y_end],'linewidth',5,'color','b')  %string of pendulum
    hold on
    plot(x_end,y_end,'marker','O','markersize',26,'markerfacecolor','r')                     %bob of pendulum
    axis([-2 2 -2 2])
    pause(0.03)
    hold off
    M(ct)=getframe(gcf);
    ct=ct+1;
end

% Creating animation videofile
movie(M);
videofile=VideoWriter('Simple_Pendulum.avi','Uncompressed AVI');    % To create video file in avi format of animation
open(videofile);
writeVideo(videofile,M);
close(videofile);

Code of Function:-

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

Code Explaination:-

  • Inputs are given
  • theta_0 is matrix having value of first and second angular velocity.
  • ode45 is a solver in Matlab for time 't' and theta
  • ode_func is function for input values like(t,theta,b,l,g,m)
  • Plotting graph of angular displacement Vs angular velocity
  • Counter value one is given.
  • Plot is made showing movement of pendulum.
  • movie is used for making video.

Error Occured:-

Pendulum was not in motion/moving

Error was corrected by changing angle of cos and sin from degree to radian.

Output:-

Animated Video link given below:-

https://youtu.be/OPOZhQ6rBL8

Conclusion:-

It can be concluded that simple pendulum works on simple harmonic motion and can be shown using second order differential equation. It oscillates at comes to rest due to damping force acting on it.

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.