Project

Animation of a simple pendulum using Matlab

The second order differential equation for the motion of the pendulum has been given. To script the given differential equation we need to write this equation as first-order differential equation s that it is easy for Matlab to understand the code. A function is defined in a separate script file so that it is called to run the simulation. To solve the differential equation ode45 solver is used.

function [thetadot] = pendulumsolver(t,theta);
b=0.05;
m=1;
L=1;
g=9.81;
thetadot= zeros(2,1)
thetadot(1)= theta(2);
thetadot(2)=-(b/m)*(theta(2))-(g/L)*(sin(theta(1)));

end 

% Solving second order ODE pendulum
clear all
close all
clc
% Inital conditions for time and displacement 
t= [0 20];
theta0 = [0 3];
L=1
[t,theta]=ode45(\'pendulumsolver\',t,theta0); 
plot(t,theta(:,1))
hold on
plot(t,theta(:,2))
xlabel(\'Time in seconds\');
ylabel(\'Displacemnt and velocity\');
legend(\'Displacement in rad\',\'Velocity in rad/s\');
grid on

% pendulum 
    pend = line([O(1) p(1)],[O(2) p(2)], \'linewidth\',1,\'color\' ,\'black\');
    % ball motion 
    ball= viscircles(p,0.05);
    % time interval to update plot 
    pause(0.001) ;
    M(ct)=getframe(gcf);
    ct=ct+1;
    % delete previous objects if it is not the final loop
    if i<length(t)
        delete(pend)
        delete(ball)
        delete(o_circ)
    end
end

video = VideoWriter(\'Pendulum.avi\',\'Uncompressed AVI\');
open(video);
writeVideo(video,M);
close(video)

\"\"

Link to the animation has been attached below

https://youtu.be/lIXPU980ACs

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.