simple pendulum motion using matlab
% Main Program
clear all;
close all;
clc
% Week 2 Solving 2nd order ODE using MATLAB
g = 9.81; % Acceleration of gravity
l = 1; % Length of Pendalum
m = 0.1; % Masss of Pendalum
r=0.1; % Radius of Pendalum
% Initial Conditions
theta_0 = [0;5];
% Time Points
t_span = linspace(0,2,100);
% Compute Natural Frequency
omega=g/(l*m);
% Solve ODE
[t, results] = ode45(@(t,theta) f_ode(t,theta,omega),t_span,theta_0);
% Plot Responses
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel(\'Responses\')
xlabel(\'Time\')
legend(\'Displacement\',\'Velocity\');
%animation
ct=1;
for i = 1:length(t_span);
THETA = results(i,1);
%Coordinates for pendulum
x1 = l*sin(THETA);
y1 = -l*cos(THETA);
% Plot
figure(2)
plot([0 x1],[0 y1],\'linewidth\',5)
axis([ -2*l 2*l -2*l 2*l]);
title(\'Single Pendulum (Harmonic Motion)\')
M(ct)=getframe(gcf);
ct=ct+1;
end
% Create Animation
movie(M)
videofile=VideoWriter(\'PENDULUM.avi\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
function [dtheta_dt]=f_ode(t,theta,omega)
theta1=theta(1);
theta2=theta(2);
dtheta1_dt=theta2;
dtheta2_dt= -omega^2*sind(theta1);
dtheta_dt=[dtheta1_dt;dtheta2_dt];
end
video link