Challenge

Week 3 - Solving second order ODEs

Aim:

Program to solve second-order differential equations using Matlab.

By taking an example of a simple pendulum in the program.

The way the pendulum moves depends on Newton's second law. When this law is written down, we get a second-order Ordinary Differential Equation that describes the position of the "ball" w.r.t time.

Similarly, hundreds of ODEs describe vital phenomena, and if we can solve these equations, we will be able to simulate any of these systems.

Govering equation:

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

Our objective is to write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping. In the above equation,

g = gravity in m/`s^2`,

L = length of the pendulum in m,

m = mass of the ball in kg,

b = damping coefficient.

To solve the above equation of ODE we have to reduce the order of the equation to lower order.

Let,

`theta = theta_1`

`(d theta)/dt = (d theta_1)/dt` = `theta_2` ----------- (1)

`(d^2 theta)/dt^2 = (d^2theta_1)/dt^2 = d/dt((d theta_1)/dt) = d/dt(theta_2)` ------------- (2)

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

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

`(d theta_2)/dt + b/m theta_2 + g/l sintheta_1 = 0` ------------------------------- (3)

`(d theta_2)/dt = -( b/m theta_2 + g/l sintheta_1)` -------------------------------- (4)

The simple differential equations are written in the form of a matrix called solution vector.

`d/dt [[theta_1],[theta_2]] = [[theta_2],[( -b/m theta_2 )-(g/l sintheta_1)]]` ---------------- (5)

`d/dt [[theta_1],[theta_2]] = theta` ---------------------- (6)

With the help of the above equation, we can solve the problems of a simple pendulum in Matlab by writing code.

Inputs for first program, where we are going to plot a graph between displacement and velocity with the help of function.

length of pendulum string (l) = 1 meter

mass of bob (m) = 0.1 kg

damping coefficient (b) = 0.05

gravity = 9.81 m/`s^2`

% Program to solve Second ODE problems
% Input

b = 0.01; % damping coefficient
g = 9.81; % gavity in m/s^2
l = 1;    % Length of pendulum string in meter
m = 0.1;  % Mass of in kg

% Initial condition
theta_0 = [0;5];

% time points
t_span = linspace(0,10,500);

% ODE 45 function to solve second order ode
[t, results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

% Plotting of results
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
% ODE 45 function to solve second order ODE 

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 the starting of the program input & initial condition of a simple pendulum are written. Then the equation is written in the form of code to solve the ODE in which Matlab inbuilt solver or function (ode45) is used with constant theta, t & another function is used, which is defined separately in function program. The ode 45 solver should require simple differential equation to solve any kind of higher order differential equation and by taking constants we can solve the higher order differential equation to a simple ordinary differential equation. In the last step of program result of solved ODE is plotted w.r.t. time using plot command.
Output is a graphical representation of velocity and displacement w.r.t. time.
The second program is to create an animation of a simple pendulum by using a second-order differential equation by using the ode solver function and plot a graph between angular velocity and displacement of a simple pendulum by using ode 45.
Clear all, close all, clc are used to clear the workspace, previous plot and command window.
For this program inputs are given,
damping coefficient (b) = 0.05 (n-m/s),
acceleration due to gravity (g) = 9.81 (m/s^2),
length of the string (l) = 1 (meter),
mass of the bob (m) = 1 (kg).
t_ span is a command which is used in the ode45 solver, which specifies the time span of the iterations that are going to solve the ordinary differential equation. t_span of the simple pendulum is set by using the linspace command then, theta value is given as a column matrix. After this, we use the ode45 function and the corresponding 'ode_func' function call in the main program. ode45 is a function in Matlab that solves ordinary or partial differential equations using a numerical method. In this problem, the highest order is 2. So, we have to take two constant values so that the equation is changed to a simple differential equation. The function should take at least 2 inputs to solve the equation to the approximate result. To get the accurate simulation of the pendulum corresponding to the second-order ODE with damping we have to use the for loop command. ode45 function, which solves the simple differential equations with certain specified commands, In this function we have specified the derivatives of theta for the equations to be solved.
The axis is fixed using the axis command. Marker command is used for making the bob of the pendulum. To plot the values of the angular velocity and the displacement of the bob we used the plot command. The label command is used to label the X & Y-axis. Legend command is used to label the angular velocity and displacement. To animate the motion of a simple pendulum, a for loop is used to create the specified set of points to keep the bob at different locations. At each and every point of the bob the 'ct' command, is used to count each & every position and store the data. Movie(M) is used to create a film of the stored data of the motion of the pendulum. videoWriter command specifies the name of the file and the type of data stored in it. The open command is used to open the file where the video is going to store. Writevideo command is used to create the video animation by using the M file. The close command is used to close the video file.
% Second ODE Problem program
b = 0.05; % Damping coefficient
g = 9.81; % gravity in m/s^2
l = 1;    % length of pendulum string in meter
m = 1;    % mass of bob in kg

% Initial condition
theta_0 = [0;3];
% time points
t_span = linspace(0,20,200);
% Solve ODE using function
[t, results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure('Name','Graph between time, displacement and angular velocity','NumberTitle','off')
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('Displacement and Angular velocity')
xlabel('Time')
legend('displacement','angular velocity')
grid on
hold off
% To create animation of simple pendulam
displacement = results(:,1);
angular_velocity = results(:,2);
ct=1;
for i = 1:length(displacement)
    % initial coordinates
    x0 = 0;
    y0 = 0;

    % last coordinates
    x1 = 1-1*sin(displacement(i));
    y1 = 1-1*cos(displacement(i));

    % Creating annimation
    figure('Name','Animation of simple pendulum','NumberTitle','off')
    % Base of pendulum
    plot([0 2],[1 1],'linewidth',4,'Color','g')
    hold on
    % bob of pendulum and String of pendulam
    plot([1 x1],[1 y1],[x1 x1],[y1 y1],'o','MarkerSize',15,'MarkerFaceColor','k','linewidth',3)
    hold on
    plot(x0,y0,'MarkerSize',3,'MarkerFaceColor','b')

    % axis setting
    axis([0 2 -0.5 2])
    pause(0.03)
    M(ct)=getframe(gcf);
    hold off
    ct=ct+1;
    
end

% Movie from captured frames
movie(M)
videofile = VideoWriter('Second_ode45_Simple_Pendulum.AVI','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Output:

A graph is Plotted between the time, angular velocity, and displacement of the motion of the simple pendulum.

The blue color curve is representing the displacement of the bob, which is starting from 0. The red color curve is representing the angular velocity of the bob, which starts from 3 rad/sec. At the end of the t_span, both the angular velocity and displacement become zero.

Results:

Animation of a simple pendulum using the ode45 solver has been generated and the link is attached below. Animation is made up of capturing the different positions of the bob and by connecting all the points of bob, the motion of a simple pendulum is created. here we reduced the second order differential equation to a simple differential equation.

YOUTUBE LINK:- https://youtu.be/8fH8kBU3wxA

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.