Week 3 - Solving second order ODEs
AIM:
To write a MATLAB program which solve the following ODE. This ODE represents the equation of motion of a simple pendulum with damping. that will simulate the pendulum motion.
THEORY OF ODE :(Ordinary differential equation)
An ordinary differential equation (or ODE) is an equation involving derivatives of an unknown quantity with respect to a single variable.
The purpose of this exercise is to enhance your understanding of linear second order homogeneous differential equations through a modeling application involving a Simple Pendulum which is simply a mass swinging back and forth on a string. Like the mass on a spring application, this model problem is representative of a wide variety of problems described by second order linear differential equations.
SOLUTION:
- THE Pendulum oscillation it's like a Simple Harmonic Motion. That describe hown an object's velocity oscillates proportional to the amount of displacement equilibrium can be used to describe the equation of a pendulum.
- In pendulum motion ,Three Forces Work Directly On The Pendulum
- Mass ON The BOB
- Gravity
- Tension In The String
Mass and gravity both work vertically downeards.since the pendulum doesn't move up or down , the vertical component of the string tension cancels out the mass and gravity.
GIVEN:
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.

EQUATION USED:
`{d^2theta}/{dt^2}+b/m {d theta}/{dt}+g/lsintheta=0`
where,
`theta=theta1`
`{d theta}/{dt}={d theta_1}/dt=theta_2` (1St ODE)
`{d^2theta }/dt^2={d^2theta_1}/{dt}=d/dt({d theta_1}/{dt})=d/dt(theta_2)`
BY Rewriting the main equation we get,
`{d theta_2}/dt+b/m(theta_2)+g/lsin theta_1=0=> {d theta_2}/dt=-b/m (theta_2)-g/lsin theta_1` (2nd ODE)
BY PUT THE ABOVE TWO ODEs IN THE MATRIX FROM WE GET:
`d/dt[theta_1*theta_2]=[theta_2*-(b/m)theta_2-(g/l)sin theta_1]`
MAIN PROGRAM:
clear all
close all
clc
b=0.05; % THERE IS A Damping Coefficient.
g=9.81; % THERE IS GRAVITY (m/s^2)
l=1; % THERE IS LENGTH OF THE Pendulum (m)
m=0.1; % THERE IS MASS OF THE BALL (kg)
%INITIAL CONDITION
theta_0=[0;3]; % THERE IS A INITIAN CONDITION OF PENDULUM AND THE FIRST VALUE OF DISPLACEMENT & Second One Is ANGULARVELOCITY
%TIME POINTS
t_span=linspace(0,20,500); %THERE IS ARRAY IT'S CONTAIN 500 VALUES OF 0 TO 20
% SOLVE ODE
[t,results] = ode45(@(t,theta)ode_function(t,theta,b,g,l,m),t_span,theta_0); % THERE IS ANONYMOUS FOUNCTION THAT IS ODE45 IS USED SOLVE OUR ODE
figure(1) % THERE IS A PLOT COMMANT TO ANGULAR DISPLACEMENT AND VELOCITY
plot(t,results(:,1)) % COMMAND TO PLOT THE ANGULAR DISPLACEMENT
hold on % THERE IS A PREIVES PLOT HOLDING TO CURRENT PLOT COMMAND
plot(t,results(:,2)) % COMMAND TO PLOT THE ANGULAR VELOCITY
xlabel('time') % THERE IS CALLED X AXIS OF TIME
ylabel('velocity') % THERE IS CALLED Y AXIS OF VELOCITY
legend("angular displacement","angular velicity")
title("Simple Harmonic motion")
ct=1; % THERE IS A COUNTER VARIABLR THE VALUE IS 1
for i = 1:length(t_span)
x_0=0; % THERE IS A VALUE OF X_0
y_0=0; % THERE IS A VALUE OF Y_0
x_1 = l*sin(results(i,1)); % THER IS A CALCULATE THE VALUE OF Angular displacement
y_1 = -l*cosd(results(i,1));% THERE IS A CALULATE THE VALUE OF Angular velocity
figure(2)
plot([x_0 x_1],[y_0 y_1],'linewidth',3,'Color','b') % THERE IS A PLOT COMMAND ON STRING
hold on %THERE IS A PREIVES PLOT HOLDING TO CURRENT PLOT COMMAND
plot([0.8 -0.8],[0 0],'linewidth',2,'color','g') % THERE IS COMMAND ON THE STRING FIXED POINT
plot(x_0,y_0,'.','Markersize',30,'MarkerFaceColor','b')% THERE IS COMMAND ON STRING FIXED POINT SIZE MARK
plot(x_1,y_1,'.','markersize',75,'MarkerFaceColor','g') % THIS COMMAND HELP US CREATE A SPHERE IN MATLAB
axis([-1 1 -1.3 1]) %there is a Range of x and y axis
grid on
hold off
M(ct)=getframe(gcf); % M is array its carry the number of figure
ct=ct+1; % ct is a counter variable its over write the counter variable
end
movie(M) % there is a plot movie command its carry the (M-string)
videofile = VideoWriter('pendulam_4ode.avi','Uncompressed AVI'); % there is video writeing command on (file name)
open(videofile) % OPEN A VIDEO COMMAND
writeVideo(videofile,M); % THERE IS (M)movie string to WRITE VIDEO COMMAND
close(videofile) % CLOSE THE VIDEO FILE COMMAND
THIS FUNCTION HELP US BREAK UP THE 2order ODE INTO FIRST ORDER ODE's
alredy we know
`{d theta_1}/dt=theta_2`
`{d theta_2}/dt=[-b/mtheta_2-g/lsin theta_1]`
ODE FUNCTION:
function[dtheta_dt] = ode_function(t,theta,b,g,l,m)
theta1 = theta(1) % THERE IS VALUE OF ANGULAR DISPLACEMENT
theta2 = theta(2) % THERE IS VALUE OF ANGULAR VELOCITY
dtheta1_dt = theta2; % THERE IS A 1st ORDER ODE
dtheta2_dt = -(b/m)*theta2-(g/l)*sin(theta1); % THERE IS A 2st ORDER ODE
dtheta_dt = [dtheta1_dt;dtheta2_dt];
end
[dtheta_dt] - is soluction vector
And ,there is contian the two values
- One is Angular displacement
- another one is Angular velocity
OUTPUT:


CONCLUSION:
TO FINALY SOLVE THE 2ND ORDER ODE FUNCTION ,AND
THE Simulation of simple pendulum motion (2nd Order ODE) through the link in below:
https://skill-lync.com/projects/solving-second-order-odes-in-simple-pendulum
And , Facing error on write this program to attached the CHOOSE FILLE