Challenge

Week 3 - Solving second order ODEs

Aim : Pendulum path track using Second order ODE function

Inputs :

b = Damping coefficient = 0.1

g = gravity = 9.81m/s^2

l = Length of the pendulum = 1m

m = Mass of the ball = 1Kg

Initial conditions:

Angular displacement = 0

Angular velocity = 3rad/sec

Code:

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

    



clear all
close all
clc

b=0.05;
g=9.81;
l=1;
k=-1;
m=1;

%Initial condition
theta_0 = [0;3];

%Time points
t_span = linspace(0,20,500);

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



%Pendulum motion
X_cord = t_span;
Y_cord = results(:,1);

ct = 1;
for i = 1:length(t_span)
    
Ratio = Y_cord(i)./l;
THETA(i) = asind(Ratio);

X0 = 0;
Y0 = 0;
X(i) = k*sind(THETA(i));
Y(i) = k*cosd(THETA(i));

figure(1)

plot([X0 X(i)],[Y0 Y(i)],'-s','MarkerSize',10,'MarkerEdgeColor','red','MarkerFaceColor',[1 .6 .6])
axis([-1 1 -1.1 0.2])
pause(1.5)
grid on

M(ct) = getframe(gcf);
ct = ct+1;

end

movie(M)
videofile = VideoWriter('Pendulum_travel.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Process:

  1. Created ODE_func that converts double order DE into two single order DE.
  2. Called the function in the parrent program.
  3. With this in the time period of 0 to 20s, the Damped oscellations got generated.
  4. Results received in Tabular format with Coloumn no 1 = Damped diplacement & coloumn no 2 +Damped velocity.
  5. In order to generate pendulum track, each point is associated with (X_cosØ, Y_sinØ)
  6. Found out theta with trignomatric relationships.
  7. Used for loop to trace out all the path coordinates.
  8. Using plot command generated graphics.

Errors faced: All the string travel was appeared.

Remedy: Hold on command was there in the loop, Deleting it solved the issue.

Conclusion:

Pendulum travel track generated susccesfully using ‘ode45’ command.

Video Link:

https://youtu.be/kL6ybakn3gY

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.