Project

Simulation of Pendulum motion using MATLAB

AIM:
Writing a program in Matlab to simulate the motion of a pendulum.

THEORY:
A pendulum is a weight suspended from a pivot so that it can swing freely. When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position. When released, the restoring force acting on the pendulum's mass causes it to oscillate about the equilibrium position, swinging back and forth.

FORMULA:

  • x1=l*sin(results(j,1));
  • y1=-l*cos(results(j,1));
  • theta1=theta(1);
  • theta2=theta(2);
  • dtheta1_dt=theta2;
  • dtheta2_dt=-(b/m)*theta2-(g/l)*sin(theta1);
  • dtheta_dt=[dtheta1_dt;dtheta2_dt];

All the above-mentioned formulae are obtained by converting the higher order differential equation into several first order differential equations. This is used for writing a code in Matlab.

MATLAB PROGRAMME:

Main function:

%given values
b=0.05;
g=9.81;
l=1;
m=1;
 
%initial condition
theta_0=[0;3];
 
%time points
t_span=linspace(0,20,100);
 
%Solve ODE
[t, results]=ode45(@(t, theta) ode_func(t, theta, b, g, l, m), t_span, theta_0);
 
figure(1)
hold on
plot(t,results(:,1),'color','r');
plot(t,results(:,2),'color','b');
hold off
xlabel('Time');
ylabel('Plots');
legend('Angular displacement', 'Angular Velocity');
 
figure(2)
ct=1;
for j=1:length(results(:,1))
        x0=0;
        y0=0;
        x1=l*sin(results(j,1));
        y1=-l*cos(results(j,1));
        plot([x0,x1],[y0,y1]);
        axis([-1.5 1.5 -1.5 1.5]);
        hold on
        plot([x0,x1],[y0,y1],'linewidth',2.5,'color','b');
        plot(x1,y1,'o', 'Markersize', 15, 'Markerfacecolor','r','markeredgecolor','r');
        plot([-1 1],[0 0],'linewidth',3.5,'color','black');
        hold off
        P(ct)=getframe(gcf);
        ct=ct+1;
end
 
movie(P);
videofile=VideoWriter('pendulum.avi','uncompressed AVI');
open(videofile);
writeVideo(videofile,P);
close(videofile);

Side 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

The side function is basically a function describing the formulas which were obtained by using the runge kutte method. This function is called in the main function to solve the differential equations.

RESULTS:

The plot for angular dispacement and angular velocity vs time is shown below

The animation of the pendulum simulation can be found here: https://www.youtube.com/watch?v=LboHq9ZgRzQ&feature=youtu.be

DISCUSSION OF RESULTS/CONCLUSION:

It is seen that with respect to changes in the time the position of the pendulum is also changing, since the angular displacement is varying and is completely dependent on time. We can clearly see that the angular displacement is not 0 after 20 seconds which means the pendulum is still moving. If we can increase the damping co-efficient, we can notice that the ang displacement comes to zero at a very fast rate. Thus, by using the above code the motion of a simple pendulum is achieved.

ERRORS:

In the above picture we can clearly see that all the figures are being displayed one on the other. This is because we didn’t use the hold off command after using the hold on command so there was an error and later it was rectified.

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.