Week 3 - Solving second order ODEs
This report is written to detail the methods of producing andcreating animation data of damped pendulum motion - given the initial input data of the system. FOr this purpose, MATLAB and Cygwin were used in conjunction. Tht MATLAB code used in this process is shown below:
clear all;
close all;
clc;
%Defining Constants
b=0.05;
g=9.81;
l=1;
m=0.1;
theta0=[0;5];
tSpan=linspace(0,10,600);
[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),tSpan,theta0);
x0=0;
y0=0;
%Plotting the graphs and saving them
legendlabels=["Angular Acceleration (rad/s^2))","Angular Velocity (rad/s)"];
ct = 0;
figure
subplot(2,1,2), plot(t, results(:,1),"b-",t, results(:,2), "r-");
ylabel("Magnitude")
xlabel("Time(s)")
legend(legendlabels);
for i=1:length(results(:,1))
ct=ct+1;
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1));
subplot(2,1,1),
plot(x1,y1,"o-","markers",15);
title("Dampened Pendulum Motion");
hold on;
plot([x0 x1],[y0 y1],"k-");
hold on;
plot([1.5 -1.5],[0 0],"k-");
grid on;
axis([-1.5 1.5 -1 0]);
pause(0.01);
filename=sprintf("%ddampenedpendulum.png",ct);
if ct < 1000
filename=sprintf("0%ddampenedpendulum.png",ct);
if ct < 100
filename=sprintf("00%ddampenedpendulum.png",ct);
if ct < 10
filename=sprintf("000%ddampenedpendulum.png",ct);
end
end
end
saveas(gcf,filename);
cla;
end
The MATLAB code takes data associated with the pendulum, such as pendulum mass and length, damping coefficient, gravitational constant and initial pendulum velocity and acceleration. These are used as coefficients in the second order differential equation solved at 600 time instances spanning 10 seconds. The output of this data is made of angular position and velocity of the pendulum.
Once this data is obtained, each data point is used to produce snapshots of the pendulum, plotted and saved on disk for processing using Cygwin and Image Magick. The output of this process is found at the link below:
Dampened Pendulum Motion - Youtube
Errors encountered whilst coding:
- Various syntax-related errors resulting from misplaced characters, parentheses and other symbols. Solved by placing said characters and symbols in their rightful place, thereby correcting said errors.