Challenge

Week 3 - Solving second order ODEs

                   SECOND ORDER ODE
                              (Oscillation of pendulum)

Aim:
         In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum
The way the pendulum moves depends on the Newtons second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the "ball" w.r.t time. 
Similarly, there are hundreds of ODEs that describe key phenomena and if you have the ability to solve these equations then you will be able to simulate any of theses systems.
Your objective is to write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping
 
 

Governing Equations:
                        g = gravity in m/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Write a program in Matlab\Octave that will simulate the pendulum motion, just like the one shown in the start of this challenge.
use,
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
3. Objectives of the project:
When you do this you will get the position of the pendulum as a function of time
And oscillations of the pendulum can also be achived.
 

4. Body of the Content:
1.function [dtheta_dt] = ode_fun(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

2. % Solving Second Order Ordinary Differential Equation(ODE) Using Mathlab

% Inputs
% Damping force Coefficient
b = 0.05;

% Gravity
g = 9.81;

% Length of the string or rope
l = 1;

% Mass of the Ball
m = 1;

% Intial Condition
% Angular displacement = 0 and Angular Velocity =3 
theta_0 = [0;3];

% Time Taken for the pendulum to ossilate 
% Giving 300 values between 0-20sec using linspace command
t_span = linspace(0,20,300);

% solve ODE
% using ODE45 inbuilt function to solve first order ODC for all the given values 
[t,results] = ode45(@(t,theta) ode_fun(t,theta,b,g,l,m),t_span,theta_0);

% ploting
% figure of the window
figure(1)
% Angular displacement
plot(t,results(:,1))
hold on
% Angular Velocity 
plot(t,results(:,2))
xlabel('Time')
ylabel('Plot')
legend({'displacement','velocity'})

% To define piviot point of pendulum
x0 = 0;
y0 = 0;
%plotting x0 y0 as a fixed point
plot(x0,y0,'Marker','o')

% To extract the first coloumn of the result given to y
% y = theta/Angular displacement
y = results(:,1);

% To create images for each time values and stored in the array 'M'
ct = 1;
for i = 1:length(t_span)
    x1 = l*sin(y(i));
    y1 = -l*cos(y(i));
    % ploting [x0,x1],[y0,y1] for the osicallation of pendulum
    plot([x0,x1],[y0,y1],'LineWidth',5,'Marker','o')
    hold on
    plot(x1,y1,'Color','g','Marker','o','MarkerSize',40,'MarkerFaceColor','auto')
    hold off
    title({'Simple Harmonic Motion'; 'Pendulum'},'color','r')
    axis([-1 1 -2 0])
    pause(0.03)
    xlabel('Time')
    ylabel('Angular displacement & Angular velocity')

    % Were all the figures and plot converting into frames and stored in
    % array called'M'
    M(ct) = getframe(gcf);
    ct = ct +1;
end
%Movie or Animation Creation
movie(M);
videofile = VideoWriter("second_order_ODE.avi","Uncompressed AVI");
open(videofile);
writeVideo(videofile,M);
close(videofile);


5.Conclusion:
        The result is we can able to get a plot where the pendulum one end is fixed and the other end able to oscillate according to the values we achieved during the program .
The problem faced during this program is mismated of words like ‘theta’ were written wrong like ’theata’.The problem has been cleared by correcting it.
The other problem is instead or ‘results’ the program were written with ‘t_results so it was mismatch with the plot and function. The problem were able to cleared it by changing it.
There were pictures were attached with it about the problem that I faced and how I cleared it.
Animation were given in this link:
https://youtu.be/Xrnrrytf9MQ 


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.