Challenge

Solving differential equation of motion of pendulum with damping

AIM:

To write a program that solves the ordinary differential equation that represents the motion of the simple pendulum with damping

INPUTS:

The ODE that needs to be solved:

`(d^2theta)/dt^2+b/m*(d theta)/dt+g/l*sin theta=0`

PROGRAM:

% Simulate the transient behaviour of a simple pendulum and to create an animation of it's motion.

clear all

close all

clc

% Inputs

l = 1; % Length of the string, m

m = 1; % Mass of the bob, kg

b = 0.05; % Damping coefficient

g = 9.81; % Accerleration due to gravity, m/s^2

% Initial condition

theta_0 = [0;3];

% Time points

t_span = linspace(0,20,500);

% Solve ODE

[t, results] = ode45(@(t, theta) pendulum_ode(t, theta, l, m, b, g),t_span, theta_0);

% Plotting

figure(3)

plot(t,results(:,1),'LineWidth',2,'Color','r')

hold on

plot(t,results(:,2),'LineWidth',2,'Color','b')

axis([0 30 -6 6])

xlabel('Time')

ylabel('Angular velocity and Angular Displacement')

legend('Angular Displacement','Angular_velocity')

figure(2)

plot(results(:,1),results(:,2))

xlabel('Angular Displacement')

ylabel('Angular velcoity')

% Pendulum animation

figure(1)

ang_displacement = results(:,1);

ct = 1;

for i = 1:length(ang_displacement)
    
    Ang_disp = ang_displacement(i);
    
    x0 = 0;
    
    y0 = 0;
    
    x1 = 1*sin(Ang_disp);
    
    y1 = -1*cos(Ang_disp);
    
    plot([-0.5,0.5],[0,0], 'LineWidth',5,'color','k')
    
    hold on
    
    plot([x0 x1],[y0 y1], 'LineWidth', 2.5,'color','r')
    
    hold on
    
    plot(x1,y1,'o','markersize',30,'Markerfacecolor','g');
    
    hold off
    
    axis([-1 1 -2 0.5]);
    
    pause(0.01);
    
    title('Animation of Simple Pendulum')
    
    M(ct) = getframe(gcf);
    
    ct = ct+1;
    
end

% Movie Creation

movie(M);

videofile = VideoWriter('Animation_of_Simple_Pendulum.avi' , 'Uncompressed avi');

open(videofile)

writeVideo(videofile,M)

close(videofile)

FUNCTION USED:

% Function for ODE

function [dtheta_dt] = pendulum_ode(t,theta,l,m,b,g)
    
    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

PROEDURE:

1. Define the inputs i.e., length of the string,mass of the bob,damping coefficient and acceleration due to gravity

2. Define the initial condition for angular displacement and angular velocity

3. Then define the time span using linspace

4. For solving the ODE there are many inbuilt matlab solvers in which i have used ode45.

ode45:

The MATLAB command ode45 performs a direct numerical integration of a set of differential equations y' = f(t,y), y(to)=yo from to to some final time tf.

[t,y] = ode45(odefun,tspan,y0), where tspan = [t0 tf], integrates the system of differential equations y′=f(t,y) from t0 to tf with initial conditions y0. Each row in the solution array y corresponds to a value returned in column vector t.

5. We defined the function separately as mentioned above and used in the ode45 syntax

[t, results] = ode45(@(t, theta) pendulum_ode(t, theta, l, m, b, g),t_span, theta_0);

6. Plot the graph between time vs angular displacement,angular velocity.

7. Plot the graph between angular displacement vs angular velocity.

8. Generating of animation of simple pendulum motion using for loop.

PLOTS:

1. Time vs Angular displacement,Angular velocity

2. Angular displacement vs Angular velocity

ANIMATION OF SIMPLE PENDULUM:

https://www.youtube.com/watch?v=EGesjO-KsRQ

https://www.youtube.com/watch?v=oANa4lgWwOY

CONCLUSION:

Thus the ode of simple pendulum is solved using matlab and the animation of the simple pendulum is done.

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.