Solving Second order ODE S
SOLVING SECOND ORDER ODE'S
Introduction: ODE is used to describe the transient behavior of a system. A simple example is a pendulum. 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.
The main objective here 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/s2,
L = length of the pendulum in m,
m = mass of the ball in kg,
b=damping coefficient.
Θ = displacement
PROGRAM
- ODE Function Code:
%ODE function 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
- ODE Main Code:
%program to simulate a simple pendulum by solving second order ODE
clear all
close all
clc
% input parameters
l=1;
m=1;
b=0.05;
g=9.81;
theta_0 = [0;3];
t_span = linspace(0,20,800);
% performing ODE45 command to solve second order ODE
[t, results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, theta_0);
% plots of time vs angular displacement & time vs angular velocity
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('Plot')
xlabel('time')
% using for loop command for different values of time of the simple pendulum
figure (2)
ct=1;
for i=1:length(results(:,1))
THETA=results(i,1);
%Coordinates
x0=0;
y0=0;
x1=1*sin(THETA);
y1=-l*cos(THETA);
plot([-1.5 1.5],[0 0])
hold on
plot([x0 x1],[y0 y1],'color','b')
% plotting of mass by creating a sphere
plot(x1,y1,'-o','markersize',30,'color','r')
hold off
axis([-1.5 1.5 -1.5 5.5])
pause(0.0001)
M(ct) = getframe(gcf);
ct = ct+1
grid on
end
movie(M)
videofile=VideoWriter('second_ode_pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Code explanation :
Step 1: Always Proceed with clear all, close all and clc are entered, which helps in clearing all the computed data in the editor window, command window, and workspace so that all the previous data would be cleared to run the new program.
Step 2 : The input parameters of the given simple pendulum are entered i.e,
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s^2.
Step 3: To solve the two first-order ode, we need to specify the initial conditions i.e the angular displacements and angular velocity as shown in the program.
Step 4: With the time limit of 0 to 20 sec for the motion of a simple pendulum, we can assume 100 intervals in between.
By using linespace command we can get for each value in the time of 0 to 20 sec with 100 intervals which creates an image that represents the position of the pendulum.
Step 5: The ODE 45 command is used to solve the second-order ODE, for this to happen we need to call for the function, which in the program is called by "ode_func" which is shown in 2nd program, this will help us to simply the program and ODE is solved in here & when it is called it to the main function assigning its input parameters, it provides us the required output.
In the function code, the two first-order ode solutions are written and theta1 and theta2 values are assigned to theta(1) & theta(2) respectively to call them in the main function using the word "theta".
Step 6: Next the plot command is used in the main program to obtain a plot of time vs angular displacement & time vs angular velocity as shown in the program.
Step 7: Using the for loop command for obtaining various angles of displacements of the simple pendulum for different values of time in the ODE is created by giving the initial condition x0 and y0, and then x1 y1 conditions are specified for the pendulum.
Step 8: Next with the help of marker command we create a sphere, by selecting a random color code as shown in the program.
Step 9: The axis is specified and with the help of pause command we can pause the movement.
Step 10: Then to create a movie and save our plot so that the animation would be like a continuous form, we have a command called ct and let us set the counter to be 1 to save the movie, and the frames of various positions of the pendulum will be then stored to ct = ct+1; which will be then called in by getframe command to call all the plotted frames which can be displayed with the given values of pause making it like a movie.
Step 11: Creating a movie – To create a movie, we use a movie command, for all the values obtained for the M variables.
In order for this to happen we need a place/file to store our video, and to do this we use videowriter command to create a new file for storing our movie animations.
Now we open the videos for writing, using open command and then the values obtained from variable M will be written by the command ‘writevideo’
In the end, after the file is written we need to close the file by using the close command.
OUTPUT:
Youtube Link:
SCREENSHOT:
ERRORS ENCOUNTERED:
There are no major errors except spelling mistakes.