Challenge

Week 3 - Solving second order ODEs

Aim:

To code a program solving second oder equation for solving motion of the pendulam with damper.

Objective:

  • To write a program that solves the following ODE.
  • This ODE represents the equation of motion of a simple pendulum with damping.
  • To upload the animation in YouTube.

Formula:

where b = damping, m = mass, g = gravity, L =Length

Code:

To plot the graph of pendulum and to create animatioon of it.

clear all
close all
clc

%inputs
b = 0.05;   % damping co-efficient
g = 9.81;   % gravity
l = 1;      % length (unit m)
m = 1;    % mass (unit kg)

% initial condition for the displacement and velocity of the pendulum

theta = [0;3];  % displacement = 0, velocity = 3

% time span

t_s = linspace(0,20,500);   % from 0 to 20 seconds

%solve ODE

[t,r] = ode45(@(t,th) ode_func(t,th,b,g,l,m),t_s,theta);

figure(1)
plot(t,r(:,1));
hold on
plot(t,r(:,2));
ylabel('Plot')
xlabel('Time')

% animation
figure(2)
c = 1;  % counter variable
for i= 1:length(r(:,1)) 
   
    x = 0;
    y = 0;
    x1 = l*sin(r(i,1));
    y1 = -l*cos(r(i,1));
    
    plot([-1 1],[0 0],'linewidth',15,'color','r');  % fixed part of the pendulum
    axis([-1.5 1.5 -1.5 1.5])
    hold on
    line([x x1], [y y1], 'linewidth',5,'color','b') % String part of the pendulum
    plot(x1,y1,'o','markersize',25,'markerfacecolor','g')   % Ball position of the pendulum
    hold off
    
    % assigning an array function to get the current figure
    M(c) = getframe(gcf);
    c = c+1;
end

% To generate movie with frames
movie(M)

% To create new AVI file
videofile = VideoWriter('ode','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M)
close(videofile) 

Solving ODE:

function [dtheta_dt] = ode_func(t,th,b,g,l,m)   % t = time, th = theta
th1 = th(1)
th2 = th(2) 
dtheta1_dt = th2;
dtheta2_dt = -(b/m)*th2 - (g/l)*sin(th1);
dtheta_dt = [dtheta1_dt ; dtheta2_dt];
end

Procedure:

Open the Matlab Software and open the editor window. To solve the second order ODE for the pendulum, input values such as length of the string, damping co-efficient, mass of the pendulum and gravity are declared initially. Then the initial condition of displacement and velocity of the pendulum is declared as theta. Time span is given from 0 to 20 seconds. To solve the second order ODE, we create a function as a seperate program. Formula and derivation to solve ODE is given below,

The function is created by above equation and it is called into the program, to create the graph representing the rate of change of angle with respect to time in a second order ODE for simple pendulum with damper. To create the animation, plotting values of x axis and y axis is declared as zero initially. To plot the pendulum, we use the formula such as x1 = l*sin(r(i,1)) and y1 = -l*cos(r(i,1)). To create the animation of the pendulum attached with the damper, an array function is assigned and count value is then incremented. To generate movie frames, we provide the function 'movie' to the program. After creating the movie, AVI video file is created. The basic functions of video file is made such as open,write and close. After creating animation, it is uploaded in YouTube.

Errors occured:

  • When declaring y1 as a positive value, the position of pendulum in the output is reversed.

  • When hold on command is not given, the following output is arrived

Output:

  • Graph representing the rate of change of angle with respect to time in a second order ODE for simple pendulum with damper.

  • The animation uploaded in my YouTube channel,

https://youtu.be/FiIW9uLF4n4

Conclusion:

Thus the animation for second order ODE for simple pendulum with damper is created and uploaded in my YouTube channel.

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.