Project

simulation of the pendulum motion in MATLAB

Aim: to write a program in Matlab that will simulate the pendulum motion

Governing Equation :

where,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

theta is the angle made by the pendulum with the initial position.

Body:

code i) for ODE equation

b=0.05; %damping co-efficient
g=9.81; %gravitational constant
l=1; %length of the pendulum
m=0.1; %mass of the bob

%initial conditions 

theta_0= [0;5];  %0 is initial displacement and 5 is initial velocity

%time points

t_span = linspace(0,10,500);

%solve ODE
[t,results] = ode45(@(t,theta) ode_fun(t, theta,b,g,l,m),t_span,theta_0);  
%here ode_function is defined in another program

%plotting

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

fig= 1;
for i = 1:length(t)
    
    %intial position of the pendulum
    
    x0= 0;
    y0= 0;
    
    %finel positon
    
    x1 = l*sin(results(i,1));
    y1 = -1*cos(results(i,1));
    
    %plotting
    figure(2)
    %base line 
    plot([-1,1],[0,0],'linewidth',4,'color','y');
    % pendulum line
    line([x0 x1],[y0 y1],'linewidth',3,'color','r');
    hold on;
    %pendulum ball
    plot(x1,y1,'o','markersize',30,'markerfacecolor','b');
    grid on
    %o is the circular pointer which can be used to represent bob.
    axis([-2 2 -2 1]);
    pause(0.01);
    hold off
    M(fig) = getframe(gcf)
    fig= fig+1;
end
% for genarating and storing the video file in avi from at
movie(M)
videofile = VideoWriter('second_order_ODEs.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

ii) to define the function ode_fun

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

Result:

this plot shows how the displacement and velocity of the pendulum vary with respect to time.

this graph will be damped as the damping co-efficient (b) increases. Here displacement starts from zero but velocity doesn't because KE at theta = 0 is not zero i.e., velocity is not zero. At theta = max, velocity is max.

pendulum AVI:

https://drive.google.com/file/d/1YF3R5-n-tvvhVk60SnLla0Jbt84aEUPB/view?usp=sharing

with time the velocity and displacement of the pendulum motion comes down the same is shown graphically in the above plot

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.