Challenge

Week 3 - Solving second order ODEs

SECOND ORDER ORDINARY DIFFERENTIAL EQUATION (ODE):

ORDINARY DIFFERENTIAL EQUATION (ODE):

An ordinary differential equation (ODE) is a differential equation containing one or more functions of one independent variable and the derivatives of those functions. The order of the ODE's depend on the higher power in the equation.

Example:

(i) 1st order ODE: dy/dx = 3xy;

(ii) 2nd order ODE: f(x) = 8 - x^2;

These types of ODE's are used in MATLAB by the solver called 'Ode45' which is used to solve the problems which equations are in ODE forms. when you solve a higher order ODE's, we resolve them into set of first order ODE's because the solver only takes the first order that too with 'n' no.of ODE's. These types of ODE's are used to solve the motion of a simple pendulum as well as in vehicle dynamics.

AIM:

1. To simulate the motion of pendulum using 2nd order ODE in 'MATLAB'.

2. Create a movie of a moving pendulum using MATLAB.

THEORY:

A simple pendulum consists of a mass m hanging from a string of length L and fixed at a point. When displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion. For small displacements, a pendulum is a simple harmonic oscillator.

Where, L = length of string (m)

m = mass of the bob (kg)

b = damping coefficient

g = gravity (m/sec^2)

Angular displacement:

The short distance travelled by a body is called as 'displacement'. A body that rotates about a rotation axis, then its distance is said by 'angular displacement'. that changes its angular position from θ1 to θ2, lets assume it as 'θ'.

Angular velocity:

The changing distance with respect to time is called 'velocity'. Then the changing angular displacement with respect to time is called as 'angular velocity'.

Angular velocity = angular displacement/changing w.r.t to time

ω = dθ/dt

The motion of a simple pendulum is given by,

---(eq-1)

The above equation is a second order ODE. When we have a higher power, we should resolve into set of first order ODE's

From these eq's,

dθ1/dt = θ2;

dθ2/dt = -(b/m)*θ2 -(g/L)*sin(θ1)

From these two eq's, we make an vector matrix for it represent as a single variable 'dθ/dt'.

Program:

Write a program in MATLAB that will simulate the motion of a simple pendulum.

Finding the changing angular displacements and angular velocities changing with respective to time.

  • First assign the set of first order 'ODE's in a function to use it in the main program.
function [dtheta_dt] = ode_variable_function(t,theta,b,g,l,m)


%theta = [theta1; theta2];
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
  • ode solver ode45 is used in this matlablab program. The results are stored in a array or row matrix of two coloumns, for the values of diaplacement and velocity at different time periods.
  • ode_func is the function are made to solve the given equation. where the asumptions are made in the form of code.
  • The function ode45 takes 3 inputs. The 3 inputs are the function to be integrated, the time span and the initial conditions.
  • The initial condition would be the value of the function at t=0s.

The function can be provided in the form of an anonymous function, This function must then be input to ode45 as

[t,results] = ode45(@(t,y) ode(t,y,args), tspan, y0)

  • This is necessary to tell the function which arguments of the function are to be found by the solver.
% solving ode45
[t,results] = ode45(@(t,theta) ode_variable_function(t,theta,b,g,l,m),t_span,theta_0);
  • By the above code, we will able to solve the program by putting the 'ODE45' solver in the main program.

MAIN PROGRAM:

Inputs are taken as given in the problem

L = 1m,

m = 1kg,

b = 0.05,

g = 9.81m/s^2

Initial conditions are taken as,

Angular displacement = 0,

Angular velocity = 3rad/sec ,

at time t = 0,

The motion is simulated between 0-20sec.

% taking ode45 solver to solve a simple pendulum

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

% initial conditions
% angular displacement = 0 & angular vel = 3 rad/sec at time(t) = 0
theta_0 = [0; 3];

% oscillating for 20 seconds
t_span = linspace(0,20,400);


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

% plotting the angular disp & angular vel w.r.t to time
figure(1)
plot(t,results(:,1),'color','r')
xlabel('time (sec)')
ylabel('angular displacement (θ)')
legend('angular displacement')
grid on
hold on
figure(2)
plot(t,results(:,2),'color','b')
xlabel('time (sec)')
ylabel('angular velocity (dθ/dt)')
legend('angular velocity')
grid on 
hold off

% to create an animation, use 'for loop'

% for loop
% counter value is defined

ct = 1;
for i = 1:length(results(:,1))
    x_start = 0;
    y_start = 0;
    
    x_end = l*sin(results(i,1));
    y_end = -l*cos(results(i,1));
    
    % plotting animation
    figure(3)
    plot([x_start x_end],[y_start y_end],'linewidth',3)
    hold on 
    plot(x_end,y_end,'o','markersize',20,'markerFacecolor','r')
    hold on
    plot([-3 3],[0 0],'linewidth',2)
    axis([-3 3 -3 3])
    pause(0.01)
    hold off
    
    M(ct) = getframe(gcf);
    ct = ct +1;
    
end

% animation of the pendulum

movie(M)
videofile = VideoWriter('Simple pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

OUTPUT:

Figure(1) & (2) : represents the angular displacement and angular velocity changes with respective to time.

Image of a simple pendulum:

Animation of the simple pendulum:

CONCLUSION:

  1. Thus the moving of a simple pendulum is obtained.
  2. Therefore, angular displacements and angular velocities are changign with respect to time.
  3. By 'ODE's method we can able to solve the dynamics of a vehicle.
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.