Week 3 - Solving second order ODEs
AIM:-
To write a program in Matlab\Octave that will simulate the pendulum motion
Theory:-
In mathematics, an ordinary differential equation (ODE) is a differential equation containing one or more functions of one independent and the derivatives of those functions. The term ordinary is used in contrast with the term partial differential equation which may be with respect to more than one independent variable.
In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum
The way the pendulum moves depends on the Newtons 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, there are hundreds of ODEs that describe key phenomena and if you have the ability to solve these equations then you will be able to simulate any of theses systems.
In the above equation,
g = gravity in m/s2,
L = length of the pendulum in m,Write a program in Matlab\Octave that will simulate the pendulum motion,
m = mass of the ball in kg,
b=damping coefficient.
Objective:-
- To write a program that solves the above ODE.
- To write a program in Matlab\Octave that will simulate the pendulum motion
Details given:-
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.
Code:-
Main Program
clear all
close all
clc
%inputs
b=0.05;
g=9.81;
m=0.1;
l=1;
%initial condition
theta_0=[0;5];
%time points
t_span=linspace(0,10,500);
%Slove ODE
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
% Plotting Oscillation Graph of Simple Pendulum
subplot(5,4,[13,14,17,18]);
plot(t, results(:,1),'color','b');
hold on
plot(t,results(:,2),'color','r');
ylabel('Velocity & Displacement Osillation')
xlabel('Time 0-20 Sec')
title('Oscillation Graph of Simple Pendulum')
grid on
legend('Angular Displacement','Angular velocity')
hold off
subplot(5,4,[15,16,19,20]);
plot(results(:,1),results(:,2));
%Creating the Pendulum and plotting
ct=1;
for i=1:length(t_span)
x0=0;
y0=0;
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1));
subplot(5,4,[1,2,3,4,5,6,7,8,9,10,11,12]);
plot([x0 x1],[y0 y1],[-0.5 0.5],[0 0],'linewidth',2)
hold on
plot(x1,y1,'marker','o','markerSize',25,'markerFacecolor','r')
axis([-1.5 1.5 -1.5 0.5])
grid on
file_text=sprintf('simple%05d.png',ct)
saveas (gca, file_text)% saving images generated
pause(0.01)
ct=ct+1;
hold off
end
Function code:-
This code is used to solve the given 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];
Animation Video Link:-
Errors encountered:-
- Typing error in plotting syntax
- In using the ode45 function
Output:-
Workspace
Conclusion:-
From the animation we can see that the pendulum stops in the given interval.