SOLVING SECOND ORDER DIFFERENTIAL EQUATION USING MATLAB.
objective :to write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping.
SECOND ORDER EQUATION FOR MOTION:
d²θ/dt²+(b/m)(dθ/dt) +(g/L)sinθ=0
Consider dθ/dt=θ1
Thus dθ²/dt²=dθ1/dt
Eq : d²θ/dt²+(b/m)(dθ/dt) +(g/L)sinθ=0
Become dθ1/dt+(b/m)θ1+(g/L)sinθ=0
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.
Now we write a program in Matlab that will simulate the pendulum motion, just like the one shown in the start of this challenge.
we use input:
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
we Simulate the motion between 0-20 sec, angular displacement=0,
angular velocity=3 rad/sec at time t=0.
PROGRAM FOR CREATING ODE SOLVER FUNCTION:
clear all
close all
clc
function[dtheta_dt] =ode_function(t,theta,b,g,l,m)
theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt = theta(2)
dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt = [dtheta_dt;dtheta2_dt];
end
EXPLANATION:
1. we create array that store differential equation
2.function definition gives some attributes that Definevas function parameters.
3.the output of function is array storing solution attributes of equation.
PROGRAM FOR SIMULATION OF SIMPLE PENDULUM IN MOVIE FILE:
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=0.1;
ct=1;
%initial condition
theta_0 = [0;5];
%time points
t_span = linspace(0,10,500);
%solve ode
[t,results] = ode45(@(t,theta),ode_function(t,theta,b,g,l,m),t_span,theta_0)
plot(t,results(:,2))
hold on
grid on
plot(t,results(:,2))
axis([0 20 -3 3])
ylabel('plot')
xlabel('time')
x0=1;
y0=1;
ct=1;
cord = results(:,1);
for i=1:length(cord)
figure(2)
x1 = 1-l*sin(cord(i));
y1 = 1-l*cos(cord(i));
plot([0,2],[1,1],'linewidth',5)
hold on
plot([1,x1],[1,y1],[x1,x1],[y1,y1]
hold off
axis([0 d2 -0.5 1.5])
grid on
pause(0.01)
M(ct) = getframe(gcf);
ct = ct+1;
end
movie(M)
videofile = VideoWriter('pendulam.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
OUTPUT MOVIE FILE:
CONCLUSION:
thus we obtain simulation of simple pendulum.