Week 3 - Solving second order ODEs
AIM:
Write a program in Matlab\Octave that will simulate the pendulum motion.
PROCEDURE:
clear all
close all
clc
%parameters
b=0.03
g=9.81
l=1
m=0.1
%initila conditions
theta_0=[0;3];
time 0 to 20 seconds with 200 time steps
t_span=linspace(0,20,200);
%calling in built function ode45 to find the derivative of our function
%our function is ode_func with input arguments b,g,l,m
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1))
xlabel('Time')
ylabel('Displacement')
figure(2)
plot(t,results(:,2))
xlabel('Time')
ylabel('Angular velocity')
figure(3)
Theta=results(:,1)
count=1
for i=1:length(Theta)
theta=Theta(i)
x0=0;
y0=0;
x1=l*sin(theta);
y1=-l*cos(theta);
plot([-0.5 0.5],[0 0],'r')
hold on
plot([-0.5 0.5],[0.0625 0.0625])
hold on
plot ([0 0],[0 -1])
plot(x1,y1,'o','markersize',25,'color','r','markerfacecolor','r');
plot([x0 x1],[y0 y1],'linewidth',5);
hold off
axis([-1 1 -1.5 1])
pause(0.03)
M(count)=getframe(gcf);
count=count+1;
end
%Animation
movie(M);
videofile=VideoWriter('Pendulum.avi')%saving our animation to a video file with name pendulum
open(videofile)%opening the video file
writeVideo(videofile,M)%writing the video file with arrays in M
close(videofile)
RESULTS:

