Project

Simulating Pendulum using MATLAB

clear all
close all
clc
%Data of ODE
%Acceleration due to gravity
g = 9.81;
%Length of pendulum 
L = 1;
%Mass of pendulum
m = 1;
%damping coeffient
b = 0.5;
%Initial condtions
theta0 = [0 3];
time = linspace(0,20,200);
%Solving using ODE45
[t, answer] = ode45(@(t,theta) ode_challenge(t, theta, g, L, m, b), time, theta0);
%plotting
figure(1)
plot(t,answer(:, 2))
axis([0 20 -1 1]);
hold on
plot(t,answer(:, 1),\'r\');
xlabel(\'Time\');
ylabel(\'Amplitude\');
legend(\'Ang Disp\', \'Ang Vel\');

%Creating animation for pendulum
theta = answer(:,1);
ct = 1;

for i = 1:length(theta)
    RESULT_THETA = theta(i);
    
    %Initializing starting point for pendulum
    x0 = 0;
    y0 = 0;
    
    %Displacement of the pendulum
    x1 = L*sin(RESULT_THETA);
    y1 = -L*cos(RESULT_THETA);
    
    figure (2);
    %Plotting string
    plot([x0 x1], [y0 y1],\'color\', \'black\');
    grid on
    hold on
    
    %Plotting pendulum bob
    plot(x1,y1,\'o\',\'markersize\',25,\'color\',\'g\',\'MarkerFaceColor\',\'g\');
    hold off
    
    axis([-1 1 -1.5 0]);
    pause(0.003);
    mov(ct) = getframe(gcf);
    ct = ct+1;
end

movie(mov);
videofile = VideoWriter(\'Pendulum.avi\', \'Uncompressed avi\');
open(videofile)
writeVideo(videofile,mov)
close(videofile)

Fuction code is below

function out = ode_challenge(t, theta, g, L, m, b);
theta1 = theta(1);
theta2 = theta(2);
theta_dot = theta2;
theta_double_dot = -(b/m)*theta2-(g/L)*theta1;
out = [theta_dot; theta_double_dot];
end

Link of Youtube

https://youtu.be/1MJP-1IRSN4

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.