Project

Second order ODE

clear all
close all
clc

l =1;   %length
b = 0.05; %damping coefficient
m = 1; %mass
g = 9.81;
x0 = 5;
y0 = 5;
%initial conditions
theta_0 = [0;3];
 

%timespan
t_span = linspace(0,20,500);

%solving ODE
[t,disp]=ode45(@(t,theta) ode_func(b,l,g,m,theta,t),t_span,theta_0);

figure(1)
hold on
plot(t,disp(:,1))
xlabel(\'time\')
ylabel(\'disp\')
hold off
 
figure(2)
hold on
plot(t,disp(:,2))
xlabel(\'time\')
ylabel(\'angular velocity\')

ct = 1

figure
for i = 1:length(t_span)
      
    x(i) = x0 +l*sin(disp(i,1));
    y(i) = y0 -l*cos(disp(i,1));
    plot([4.5 5.5],[5 5],\'color\',\'y\',\'linewidth\',2)
    hold on
    plot([x0 x(i)],[y0 y(i)],\'color\',\'r\');
    plot (x(i),y(i),\'o\',\'Markersize\',5,\'color\',\'b\');
    hold off
    
    axis([4 6 3 6]);
    pause(0.001);
  
    M(ct) = getframe(gcf);
    ct = ct+1;
    
end

movie(M);
video = VideoWriter(\'test.avi\',\'Uncompressed AVI\');
open(video)
writeVideo(video,M)
close(video)


%writing the code for the ode function:

function [dtheta_dt]=ode_func(b,l,g,m,theta,t)
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
    

Aim of the project :To write a program to simulate the transient behaviour of a simple pendulum and to create an animation of it\'s motion.

This was the second order ODE given to us for damped oscillatory motion of a simple pendulum.

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 for solving the second order ODE, we will first convert this second order ODE into a number of first order ODEs.

We will assume two variables theta1 and theta2, where theta2 is the first order differential of theta1 and theta1 = theta. Now , using the equationn given we will make an eqauation for theta2 where

theta2 = -b/m*theta1 - g/l*sin(theta1)

We will separate funcion for that with the name ode_func

After having written the function, we will use in-built function ode45 to solving the differentail equation to get values of theta (both theta1 and theta2) at different time instances.We will get 500 values of theta1 and theta2 where theta1 will be first column of the solution and theta2 will be second column of the solution.

Now ,we will start plotting of the function:

1) plot time vs theta1(displacement) = plot(t,disp(:,1))

2)plot time vs angular velocity = plot(t,disp(:,2))

NOw , we will begin with the animation of the pendulum motion. For this, we will plot displacement points within the time period of 20seconds. We will keep one end of the string fixed and other end shall move according to the solution of the ODE i.e. column 1 of the theta solution. WE will use a simple for loop for execution. I used the origin as (5,5) for plotting animation.

for i = 1:length(t_span)

x(i) = x0 +l*sin(disp(i,1));
y(i) = y0 -l*cos(disp(i,1));
plot([4.5 5.5],[5 5],\'color\',\'y\',\'linewidth\',2)
hold on
plot([x0 x(i)],[y0 y(i)],\'color\',\'r\');
plot (x(i),y(i),\'o\',\'Markersize\',5,\'color\',\'b\');
hold off

axis([4 6 3 6]);
pause(0.001);

M(ct) = getframe(gcf);
ct = ct+1;

After that, we will use VideoWriter command to store the movie made by capturing frames.

Errors encountered :

1) In line 41, I had used line command . I changed line command to plot command and the problem got solved.

YOUTUBE LINK FOR THE VIDEO ANIMATION

https://www.youtube.com/watch?v=XwiQrE2aGfo

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.