Professional Documents
Culture Documents
Motion of Simple Pendulum Using Matlab
Motion of Simple Pendulum Using Matlab
This solution shows that the amplitude keeps on increasing if the time of integration is large.
clear;
length = 1;
%pendulum length in metres
g =9.8;
% acceleration due to gravity
npoints = 250;
%Discretize time into 250 intervals
dt = 0.04;
%time step in seconds
omega = zeros(npoints,1);
% initializes omega, a vector of dimension
% npoints X 1,to being all zeros
theta = zeros(npoints,1);
% initializes theta, a vector of dimension
% npoints X 1,to being all zeros
time = zeros(npoints,1);
% this initializes the vector time to being all zeros
theta(1) =0.2;
% you need to have some initial displacement,
% otherwise the pendulum will not swing
for step = 1:npoints-1
% loop over the timesteps
omega(step+1) = omega(step) - (g/length)*theta(step)*dt;
theta(step+1) = theta(step)+omega(step)*dt;
time(step+1) = time(step) + dt;
end
plot(time,theta,'r' );
%plots the numerical solution in red
xlabel('time (seconds) ');
ylabel('theta (radians)');
The next solution, using Euler-Cromer method, shows that the amplitude is stable even if the time of
integration is too large
omega = zeros(npoints,1);
theta = zeros(npoints,1);
time = zeros(npoints,1);
theta(1) = 0.2;
for step = 1:npoints-1
% loop over the timesteps
omega(step+1) = omega(step) - (g/length)*theta(step)*dt;
theta(step+1) = theta(step)+omega(step+1)*dt;
time(step+1) = time(step) + dt;
end
plot(time,theta,'r' );
%plots the numerical solution in red
xlabel('time (seconds) ');
ylabel('theta (radians)');
The only difference between the two methods is in the two lines which runs within the for loop:
vs.