Project

Simulation Of Pendulum and solving Second order linear differential equation

Aim:

Write a program in MATLAB that will simulate the pendulum motion.

Theory:

In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum. The way the pendulum moves depends on Newton's 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. Similarly, there are hundreds of ODEs that describe key phenomena and if you have the ability to solve these equations then you will be able to simulate any of these systems.

This ODE represents the equation of motion of a simple pendulum with damping

`(d ^2 θ )/(dt ^2 ) + b/m (dθ )/dt + g/l sinθ = 0`

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.

From the above equation, we create two ODEs’ i.e.,

`(dθ )/dt = θ2,`

`(dθ2 )/dt = -(b/m (θ2) + g/l sin⁡(θ1))`

Now we write a function code in which we are going to use some parameters that are going to use in the main program. We basically call this function in our program to solve our problem.

And, after the function code, we run the function code and call its value in the main code. And in the main code, we will first write all the inputs,

  • L=1 meter,
  • m=1 kg,
  • b=0.05.
  • g=9.81 m/s2.

Explanation of the main for solving ODE and simulation of the pendulum: -

  1. Now we are solving the ODE as mentioned above and we split that ode into two single order ODE’s one for velocity and one for displacement.
  2. So, when we need to solve the first ODE, we need to provide the initial conditions for the displacement and velocities.
  3. Since in MATLAB “ode45” is an inbuilt solver, which is going to solve the ODE system.
  4. Our ODE depends upon the time, theta and other parameters i.e.,
  • Damping coefficient (b)
  • Acceleration due to gravity (g)
  • Length (l)
  • Mass of bob (m)
  1. For simulation, we created an image for each value in the time that represents the position of the pendulum.
  2. For making bob I use the marker command. And keeping the one end of the pendulum fixed so that we can oscillate our pendulum.
  3. As I use octave for programming I use the command “text = sprint(“Pendulum%d.png”) & saveas(1, text)” using this program I capture the pendulum for each value in time.

Matlab Codes: -

% function Code
function[dtheta_dt] = odefunc(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
%main code
clear all
close all
clc
%inputs
b = 0.5;
g = 9.81;
l = 0.5;
m = 2;
%Initial Condition
theta_0 = [0;3];
% Timpe Point
time = linspace(0,20,100);
%ode solver
[t,results] = ode45(@(t,theta) odefunc(t,theta,b,g,l,m),time,theta_0);
plot(t,results(:,1));
hold on
plot(t,results(:,2));
xlabel('Positon')
ylabel('Angular Velocity')
grid on;
% Simulation
ct = 1;
for i = 1:length(results(:,1))
x0 = 0;
y0 = 0;
x1 = l*sin(results(i,1));
y1 = -l*cos(results(i,1));

plot([-0.5,0.5],[0,0], 'linewidth',3,'color','r');
line([x0 x1], [y0 y1], 'linewidth',2,'color', 'g');
hold on;

plot(x1,y1, 'o', 'markers',20,'markerfacecolor','b');
grid on
axis([-1 1 -1 1])
pause(0.01);
hold off
M(ct) = getframe(gcf);
text = sprintf("Pendulum%d.png",ct)
saveas(1,text)
ct = ct+1;
endfor

%creatinf Movie
movie(M)
videofile = videowriter('pendulum_animation.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Output Curve:
Output Video
Press here to watch my simulation video (https://youtu.be/_MxMpIyk6pQ)
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.