Week 3 - Solving second order ODEs
AIM
To create a matlab program to solve a second order ode of a simple pendulum and plot a movie to demonstrate its movement .
THEORY
A simple pendulum is a heavy point mass suspended by a weightless ,perfectly flexible and inextensible string from a perfectly rigid support is called a simple pendulum
The way pendulum moves depends on the newtons second law it states that accelaration of an object is directly propotional to the net-force acting on it and is inversly propotional to its mass.
PROGRAM
% solving a second order ode of a simple pendulum
%inputs
b=0.05;
g=9.81;
m=1;
l=1;
% initial conditions
dis_angvelo=[0;3];
%time points
t_span=linspace(0,20,500);
%solving ode
[t,results]=ode45(@(t,theta)ode_function(t,theta,b,g,l,m),t_span,dis_angvelo);
% plotting
figure(1)
plot (t,results(:,1),'LineWidth',2)
hold on
plot(t,results(:,2),'LineWidth',2)
axis ([0 25 -3 3])
xlabel ('time_sec')
ylabel('angular velo_disp')
legend('change in displacement','change in angular velocity')
hold off
figure(2)
plot (results(:,1),results(:,2))
xlabel('position_rad')
ylabel('angular velocity_rad/sec')
theta2=results(:,1);
ct=1;
for i=1:length(theta2);
theta3=theta2(i);
x0=0;
y0=0;
x1=1*sin(theta3);
y1=-1*cos(theta3);
figure(3)
plot([-1.1 1.1],[0,0],'linewidth',2);
plot([x0 x1],[y0 y1])
hold on
plot(x1,y1,'o','Markers',20,'MarkerFaceColor','b')
hold off
grid on
axis([-1.5 1.5 -1.5 0.5])
pause (0.003)
M(ct)= getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('pendulum.avi','uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close (videofile)
EXPLANATION OF THE PROGRAM
- In order to solve a second order ODE we need to create a function ode_function .
- various inputs are defined .
- ode45 is a new command used as a solver in matlab to solve first order ODE .
- after completing the whole program angular displacement and angular velocity is plotted against time
- legend is a command used to name graphs
- to create the motion of pendulum a for loop is used
- get frame is a command used to create a movie
- the movie is saved in a new video file named as pendulum.avi
- video file is written
RESULTS AND OUT-PUT PLOTS
The second order ODE of a simple pendulum is solved using matlab
the movie and plots of simple pendulum is attached below .

figure 1 Angular velocity / time

figure 2 position/angular velocity
MOVIE YOU TUBE LINK
https://youtu.be/uaFTvZEQ93Y
ERRORS FACED

Error -1 ,forgot to end loop counter

Error 2 - name of function changed , used capital instead of small