Project

Simulating the Motion of a simple pendulum using MATLAB

%% Code to simulate the motion of a pendulum

clear all
close all
clc

%% Inputs

b = 0.05;   
g = 9.81;
L = 1;
m = 1;

%% Initial Conditions

theta_i = [0;3];

%% Time conditions

time = linspace(0,20,1000);

%% Solving the ODE

[t, results] = ode45(@(t,theta) pendulum_ode(t,theta,b,g,L,m),time,theta_i);

%% Plot
figure(1)
plot (t,results(:,1))
hold on
plot (t,results(:,2))
xlabel (\'Time\')
legend ( \'Displacement\' , \'Angular Velocity\')
hold off
%% Animation

ang_disp = results(:,1);            % saving the angular displacements in column vector
ct = 1;

for i = 1:length(ang_disp)          % for loop for all the angular displacemets
    x0 = 0;                         % origin x cordinate
    y0 = 0;                         % origin y cordinate
    x1 = L*sin(ang_disp(i));        % end point of pendulum x cordinate
    y1 = -L*cos(ang_disp(i));       % end point of pendulum y cordinate
    
    figure(2)
    plot ([x0 x1], [y0 y1],\'color\',\'k\')         % plotting the pendulum arm
    hold on
    plot (x1,y1,\'o\',\'MarkerFaceColor\',\'r\')      % plotting the ball
    axis([-1.5 1.5 -1.2 0]);                    % specifying axis lengths
    hold off
    pause(0.005)
    Frame(ct) = getframe(gcf);                  % saving all the frames in matrix \'Frame\'
    ct = ct +1;
    
end

%% Creating a video of the simulation

movie(Frame)                                 % Command to play movie in matrix \'Frame\'
Video = VideoWriter(\'pendulum_animation\')    % to create a video from the MATLAB movie
open(Video)                                  % open the video file for writing
writeVideo(Video,Frame)                      % write the movie data into the video file
close(Video)                                 % close the video file

--------------------------------------------------------------------------------------

%% ODE function

function [d_theta] = pendulum_ode(t,theta,b,g,L,m)

d_theta1 = theta(2);
d_theta2 = (-b/m)*theta(2) - (g/L)*sin(theta(1));

d_theta = [d_theta1;d_theta2];

end

Explaination of code:

Initially the inputs of the system are specified. The the initial conditions of displacement = 0 and angular velocity = 3 at time = 0 is specified in the column matrix theta_i.

The timespan of 20 seconds is specified using linspace with 1000 values.

The ODE is solved usin the ode45 solver. The displacements and angular velocities is saved in the matrix \'results\'.

Figure is ploted of the displacements and velocities with time on the x-axis.

For the animation, the displacement values are saved in a new column vector \'ang_disp\'. A for loop was initiated for all the values of displacements. The position of the pendulum was calculated using trignometry. The arm of the pendulum and the ball were plotted in two seperate plot commands. Axis lengths were specified.

The getframe command was used to save each figure in a matrix named \'Frame\'.
The movie command was used to play all the frames in the matrix \'Frame\'.
In order to save the video, the videowriter command was used. The videofile was opened, the data from the matrix \'Frame\' was saved and the videofile was closed.

Output figure:

\"\"

Errors faced:

The simple pendulum animation was getting plotted over the displacement and angular velocity plot. There was overlap between these two plots. To resolve the error, I had to make two different plot which was done using the figure command. Figure(1) had the plot and figure(2) had the animation.

One more error that was faced was that all the positions of the pendulum were getting plotted at the same time. This was solved using the hold off command.

Both the errors are shown in the photo below:

\"\"

Output Video:

Youtube link: https://youtu.be/o9_CN5ZfV1o

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.