Week 3 - Solving second order ODEs
Introduction:
A second order ordinary differential equation is an ordinary differential equation in which any derivatives with respect to the independent variable have order no greater than 2.
Ordinary differential equations helps us to solve the many engineering systems. Such as harmonic motions, vibrarions, damping,etc.
Second-order linear differential equations are used to model many systems in engineering.
A simple pendulum consists of a mass ‘m’ hanging from a string of length ‘l’ and fixed at a pivot point . When displaced to an initial angle ’θ’ and released, the pendulum will swing back and forth with periodic motion.
The equation is given by:
Explanation of the code :
Inputs are taken as given in the problem
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2
Initial conditions are taken as
Angular displacement = 0, Angular Velocity = 3 rad/sec, at time t = 0
The motion is simulated between 0-20sec
Solving ODE
Ode solver ode45 is used in this matlab program. The results are stored in an array of row atrix of two coloumns, for the values of displacement and velocity at different time periods.
Ode_func is the function made to solve the given equation . where the asumptions are made in the from of code .
Figure 1: plotting
Here the graph is plotted to get the wave form of the displacement and the velocity from the initial condtions.
Figure 2: plotting
Animating the transient motion of the simple pendulum:
Here we use the concept of the for loop . as the the bob moves with a velocity ,and displaces for the different time period. And recorded it and animated the path of the oscillations
The following steps are followed in writing the code:
1.collected inputs
2.assign the inputs to MATLAB.
3.programmed ode45 and required functions.
4.plotted the graph between angular disp, angular velocity vs time.
5.make the animation program.
writing a program to solve ODE:
clear all
close all
clc
%inputs
b=0.05;
g=9.81;
l=1;
m=1;
% initial conditions
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);
plot(t,results (:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
fig=1;
for i = 1:length(t)
%initial position of the pendulum
x0 = 0;
y0 = 0;
%final position
x1 = l*sin(results(i,1));
y1 = -1*cos(results(i,1));
%plotting
figure(2)
%base line
plot([-1,1],[0,0],'linewidth',4,'color','y');
% pendulum line
line([x0 x1],[y0 y1],'linewidth',3,'color','r');
hold on;
%pendulum ball
plot(x1,y1,'o','markers',30,'markerfacecolor','b');
grid on
axis([-2 2 -2 1]);
pause(0.01);
hold off;
M(fig) = getframe(gcf);
fig= fig+1;
end
% for genarating and storing the video file in avi format
movie(M)
videofile = VideoWriter('second_order_ode.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
- To solve the ode an ode function is required which accepts the the values of t, theta,b,g,l,m,t_span, theta_0,
so a function calle Ode_func is declared and the syntax for the function is as follows
%function for solving the 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
the above function consists of function declartion an ode_fun is the function statement or name and it acccepts the values of t,theta,b,g,l,m
in the next two lines the seond orer ode is split in to two
and in the end both the terms dtheta_1dt,dtheta2_dt are made in to an array of single variablee dtheta_dt which is a column matrix
Errors faced:
Not using the hold on & hold off command at the proper step.
Giving the proper value for the pause
Determining the x and y values
Steps To Overcome those errors:
Make sure the use of hold on and hold off commands after the proper plot command or the graph wont be as per requirement and to avoid the path trace to left in the graph
Use of proper value for the pause (in this case 0.0001) to get the proper animation form
In the motion of the simple pendulum the coridnates x, y are interchanges like
X=l*sinθ and Y=-l*cosθ
output:
Animation output youtube link: