Project

2nd order ODE and pendulum animation

2nd order ODE and pendulum animation

AIM: To write a program in Matlab that will simulate the pendulum motion.

ASSUMPTIONS:

1. The value of acceleration dueto gravity is (g) 9.81 m/s^2

2. The length of the pendulum is 1 meter.

3. The damping coefficient is 0.05.

4. Tha mass of the pendulum bob is 1 kg.

THEORY:

The motion of the pendulum can be easilty represented by the following dufferential equation:

where:

`theta` = angular displacement of the bob

t = time period

b = damping coefficient

m = mass of the pendulum bob

g = accelration due to gravity

L = length of the pendulum string

`(d theta)/dt` = angular velocity

`(d^2theta)/(dt^2)` = angular acceleration

MATLAB CODE FOR FUNCTION:

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

Explanation:

1. dtheta_dt is a function of the parameters (t,theta,b,g,l,m)

theta(1) is the value assigned to theta1

theta(2) is the value assigned to theta2

2. dtheta1_dt is equal to theta2 and dtheta2_dt is equal to -(b/m)*theta2-(g/l)*sin(theta1)

3. dtheta_dt is 2 row and 1 column matrix

MATLAB CODE FOR MAIN PROBLEM:

clc
clear all
close all

%Constant Parameters
b = 0.05;
g = 9.81;
l = 1;
m = 1;

%Initial Conditions
theta0 = [0;3];
time_span = linspace(0,20,500);

%Solver Function
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),time_span, theta0);

%Plotting the results
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('Time')
ylabel('Plot')
grid on


figure(2)

%counter for frame
ct = 1;
for i = 1:length(time_span)
    x0 = 0;
    y0 = 0;
    x1 = l*sin(results(i,1));
    y1 = -l*cos(results(i,1));
    plot([-0.5 0.5],[0 0],'linewidth',10)
    hold on
    plot([x0 x1],[y0 y1],'color','g','linewidth',4)
    axis([-3 1 -3 1])
    hold on
    plot(x1,y1,'o','MarkerSize',40,'MarkerFaceColor','r')
    hold off
    axis([-1 1 -1.5 1])
    pause(0.001)
    M(ct) = getframe(gcf);
    ct = ct+1;
    grid on
end

%Animation
movie(M)
pendulum_movie = VideoWriter('2nd order ODE pendulum animation.avi','Uncompressed AVI');
open(pendulum_movie)
writeVideo(pendulum_movie,M)
close(pendulum_movie)

Explanation:

1. Take inputs such as b,g,l,m

2. Assume proper initial states for the time span and the angular displacement

3. Apply the in built sover function in MATLAB, i.e ode45, which basically acts as a solver of the ODE. It calls the function ode_func and accesses the values. It basically integrates the values of angular velocity at various time intervals and stores the results in matrix [t,results]

4.Plot the data obtained with a graph of Plot versus Time, by plotting the time and the results.

5. For the animation, we use the for loop by separating the frames and stitching them together. This can be achieved by setting initial value of counter frame equal to 1.

6. For aesthetic purposes, the color and the size of the bob can be adjusted.

7. Finally use the writeVideo command to stitch all the frames together.

8. Save the file and close it.

ERRORS FACED DURING CODING:

1. Wrongly used the command to set the color of the bob. The correct command is MarkerFaceColor

2.

Forgot to use the symbol '@' in the ode45 function. Corrected it.

CONCLUSION:

1. MATLAB can be efectively used to solve 2nd order ODE's

2. The graph of plot versus time can be easily obtained.

3. The plot abtained is as follows:

4. The animation of th pendulum is done through MATLAB. The YouTube link of the same is provided below:

https://youtu.be/4-DUIXJQYZ4

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.