Challenge

Week 3 - Solving second order ODEs

Aim : To solve the ODE which represents the equation of motion of a simple pendulum with damping.

Objective :

To write a program that solves the following ODE which represents the equation of motion of a simple pendulum with damping and create an animated video of output obtains by solving this ODE.

Introduction :

A second order ordinary differential equation is an ordinary differential equation in which any derivatives with respect to the independent variable have order no greater tha 2.

Ordinary differential equations helps us to solve the many engineering systems. such as harmonic motions, vibrations, damping,etc.

Second-order liear differential equations are used to model many systems in engineering.

A simple pendulum consists of mass 'm' hanging from a string of length 'l' and released, the pendulum will swing back and forth with periodic motion.

The equation is given by :

Explanation of the code :

%Inputs are taken as given in the problem

L=1m,

m=1kg,

b=0.05,

g=9.81m/s^2

%Initial conditions are taken as

Angular displacement=0,

Angular velocity=3rad/sec ,

at time t=0,

The motion is simulated between 0-20sec

%Solving ODE

ode solver ode45 is used in this matlablab program. The results are stored in a array or row matrix of two coloumns, for the values of diaplacement and velocity at different time periods.

ode_func is the function are made to solve the given equation. where the asumptions are made in the form of code.

Figure 1 : plotting

here the graph is plotted to get the wave form of the displacement an dthe velocity from the intial conditions.

Figure 2 : plotting

Animating the transient motion of the simple pendulum :

Here we use the concept of the for loop. as the bob moves with a velocity, and displaces for the different time period. and recorded it and animated the path pf the oscillations.

% Solving second order ODEs of a pendulam


clear all
close all
clc


b = 0.05; % damping cofficient
g = 9.81; % gravity (m/s^2)
l = 1; % length of the spring (m)
m = 0.1; % mass of the bob (kg)

% initial condition
% angular diaplacement is 0 and angular velocity is 5

theta_0 = [0;5];

% time points
% pendulum oscillates between 0-20 seconds

t_span = linspace(0,10,500);

%solving ODE
[t,results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m), t_span, theta_0);

%Plotting
figure(1)
hold on
plot(t,results(:,1))
plot(t,results(:,2))
title('Damped harmonic motion of a pendulum')
legend('Angular displacement', 'Angular velocity')
xlabel('Time(seconds)')
ylabel('Plot')



% For loop
% Counter value was defined

ct = 1;
for i = 1:length(results(:,1));
x_start = 0;
y_start = 0;
x_end = l*sin(results(i,1));
y_end = -l*cos(results(i,1));

% plotting the animation
figure(2)
plot([-1 1], [0 0], 'linewidth', 5)
hold on 
plot([x_start x_end],[y_start y_end],'linewidth',3)
hold on

plot(x_end,y_end,'o','markersize',20,'markerFacecolor','y')
title('Animation of trasient behaviour of a pendulum')
axis([-1.5 1.5 -1.5 1])
pause(0.03)
hold off

M(ct) = getframe(gcf)
ct = ct+1

end


% Animation of the pendulum

movie(M)
videofile = VideoWriter('Pendulum Animation.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)



function [dtheta_dt] = ode_func(t,theta,b,g,l,m)


theta1 = theta(1)
theta2 = theta(2)
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2 - (g/l)*sin(theta1);
dtheta_dt = [dtheta1_dt; dtheta2_dt];

end


Figure-1 :

Error #1 : Invaild expression, forgor to use parentheses

Error #2 : Not using the hold on &hold off command at the proper step.

Conclusion :

The simulation of the trasient behaviour of a simple pendulum is successfully created by using a program in MATLAB.

Youtube Video link :https://youtu.be/9n2OeoNctRQ

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.