Challenge

Week 4.1 - Solving second order ODEs

%Second Order ODE of a Simple Pendulum and its Animation
clear all
close all
clc
%Inputs
b = 0.05; %damping coefficient
g = 9.81; %acceleration due to gravity in m/s2
l = 1; %length of the pendulum in m
m = 1; %mass of the pendulum bob in kg
%Initial condition
theta_0 = [0;3];
%Time span
t_span = linspace(0,20,600);
%Solving ODE
[t,y] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
%Plotting Angular displacement and Angular velocity w.r.t Time
plot(t,y(:,1),'linewidth',2);
hold on;
plot(t,y(:,2),'linewidth',2);
axis([0 20 -4 4]);
xlabel('Time (sec)');
ylabel('Angular displacement and Angular velocity');
grid on
title('Damping Effect in Simple Pendulum');
legend('Angular displacement','Angular velocity');
pause(10);
ct = 1;
for i = 1:length(t_span);
x0 = 0;
y0 = 0;
x1 = l*sin(y(i,1));
y1 = -l*cos(y(i,1));
%Simulation of motion of Pendulum
plot([x0 x1],[y0 y1],[-0.5 0.5],[0 0],'linewidth',3)
hold on
plot(x1,y1,'marker','o','markerSize',25,'markerFacecolor','r')
axis([-1.5 1.5 -1.5 0.5])
grid on
title('Motion of a Simple Pendulum with Damping')
pause(0.01)
M(ct) = getframe(gcf);
ct=ct+1;
hold off
end
%Animation of motion of Simple Pendulum
movie(M)
videofile = VideoWriter('Simple_Pendulum.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
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.