Project

Solving ODE & Plotting motion of Simple Pendulum

  1. AIM :
  • To write a program to solve an ODE and represent an animation of pendulum motion using MATLAB.
  1. OBJECTIVE :
  • The objective of this project is to write a program that solves an Ordinary Differential Equation which represents the equation of motion of a simple pendulum with damping.
  1. FORMULA USED :

`(d^2 θ)/dt^2 + b/m * (dθ)/dt+ g/l* sin⁡θ`

In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient

4. CODE :

clear all
close all
clc

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

% Initial Equation
theta_0 = [0,3];
% Time points
t_span = linspace(0,20,500);
% Solve ode
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m), t_span,theta_0);

plot(t,results(:,1));
hold on
plot(t,results(:,2));
ylabel('displacement & Velocity')
xlabel('Time')
title('Angular velocity v/s Time')
legend('Displacement','Velocity')

% Plotting Pendulum
figure
ct = 1;
for i=1:length(t_span)
    x_0 = 0;
    y_0 = 0;
    x_1 = l*sin(results(i,1));
    y_1 = l*cos(results(i,1));
    
    plot([x_0 x_1],[y_0 y_1],'linewidth',3,'color','b');
    hold on
    plot([-0.5 0.5],[0,0],'linewidth',3,'color','r');
    hold on
    plot(x_1,y_1, 'marker','o','markerSize',30,'markerFacecolor','g');
    axis([-1.5 1.5 -1.5 0.5]);
    grid on;
    pause(0.01)
    M(ct) = getframe(gcf);
    ct = ct+1;
    hold off
end
movie(M);
videofile=VideoWriter('ode_func.avi','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);

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

5. BODY OF CONTENT :

  1. Inputs :
  • We start the program by providing some inputs such as length of pendulum (l), mass of bob (m), damping coefficient (d) and acceleration due to gravity (g).
  • Initial values of Angular displacement and velocity are also specified. The time span is taken as 0 to 20 seconds with 500 sections using linspace command.

b. Solving ODE:

  • To solve the function we use an in-built solver named ‘ode45’. It includes all the inputs like b, g, l, m, and theta.
  • It also includes a function ‘ode_func’ which is separately defined in function program.

c. Plotting :

  • We plot a graph showing the variation of displacement and velocity with respect to time. The plot contains two different curves.
  • A hold on command must be used in order to plot all points in the same figure.

d.‘For Loop’ for Pendulum Animation :

  • A counter variable ‘ct’ with a value of 1 is defined before the loop begins. It has to be incremented by 1 inside the loop command.
  • Inside the for loop we define the fixed point of pendulum as zero while the free end is define as equations = and

e. Plotting of Pendulum :

  • At first we plot the base as a straight horizontal line and then we plot the string on which the bob is left free.
  • Next we plot the pendulum bob using the marker command. We also define the size and colour of the marker.
  • We define the axis and specifies a pause of 0.01 for the animation.
  • We create a movie using the movie function.
  • Create a video file to save the file in .avi format uncompressed.
  • Write commands to open, edit & close the video file.

6. OUTPUT :

  • Variation of Displacement and velocity with respect to Time :
  • Plot of Pendulum motion :

Errors Faced :

  • Forgot to use the hold off command :
  • Error in the formula used for free end of the pendulum :

7. CONCLUSION :

  • The motion of simple pendulum is illustrated using second order ordinary differential equation in MATLAB.

8. ANIMATION VIDEO :

https://drive.google.com/file/d/1Nlul4wlKx8xFkGnpzQBzNdFiUOtTR5yT/view?usp=sharing

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.