Week 3 - Solving second order ODEs
SOLVING SECOUND ORDER ODE
AIM:
To solve a secound order ODE and simulate the simple pendulam motion by using MATLAB.
GIVEN DATA'S:
- L=1 metre,
- m=1 kg,
- b=0.05.
- g=9.81 m/s2.
- Angular displacement = 0
- Angular velocity = 3 rad/sec
- T = 0
EXPRESSION:
- g = Gravity in m/s2,
- l = Length of the pendulum in m,
- m = Mass of the ball in kg,
- b =Damping coefficient.
FORMULA TO BE USED :
SIMPLE PENDULAM :
THE ABOVE PICTURE WILL SHOW AS THE SIMPLE PENDULAM
A simple pendulum consists of a mass m hanging from a string of length L and fixed at a pivot point P. When displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion.A pendulum is a weight suspended from a pivot so that it can swing freely. When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position.
MATLAB 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
The above code which is shows as function codes used in matlab. The function codes could be called by main code as a ODE 45 .
ODE MAIN PROGRAM:
clear all
close all
clc
%inputs
b = 0.05;
g = 9.81;
l=1;
m = 1;
% initial conditions
theta_0 = [0;3];
% time points
time = linspace(0,20,200);
%solve ODE
[t, result] = ode45(@(t,theta)ode_func(t, theta, b, g, l, m),time, theta_0);
% PLOTTING
figure(1)
plot(t,result(:,1),'color','g')
hold on
plot(t,result(:,2),'color','b')
xlabel('time')
ylabel('Angular velocity and displacement')
legend('change in displacement','change in angular velocity')
hold off
figure(2)
plot(result(:,1),result(:,2))
xlabel('position')
ylabel('anglar velocity')
% ANIMATION
c = 1
for i=1 : length(result(:,1))
x0 = 0
y0 = 0
x1 = l*sin(result(i,1))
y1 = -l*cos(result(i,1))
plot([-1,1],[0,0],'linewidth',4,'color','y')
hold on
plot([x0 x1],[y0 y1],'linewidth',2,'color','r')
hold on
plot(x1,y1,'o','markers',20,'markerfacecolor','b')
grid on
axis ([-2 2 -2 2])
pause(0.03)
hold off
M(c) = getframe(gcf)
c = c+1
end
movie(M)
videofile = VideoWriter('ode_main','uncompressed avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
STEPWISE EXPLAINATION:
CLEAR ALL -it clear all privious codes.
CLOSE ALL -it close all matlab works.
CLC - I'ts clear command window.
INPUTS:
- L=1 metre,
- m=1 kg,
- b=0.05.
- g=9.81 m/s2.
INITIAL CONDITION:
theta_0 = [0;3];
This is intial starting condition to be used .Than the initial starting displacement is zero and starting velocity is 3.
TIME POINTS:
time = linspace(0,20,200);
Time intervel is - (0-20s)
Then 200 values are calculated between 20s.
%SOLVE ODE:
[t, result] = ode45(@(t,theta)ode_func(t, theta, b, g, l, m),time, theta_0);
ode 45 is call function code.Others are data which is taken from Given .
PLOTTING:
Plotting which is used to plot the values.
ANIMATION:
In this function which is used to animate the simple pendulam. In this function having the line width and color of the line . The initial stage of plottig is [x0 x1],[y0 y1]
FINAL VIDEO CODE:
In this code which is used to make a animated video for the ode main file,and save as a compressed avi file.
PLOTTED IMAGE:
SIMPLE PENDULAM ANIMATED MOVIE FILE:
THIS IS THE YOUTUPE LINK FOR ANIMATTED FILE,https://youtu.be/7T3iYyNBcHY
CONCULUTION:
Finally the Simple pendulam codeing and animatted successfully by using matlab.