Challenge

Week 3 - Solving second order ODEs

% Solving 2nd order Ordinary Differential Equation
clearall
close all
clc
% Parameters
b = 0.05;
g = 9.81;
l = 1;
m = 1;

% Initial Condition
i_c = [0;3];

% Time points
t_span = linspace(0,20,1000);

% solve ODE
[t,result] = ode45(@(t,theta) ode_func"," (t,theta,b;g;l;m);,t_span;i_c);
%result value will be consisiting of 2columns, where first column will be
% values of displacement and the other will be velocity

% Plotting Displacement and Velocity w.r.t. time
subplot (6,5,[21,22,26,27]);
plot (t,result(:,1));
xlabel('Time,s');
ylabel('Angular Displacement,rad');
axis([0 11 -1.5 1.5]);
hold on;
grid on;
grid minor;

subplot(6,5,[24,25,29,30]);
plot(t,result(:,2));
xlabel('Time,s');
ylabel('Angular Velocity,rad/s');
axis([0 11 -5 5]);
grid on;
grid minor;

% f_no is the frame number; Initialising its value as 1
f_no = 1;

for i = 1:length(result(:,1))
x1 = 1.*sin(result(i,1));
y1 = -1.*cos(result(i,1));

subplot(6,5,[21,22,26,27]);
plot(t(i),result(i,1),'marker','.','color','r','linewidth',0.5);
axis([0 20 -1.5 1.5 ]);

subplot(6,5,[24,25,29,30]);
hold on;
plot(t(i),result(i,2),'marker','.','color','r','linewidth',0.5);
hold off;
axis([0 20 -5 5]);

subplot ( 6,5,(1:15));
plot([0 x1],[0 y1],'color','m','linewidth',1.3);
hold on;
line ([-0.5 0.5 ],[0 0],'linewidth',3,'color','k');
plot(x1,y1,'marker','o','color','b','linewidth',7);
format short;
disp_t = t(i);
txt = ['Time' '= num2str(disp_t)s'];
text(0.70,0,txt);
title('Simple Pendulum');
hold off;
axis([-1.7 1.7 -1.25 0.15 ]);
pause(0.001);

% Recording Frames;
M(f_no) = getframe(gcf); % Capture each position to the variable M ;
f_no = f_no+1; % increment of frame no.
end

% Making Movie
movie(M); % plays movie of the frames captured in M
videofile = VideoWriter('simple_pendulum.avi','Uncompressed AVI'); % write video data as AVI file into videofile
open(videofile) % open video file
writeVideo(videofile,M) % to create a video file from the array "M"
close(videofile) % close video file



Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.