SOLVING ODE USING MATLAB
AIM:
The aim of our project is to write a program to simulate the transient behaviour of a simple pendulum and to create an animation of it's motion by solving a 2nd order ODE using MATLAB.
PROGRAM:
%solving 2nd order ODE to simulate a pendulum
clear all
close all
clc
%inputs
l = 1;
m = 1;
b = 0.05;
g = 9.81;
%initial condition
theta_0 = [0;3];
%time span
t_span = linspace(0,20,500)
%solving ODE
[t, result] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0)
%creating animation of a simple pendulum
ct=1;
for i=1:length(result(:,1))
x0=0;
y0=0;
x1=l*sin(result(i,1));
y1=-l*cos(result(i,1));
%plotting
plot([-1 1],[0 0],'linewidth',3,'color','b')
hold on
plot([x0 x1],[y0 y1],'linewidth',3,'color','black')
hold on
plot(x1,y1,'o','markersize',20,'markerfacecolor','r')
hold off
grid on
axis([-3 3 -3 3])
pause(0.03);
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('simple_pendulum.avi','uncompressed avi');
open(videofile);
writeVideo(videofile,M);
close(videofile);
PROGRAM OF FUNCTION 'ode_func'
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
INPUTS:
The inputs are 2nd order ODE,
`(d^2theta)/dt^2+b/m((d theta)/dt)+g/l(sin theta)=0`
and the values for
L = length = 1 metre,
m = mass = 1 kg,
b = damping coefficient = 0.05.
g = gravity = 9.81 m/s2
STEP 1:
By solving the 2nd order ODE we get two 1st order equations
`(d theta1)/dt=theta2`= eqn1
`(d theta2)/dt=-b/m (theta2)-g/l(sin theta1)` = eqn2
STEP 2:
After getting the above 1st order equations we code a program using our initial condition as displacement = 0, angular velocity = 3 rad/sec for a time span of 20 seconds starting from 0.
STEP 3:
Now for solving the ODE we are calling a function named ode_func into our main function. The coded file of the called function is a separate file which consists of the derivation derived to obtain the two 1st order equations
STEP 4:
Followed by the above step our next step is to create the animation of the pendulum. This is initiated by providing the value for ct=1 and followed by using the 'for' loop for generating new values of 'i' for each iteration. Here in the 'for' loop we are providing the values for coordinates
x0=0;y0=0 by considering centre point of all quadrants is zero.
x1=l*sin(result(i,1));y1=-l*cos(result(i,1)) as per the trignometric relation shown below
STEP 5:
Followed by the above step our next step is to plot the coordinates using plot command and create animation using the commands provided in the program.
ERRORS ENCOUNTERED WHILE PROGRAMMING:
1.Forget to use the parenthesis
2.Made an error in the trignometric formula of x1 and y1
1.By looking at the red hints popped in the command window.
2.By utilizing the doubt clearing live session.
3.By coding carefully and recaping the instructions provided during the course.
OUTPUT:
YOUTUBE LINK
https://www.youtube.com/watch?v=DOdWR1ibHqI&feature=youtu.be
CONCLUSION:
Thus the program to simulate the transient behaviour of a simple pendulum and to create an animation of it's motion is coded and executed successfully by uploading the video in youtube.