Project

Pendulum Animation in MATLAB

Link for Youtube Video - https://youtu.be/2KTN3OIWbbY

Function Code -

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

Main Code -

clear all
close all
clc

% inputs
b = 0.05;
g = 9.81;
l = 1;
m = 1;

% initial condition
theta_0 = [pi/8;3];

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

% solver ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

%defining starting points of animation
x0 = 0;
y0 = 0;

%plot in looping
ct = 1;
for i = 1:length(results)
    y1 = -l*cos(results(i,1));
    x1 = l*sin(results(i,1));
    hold off
    plot([x0,x1],[y0,y1],\'color\',\'g\')
    axis([-l*1.1,l*1.1,-l*2,0])
    hold on
    plot(x1,y1,\'o\',\'markersize\',20,\'markerfacecolor\',\'b\')
    title(\'Pendulum Animation\')
    xlabel(\'x-axis (metre)\')
    ylabel(\'y-axis (metre)\')
    pause(0.0005)
    M(ct) = getframe(gcf);
    ct=ct+1;
end
%saving the video
movie(M)
videofile = VideoWriter(\'pendulum.avi\',\'uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Code Explanation - Before writing main code for animation, we need to write code for function of differential equation. It will take length of string, mass of ball, damping co-efficient, acceleration due to gravity, time and theta as input to return matrix of differential equations as output (angular displacement and angular velocity). Thus, kinematics equation for this motion is used in program to give output equations.

For this animation, we start the MATLAB program by clear all, close all and clc to close and clear all windows and variables. Then, we provide with inputs for the pendulum equation which are length of string, mass of ball, dampening co-efficient and acceleration due to gravity. Then, we will define initial conditions for differential equations which are initial angular displacement and initial angular velocity. Then,we will define time range for integration of differential equations.

Next, step will be to solve the ordinary differential equations by ode45 method, where ode_func will be used as input function to give angular displacement and angular velocity at different time intervals as output. Now, looping will be used to get frames at different time intervals. Before looping a variable ct is defined to be used as counter in the program. Now, since string initial point is fixed, thus x0 = 0 and y0 = 0. In the for loop, which runs till the array length of results, y1 = -l*cos(θ), negative is used for visual representaion of animation whereas x1 = l*sin(θ). Then, hold off is used to erase previous state in the graph when new plot is created, then line between x0,y0 and x1,y1 is created. Then, hold on is used to retain the state of graph. Then, a circular filled marker is created at x1,y1 . A delay of 0.0005 seconds is added for smooth animation. Then, frame is saved by getframe command. At, the end of loop, counter is incremented. Thus, all frames are taken. Then, movie is created of all the frames. A new video file is created and this movie is written in the video file. The video file is closed.

Errors - Following error was encountered -

\" Error using odearguments (line 93)
@(T,THETA)ODE_FUNC(T,THETA,B,G,L,M) returns a vector of length 2, but the length of initial conditions vector is 3. The vector returned by @(T,THETA)ODE_FUNC(T,THETA,B,G,L,M) and the initial conditions vector must have the same number of elements. \"

Steps To Overcome This Error - This error occured because initialization of theta vector was wrongly defined, theta_0 = [pi/8:3] which is 1x2 vector but it should be 2x1 because ode_func has output as 2x1. Replacing, it with semicolon corrected the error and program ran.

Screenshot of Error -

Thanks!!

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.