Solving second order ODEs
AIM:
To plot the graph of displacement and velocity v/s time and animate the motion of bob.
GOVERNING EQUATION:
The ODE representing the motuion of the simple pendulum with damping is shown below:
`(d^2θ)/dt^2+b/m*(dθ)/dt+g/l*sinθ=0`
Where,
b = damping coefficient
m = mass of the bob(kg)
g = acceleration due to gravity(`m/s^2`)
l = length of the string(l)
GIVEN VALUES:
l = 1m,
m = 1kg,
g = 9.81`m/s^2`
b = 0.05
OBJECTIVE:
- To plot the graph of velocity and displacement v/s time.
- To animate the motion of the bob.
The above figure shows the motion of the bob.
WORKFLOW OF THE PROJECT:
- The value of l,m,b,g are given and the time varies from 0-20sec so we plot the graph of displacement and velocity v/s time.
- Then we plot the motion of the bob.
- At last we merge all the motion that is obtained and make the animation of that.
PROGRAMMING LANGUAGE USED- MATLAB
MATLAB CODE:
MAIN PROGRAM:
%program for solving second order differential equation
clear all
close all
clc
%input parameters
l = 1; %length in m
m = 1; %mass in kg
b = 0.05; %damping coefficient
g = 9.81; %acceleration due to gravity m/s^2
%intial condition
theta_0 = [0;3]; %[angular displacement,angular velocity]
%time plots
t_span = linspace(0,20,500);
%solving the ode
%here ode45 is the inbuilt command to solve the differential equation
[t,results] = ode45(@(t,theta) ode_function(t,theta,l,m,b,g),t_span,theta_0);
%plotting angular displacement
figure(1)
plot(t,results(:,1))
hold on;
%plotting angular velocity
plot(t,results(:,2))
xlabel('Time');
ylabel('Plot');
plot([0,20],[0,0],'color','black');
legend('Angular displacement','Angular velocity');
%loop counter variable ct
ct = 1;
figure(2)
for i = 1:length(results(:,1))
theta0 = results(i,1);
%fixed point of the string
x0=0;
y0=0;
%free point of the string
x1=l*sin(theta0);
y1=-l*cos(theta0);
%plotting of bob motion
plot([-1,1],[0,0],'linewidth',5,'color','black');
line([x0,x1],[y0,y1],'linewidth',3)
hold on
plot(x1,y1,'o','markers',15,'markerfacecolor','g');
axis([-2 2 -1.5 0.5]);
pause(0.01);
M(ct)=getframe(gcf);
hold off
ct = ct + 1;
end
%making of the movie from the collected frame
movie(M)
%saving the movie in avi format
videofile=VideoWriter('bobmotion.avi','Uncompressed AVI')
%opening thevideo file
open(videofile)
writeVideo(videofile,M)
%closing thevideo file
close(videofile)
FUNCTION PROGRAM:
function [dtheta_dt] = ode_function(t,theta,l,m,b,g) %initializing the function
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
CODE EXPLAINATION:
- Initially, we initialize all the input parameters like l,m,b,g which are given in the problem
- After that the time span over which the motion of the bob is to be observed is also defined.
- After that we write the function program,here we should remember that the function name and file name should be same.
- Then we solve the ode by using the inbuilt ode solver 'ode45' in matlab.
- Then we plot the graph of angular displacement and angular velocity v/s time .
- At last after plotting we animate the motion of the bob by using the movie command.
OUTPUT:
The plot of angular displacement and angular velocity v/s time is shown below:
The animation link of motion of bob of simple pendulum is given below:
CONCLUSION:
Thus we have seen the plot of angular displacement and angular velocity v/s time and also the animation of bob motion of simple pendulum was obtained.
ERRORS OCCURED AND IT'S SOLUTION:
Initially it was plotting all the position of the bob without deleting the previous one as shown below:
To overcome this error i used 'hold off' command on line number 55 as shown below:
After using this command i was able to obtained the output as required.