Challenge

Week 4.1 - Solving second order ODEs

AIM- Writing a program for solving the give ODE function in which the ODE represents the motion of the simple pendulum with damping.

GOVERNING EQUATION -

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

GIVEN DATA-

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

By Using

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

OBJECTIVE -

Writing a code for the ODE function with the animation video output.

BODY OF CONTENT -

1 - Given all the inputs required for the code animation.

2 - putting all the governing equation in the main file and the function file.

3 - By using the function file inputs in the main file function plated the. Graph displacement and the velocity.

4 - By using for loop command plot the simple pendulum animation.

5 - By using plot command did the animation.

CONCLUSON -

After writing the code we can conclude that the motion of the simple pendulum by using the 2nd order ODE and animated the simple pendulum with damping.

% inputs

1. ODE Function code-

% ODE_FUNCTION 

function [dtheta_dt]= ode_func(t,theta,b,g,l,m)

theta1 = theta(1);
theta2 = theta(2);
dtheta1_dt= theta(2);
dtheta2_dt = -(b/m)*theta2- (g/l)*sin(theta1);
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end

% ODE_FUNCTION

function [dtheta_dt]= ode_func(t,theta,b,g,l,m)

theta1 = theta(1);

theta2 = theta(2);

dtheta1_dt= theta(2);

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

dtheta_dt = [dtheta1_dt; dtheta2_dt];

end

1. ODE Main Code-

%second order ODE Function with Damping

%length_of_pendulum
l=1;

%mass
m=1;

%damping_coeficient
b=0.05;

%gravity
g=9.81;

%initial_condition
theta_0 = [0;3];

%time-point
t_sapn = linspace(0,20,100);

%slove_ ode

[t, results]= ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_sapn,theta_0);
figure(1)
title('parameter variation w.r.t. time plot')
plot(t,results(:,1))
hold on 
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
legend('displacement','velocity')
grid on
ct = 1;
angle  = results(:,1);

for i = 1 : length(t_sapn)
    ANGLE = angle(i);
    figure (2)
    title('simple pendulum animation')
    x0 = 0; 
    y0 = 0;
    y1 = -1*cos(ANGLE);
    x1 = 1*sin(ANGLE);

    %plotting

    plot([x0 x1],[y0 y1],'linewidth',4)
    hold on
    plot(x1,y1,'.','markers',50)
    hold on 
    plot([-5,5],[0,0],'linewidth',5)
    hold off
    axis([-9,9,-4,4])
    pause (0.01)
    M(ct) = getframe(gcf);
    ct = ct+1;
end
movie(M)
videofile = VideoWriter('pendulum animation.avi','Uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close (videofile)

%second order ODE Function with Damping

%length_of_pendulum

l=1;

%mass

m=1;

%damping_coeficient

b=0.05;

%gravity

g=9.81;

%initial_condition

theta_0 = [0;3];

%time-point

t_sapn = linspace(0,20,100);

%slove_ ode

[t, results]= ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_sapn,theta_0);

figure(1)

title('parameter variation w.r.t. time plot')

plot(t,results(:,1))

hold on

plot(t,results(:,2))

ylabel('plot')

xlabel('time')

legend('displacement','velocity')

grid on

ct = 1;

angle = results(:,1);

for i = 1 : length(t_sapn)

ANGLE = angle(i);

figure (2)

title('simple pendulum animation')

x0 = 0;

y0 = 0;

y1 = -1*cos(ANGLE);

x1 = 1*sin(ANGLE);

%plotting

plot([x0 x1],[y0 y1],'linewidth',4)

hold on

plot(x1,y1,'.','markers',50)

hold on

plot([-5,5],[0,0],'linewidth',5)

hold off

axis([-9,9,-4,4])

pause (0.01)

M(ct) = getframe(gcf);

ct = ct+1;

end

movie(M)

videofile = videowriter('pendulum animation.avi','uncompressed AVI');

open(videofile)

writeVideo(videofile,M)

close ('video file')

CONCLUSION-

By using all the inputs and the given method wriote a code and made the animation with damping for a simple pendulum.

Drive Link-

https://drive.google.com/file/d/1k-EOSuAbpD_MPiXa_ohiMy6TA4n3ORhF/view?usp=sharing

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.