Challenge

Solving second order ODEs

Aim - Using ODE (Ordinary Differential Equation) to describe the transient behaviour of simple Pendulum.

Objective -

Our objective here is to write a code for solving a 2nd Order ODE to simulate the motion of a simple Oscillating Bob aka Pendulum. We have used the following data which was provided by Skill-Lync.

1 - 2nd Order ODE.

2 - l =1 metre,

3 - m=1 kg,

4 - b=0.05.

5 - g=9.81 m/s2.

and then simulate the motion between 0-20 sec, for angular displacement =0 and angular velocity=3 rad/sec at time t=0.

%Simple pendulum
clear all
close all
clc
% Inputs
z=0.05; %damping_coefficient (Zeta)
g=9.81; %gravity
l=1;    %length
m=1;    %mass

%initial condition
theta_0 = [0;3];   %initial displacement and initial velocity
%time point
t_span = linspace(0,20,300); %since given time is 20s
% solving ODE
[t,results]= ode45(@(t,theta) ode_func(t,theta,z,g,l,m),t_span,theta_0); %ode45 is built in Matlab function
plot(t,results(:,1),'linewidth',1.5)
hold on
plot(t,results(:,2),'linewidth',1.5)
ylabel("Displ./Velo.")
xlabel("Time(Sec.)")
legend('Displ','Velo.')

% to get the coordinates of Bob
theta2 = results(:,1)
count = 1;
x0=0;
y0=0;
for i = 1:length(theta2);
    theta3 = theta2(i);
    % coordinates of movement 
    x1= l*sin(theta3);
    y1= -l*cos(theta3);
    
    figure(2)
    plot([-1 1],[0 0],'linewidth',2)
    hold on
    plot([x0 x1],[y0 y1],'color','b','linewidth',2)
    hold on
    plot(x1,y1,'o','markers',20,'markerfacecolor','r')
    hold off
    axis([-1.5 1.5 -1.5 0.25])
    grid on
    xlabel('Damped pendulum')
    pause(0.002)
    M(count)=getframe(gcf);
    count=count+1
end
movie(M)
videofile= VideoWriter('damped pendulum.avi','uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
    

Plot -

Link to see the Animated vide of Pendulum -

https://youtu.be/D55S7Z2eiPY

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.