Week 3 - Solving second order ODEs
%Solving 2nd order ODE using matlab
clear all
close all
clc
%Inputs
%Damping coefficient
b = 0.5;
%Gravity
g = 9.81;
% length of rope
l =1;
% mass of ball
m = 1;
% initial condition
% angular displacement =0;
% angular velocity =5;
theta_0 = [0;5];
% Time points
% Generation 200 values between 0-20(sec) using linspace command
t_span = linspace(0,20,200);
% slove odes
% using ode45 which is inbuilt ode solver in matlab
[t,results] = ode45(@(t,theata) ode_func(t,theata,b,g,l,m),t_span,theta_0);
% plotting
% angular displacement
plot(t,results(:,1))
hold on
% Angular velocities
plot(t,results(:,2))
xlabel('time')
ylabel('plot')
legend({'displace','velocity'})
% To defined pivot point of pendulam & plot it.
x0 = 0;
y0 = 0;
plot(x0,y0,'marker','o')
% To extract the first colomn of a result and assigned it to 'y'
y = results(:,1); % Theta/Angular displacement
% To create images for each time value and stored it in array 'm'.
ct = 1;
for i = 1: length(t_span)
x1 = l*sin(y(i));
y1 = -l*cos(y(i));
plot([x0 x1],[y0 y1],'linewidth',3,'marker','o')
hold on
plot (x1,y1,'marker','o','Color','g','markersize',30,'markerfacecolor','g')
hold off
title({'simple harmonic motion'; 'of a pendulom'},'color','r')
axis([-1 1 -2 0])
pause(0.03)
xlabel('time')
ylabel('angular displacement & angular velocity')
% All figure are converting into frames and are getting stored in array called 'M'
M(ct)= getframe(gcf);
ct=ct+1;
end
% Movie creation
movie(M);
videofile = VideoWriter("second_order_odes.avi","uncompressed AVI");
open(videofile);