Challenge

Solving second order ODEs of simple pendulum

AIM :

To solve the second-order differential equation using MATLAB which mathematical of the motion of the pendulum and to create an animation for that equation.

OBJECTIVE :

To write a program that solves the following second-order differential equation. This second-order differential equation represents the equation of motion of a simple pendulum with damping

GOVERNING EQUATIONS :

In the above equation,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

FIXED PARAMETERS

Length (L) =1 metre,

Mass (m) =1 kg,

Damping coiefficient (b) =0.05.

Acceleration due to gravity (g) =9.81 m/s2.

For Time period = 0-20 sec

Matlab code:

Main code:

clearvars
close all
clc
b = .05; %  b=damping coefficient.

l = 1; % L = length of the pendulum in m,

g = 9.81; % g = gravity in m/s2,

m = 1; % m = mass of the ball in kg,

% intial condition
theta_0 = [0 5];

% time point
t_span =linspace(0,20,100);
% solve ODE
[t, result] = ode45(@(t,theta) ODE_func(t,theta,b,g,l,m), t_span, theta_0);
figure (1);
plot(t,[result(:,1) result(:,2)]);
grid on
ylabel('Plot')
xlabel('Time')
legend('Displacement (m)','Velocity (m/s^2)');
saveas(figure(1),'SHM.jpg');
theta = result(:,1);
i = 1;

for j= 1:length(theta)
    Theta = theta(j);
    
    %initial points
    x0 = 0;
    y0 = 0;
    
    %movement of pendulum
    x1 = l*sin(Theta);
    y1 = -l*cos(Theta);
    
    figure (2);
    %Plotting string
    plot([x0 x1], [y0 y1],'color', 'g');
    hold on
    
    %Plotting pendulum bob
    plot(x1,y1,'O','markersize',10,'color','r','MarkerFaceColor','r');
    hold off
    
    axis([-1.2 1.2 -1.2 1.2]);
    pause(0.001);
    M(i) = getframe(gcf);
    i = i+1;
end

movie(M);
videofile = VideoWriter('pendulum1.avi', 'Uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile);
  • In the above program, the fixed parameters are length, mass, damping coefficient, and acceleration due to gravity.
  • Time and angle are given as the initial condition.
  • In the t_span command, 100 values are taken in between 0 to 20 times period.
  • Using the ode 45 command ODE function is introduced.
  • Using the plot command velocity and displacement are plotted in the y-axis against time on the x-axis.
  • Using the for loop command the mass at various points and various times are marked in the graph and are plotted.
  • Using the movie command the plot is saved as an uncompressed .avi file.

function code : ODE_func

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
  • In this function program, the second-order ODE is simplified to the first-order ODE by introducing the term theta1 & theta2.
  • Then theta1 & theta2 are differentiated and the dtheta_dt gives a column matrix.

Results:

The program executed and animation generated successfully.

Output:

ANIMATION

link for the motion of pendulum:

https://youtu.be/7vP1U1MFQyo

the plot of the graph:

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.