Project

Program in Matlab to simulate the pendulum motion

Differential equations are governing equation of almost all mechanical mechanism, being able to solve these equations is always a plus point.

In this project I have tried to solve following second order differential equation using MATLAB, this equation is of a pendulum’s motion.

\"ode\"

This is a second order equation solving is directly will make the process complicated and difficult to debug errors. So I have converted this second order equation into simpler first order equations. This conversion happens in a separate function not in the main function which is called in the main function.

function:

function [d_theta_dt]=ode_funct(theta,b,g,l,m)
theta1=theta(1);
theta2=theta(2);
dtheta1dt=theta2;% equation 1
dtheta2dt=-(b/m)*theta2-(g/l)*sin(theta1); % equation 2
d_theta_dt=[dtheta1dt;dtheta2dt];
end

Working of program is quite simple and straight forward, since it’s a pendulum it will have a mass, it is attached to a string of certain length. The motion of pendulum is influenced by gravity and also have damping effect.

Initial values:

Gravity g=9.81

Length of string=1

Damping coefficient = 0.05

Initial displacement = 0*

Initial velocity=5*

* it has to be noted that displacement and velocity are angular not linear.

These values are passed to the function that simplifies the second order equation into 1st order, result of that function, time limit and initial condition is then passed to ode45 a matlab solver to solve the equations. The result so obtained is stored in an array.

“[t,results]=ode45(@(t_span,theta)ode_funct(theta,b,g,l,m),t_span,theta_0);”

These values are then used to plot a graph between displacements, velocity and time.

Animation of motion of pendulum if then plotted using the value of angular velocity of the pendulum.

“x1=l*sin(results(i,1));”

“y1=l*cos(results(i,1));”

it is to be noted that in order to simulate the pendulum motion these values of x1 and y1 should be negative.

Markers keyword is used to plot bob in the animation

Once the motion is simulated using “for loop”, each frame is stored using a counter in a separate variable and using ” movie “ command an animation is created in ‘avi format’.

movie(M); % code to create a AVI format animation file

videofile=VideoWriter(\'second_ode.avi\',\'uncompressed AVI\');

code:

% code to simulate pendulum motion
clear all 
close all
clc
b=0.05;    % damping coefficient
g=9.81;    % gravity
l=1;       %length of pendulam
m=0.1;     % mass of pendulam
theta_0=[0;5];% initial values of angular displacement and velocity

t_span=linspace(0,20,200);% time limit
[t,results]=ode45(@(t_span,theta)ode_funct(t,theta,b,g,l,m),t_span,theta_0);%use of inbuilt matlab solver ode45 and calling function to simplify the equation
figure(1)
plot(t,results(:,1))        % plotting graph between time and displacement
hold on                  
plot(t,results(:,2))        % plotting graph between time and displacement
ylabel(\'plot\')
xlabel(\'time\')
ct=1;
for i = 1:length(results) % loop to obtain different orientation of pendulum
figure(2)

x1=l*sin(results(i,1)); 
y1=l*cos(results(i,1));    % calculating the coordinate for pendulum position
plot([-1,1],[0,0],[0,-x1],[0,-y1],\'o\',\'markers\',15,\'markerfacecolor\',\'g\')
line([0,-x1],[0,-y1],\'linewidth\',3)
  axis([-1.5 1.5 -1.5 1.5]);
pause(.05)
M(ct) = getframe(gcf); % storeing frames into M
ct=ct+1;
end
movie(M); % code to create a AVI format animation file 
videofile=VideoWriter(\'second_ode.avi\',\'uncompressed AVI\');
open(videofile);
writeVideo(videofile,M);
close(videofile);


output :

\"sode\"

error occoured: typing error

\"error1\"

solution : \"results\" is the correct variable

error 2: wrong code for animation

\"error

solution : using correct logic helpe me correct this also there was an exta hold on command causing multiple prints.

This project helped me understand that matlab solve higher order differential equation by first simplifying it into 1st order equation, we can even solve equation of higher order than 2 using the same methodology.

link to animation:

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.