Project

Simple Pendulum simulation

REPORT

Code for a simple pendulum animation

close all
clear all
clc


b = 0.05;
g = 9.81;
l = 1;
m = 1;
ct =1;

%Inputs
th0 = [0;3];

%time points
t_span = linspace(0,20,500);

%solve ODE
[t, results] = ode45(@(t,th) ode_func(t,th,b,g,l,m),t_span,th0);

figure(1)
hold on
plot(t,results(:,1))
plot(t,results(:,2))
xlabel(\'x-axis\')
ylabel(\'y-axis\')
grid on


th2 = results(:,1);

for i = 1:length(th2)
TH2 = th2(i);

x0 = 0;
y0 = 0;

x1 = l*sin(TH2);
y1 = -l*cos(TH2);

%ploting the pendulum

figure(2)
plot([-0.5, 0.5],[0, 0],\'color\', \'b\',\'linewidth\',\'5\')
hold on
plot([x0 x1],[y0 y1],\'color\', \'r\',\'linewidth\',\'2\')
hold on
plot(x1,y1,\'o\',\'markers\',15,\'markerfacecolor\',\'y\')
hold off
grid on
axis([-1.5 1.5 1.5 0.5]);
pause(0.03)
M(ct) = getframe(gcf);
ct = ct+1;
end

movie(M)
videofile = VideoWriter(\'simple_pendulum.avi\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Code for \'ode_func\' function

function [dth_dt] = ode_func(t,th,b,g,l,m)

th1 = th(1)
th2 = th(2)
dth1_dt = th2;
dth2_dt = -(b/m)*th2 - (g/l)*sin(th1);
dth_dt = [dth1_dt; dth2_dt];
end

EXPLANATION

The objective is to animate the condition of swinging of simple pendulum taking initial condition as a mass, length, damping coefficient, intial displacement and angular velocity as a function of time. In this we solve second-order ODE by simpifying it in two first-order ODEs. We define a seprate function solve these ODEs and taking there output as input for ode45 dolver. ode45 is in-buit MATLAB function used to solve first-order ODEs.This gives us a 2 column array giving velocity and displacement of the bob. We plot the graph of these two quatities w.r.t. time. In another plot, we plot the motion of pendulum and simulate it by using for-loop and making a video using a set of video commands.

YouTube video link : https://www.youtube.com/watch?v=P7mdDqTBHfs

Mention the errors faced while programming

* Most of the common errors are typo errors.

* Difficulty in ode45 command.

* Error in videoWriter command.

Steps taken to overcome those errors.

* Uses the MATLAB doc for correct syntax.

* Refer the video tutorial twice for bettter understanding of ode45 solver.

* Refer to the book for more knowledge on videoWriter command.

Images

1.

2.

3.

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.