Project

Simple Pendulum

https://youtu.be/AsFiRr_J3Uo

% Function Code
function [dtheta_dt] = ode_func(t,theta,C,g,L,m)
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt = -(C/m)*theta(2)-(g/L)*sin(theta(1));
dtheta_dt = [dtheta1_dt;dtheta2_dt];
end
close all
clc

% Inputs  
C=0.5; % Damping Coefficient,
g=9.81; % Gravity-m/s^2,
L=1; % Length of Pendulum-m,
m=1; % mass of pendulum-kg,

% Initial Condition
theta_0 = [0;3];

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

% Solve ODE
[t,result] = ode45(@(t,theta) ode_func(t,theta,C,g,L,m),t_span,theta_0);

% Plot
figure(1)
plot(t,result(:,1),\'linewidth\',3)
hold on
plot(t,result(:,2),\'linewidth\',2)
legend(\'Displacement\',\'velocity\')
xlabel(\'Time\')
ylabel(\'Angular Velocity\')


% Animation of Pendulum
ct = 1;
for i = 1:length(result(:,1))
    x0 = 0;
    y0 = 0;
    x1 = L*sin(result(i,1));
    y1 = -L*cos(result(i,1));
    
    figure(2)
    plot([-1 1],[0 0],\'linewidth\',3,\'color\',\'r\')
    hold on
    plot(x1,y1,\'-o\',\'MarkerEdgeColor\',\'k\',\'MarkerFaceColor\',\'y\',\'markersize\',10)
    plot([x0 x1],[y0 y1],\'linewidth\',2,\'color\',\'b\')
    hold off
    axis([-2 2 -1.5 0])
    pause(0.05)
    V(ct) = getframe(gcf);
    ct = ct+1;
end
movie(V)
Videofile = VideoWriter(\'simple_pendulum.avi\',\'uncompressed AVI\');
open(Videofile)
writeVideo(Videofile,V)
close(Videofile)

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.