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 & create an animated video of output by solving this ODE.

Theory:

The motion of simple pendulum is a basic classical example of simple harmonic motion,consisting of a small bob & a massless string.Movement of the pendulum depends on Newtons second law. When this law is written down,a second-order ordinary diffrential equation is formed that describes the position of the pendulum with respect to time.

FIG:Simple Pendulum

Equation of motion is:

In the above equation,

a) g=gravity in m/s^2.

b) L=length of the pendulum in meter.

c) m=mass of the ball in kg.

d) b=damping coefficient.

code:

%program to simulate the transient
clear all 
close all
clc

%inputs
b = 0.05;
g = 9.81;
l = 1;
m = 1;

%initial condition
theta_0 = [0;3];

%time points
t_span = linspace(0,20,200);

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

%ploting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time in sec')

%iteration using for loop to create animation of simple pendulum
ct = 1;
theta_1 = results(:,1);

for i = 1:length(results(:,1))
    
    %initial co-ordinates
    x_start = 0;
    y_start = 0;
    theta_2  = theta_1(i);
    
    %final co-ordinates
    x_end = 1*sin(theta_2);
    y_end = -1*cos(theta_2);
    
    %ploting for animation
    figure(2)
    %base of pendulum
    plot([-1,1],[0,0],'linewidth',4,'color','k')
    hold on
  
    %string of pendulum
    line([x_start x_end],[y_start y_end],'linewidth',3,'color','b')
    hold on
    %bob of pendulum
    plot(x_end,y_end,'marker','o','markersize',28,'markerfacecolor','r')
    axis([-2 2 -2 2])
    grid on
    %pause(0.03)
    hold off
    M(ct) = getframe(gcf);
    ct = ct+1;
end
%creating movie from captured frames
movie(M)
videofile = VideoWriter('simple_pendulum.avi');
open(videofile);
writeVideo(videofile,M);
close(videofile);
    
    

Code for function program:

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

Explanation:

1.In the first step input & initial condition of a simple pendulum are fed.

2.Then the program is written to solve the ODE in which matlab inbuilt function (ode45) is used with constant theta,t,and another function is used ,which is defined seperately in function program.

3.In the next step result of solved ODE is plotted w.r.t time using plot command.

4.Now iteration of co-ordinates is fed using for loop to create animation.

5.Then plot command is used to plot pendulum string and bob.

6.In the next ,step the move command is used to create an animated video of the oscillating pendulum.

Output:

The output obtained is graphical representation velocity curve and displacement curve w.r.t time.

Angular displacement and angular velocity plots were generated for a time range og 20 seconds.

Various error faced during programing are followd:

1.Underscore mistake in writing function program.

' The step taken to overcome this error '

2.Typing mistake in plotting Bob of the pendlum.

'The step taken to overcome this error'

Result:

The video file was made and published. The link to the video is given below.

https://drive.google.com/file/d/1TIkvTK-YWotpnD9CEpI7hNN8NcHCMxI6/view?usp=sharing

Conclusion:

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

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.