Challenge

Week 4.1 - Solving second order ODEs

AIM: Write a program in MATLAB\Octave that will simulate the pendulum motion.

DESCRIPTION: In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum.

Figure1: A simple pendulum.

A simple pendulum is one that can be considered to be a point mass suspended from a string or rod of negligible mass. It is a resonant system with a single resonant frequency.

GOVERNING EQUATIONS: The way the pendulum moves depends on Newton's second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the "ball" w.r.t time.

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.

Write a program in Matlab\Octave that will simulate the pendulum motion, just like the one shown in the start of this challenge.

Given Values:-

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0.

When you do this you will get the position of the pendulum as a function of time. Use this information to animate the motion of the pendulum, as shown in the below figure.

CODE

Function Code -

% splitting the second order derivative into two prts
function [dtheta_dt] = second_order_ode_pendulum (t,theta,g,l,m,b)
theta1 = theta(1)
theta2 = theta(2)
% need to look the equation in two two parts
dtheta1_dt = theta2
dtheta2_dt = (-(b/m)*theta2)-((g/l)*sin(theta1))
dtheta_dt = [dtheta1_dt;dtheta2_dt]
end

Main code -

% code to simulate the pendulum motion
clear
close
clc
%given parameters
g = 9.81;
l = 1;
m = 1;
b = 0.05;
% we need to represent initial conditions to the problem
theta_0 = [0 3];
% given time interval or time span
time_given = linspace(0,20,400);
% write the ODE45 solver now
[t, outputs] = ode45(@(t, theta) second_order_ode_pendulum(t, theta, g,l,m,b), time_given,theta_0);
figure(1)
plot(t, outputs(:,1))
hold on
plot(t, outputs(:,2))
xlabel ('Time')
ylabel ('Angular displacement vs Angular velocity')
legend ('Angular displacement','Angular velocity')
grid on
figure(2)
plot(outputs(:,1), outputs(:,2))
xlabel ('position')
ylabel ('Angular velocity')
legend('Angular velocity')
theta2 = outputs(:,1);
ct = 1;
for i = 1:length(theta2)
theta3 = theta2(i);
% for fixed bar and movung bar of a pendulum
x0 = 0;
y0 = 0;
x1 = 1*sin(theta3);
y1 = -1*cos(theta3);
% giving axis, color for the pendulum bob, etc.,
figure(3)
plot([-1.1 1.1],[0,0],'linewidth',3)
hold on
plot([x0 x1],[y0 y1], 'g','linewidth',3)
hold on
plot(x1,y1,'o','markers',20,'markerfacecolor','m')
xlabel('x-axis')
ylabel('y-axis')
hold off
grid on
axis ([-1.5 1.5 -1.5 0.5])
% time frame to get the the video
pause(0.003)
M(ct) = getframe(gcf);
ct = ct+1;
end
% code to run and save the video
movie(M)
videofile = VideoWriter('Simple Pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Code Explanation:-

Before writing the code, it's important to start with clear all, close all and clc commands since

Clear all - removes data from the workspace, freeing up system memory

Close all- deletes the current figure or the specified figure

clc - clears all input and output from the Command Window display, giving you a "clean screen

Function Code - In this we create a function code for splitting the second order derivation into two parts dtheta1_dt and dtheta2_dt and combine those two into one as dtheta_dt

Main Code - in main code we input all the given values, then the initial conditions are coded into the code. In second step we create an array for given time using 'Linspace' command then we create Solver code in thirsd stepusing 'ODE45' code then we plot all of our findingwe plot 2 figures.

The final step of the code is to create a movie of the animation of the Pendulum, so the movie command was used. The VideoWriter command was used to assign the video format, for this project .avi was used as the format. The open command was used to open the video. writevideo command was used to write the video with plot frames. Finally, the close command was used to close the video file.

USE OF THE FOLLOWING FUNCTIONS USED WHILE PROGRAMMING

FOR LOOP- For loop is used here for the iteration of the theta values from 0 to 90 degrees I is used as the outer loop and J is used as the inner loop ,i is used for the movement of the link 1 and J is used for the movement of link 2.

PAUSE - We have paused the code so that the process can be visualised more clearly .

Ct-Ct is used for saving the frames in order to create a movie of the frames

Getframe-Similarly getframe is used to capture the frame and then by fast movement of those frames it can be converted into a movie

Movie -Movie command is used to create a movie of the frames captured .

VideoWriter -It is to create a Video file of the movie created and save in the pc hard disc.

Plot - The plot command is used to plot the graphs of the coordinates provided.

Axis- The axis command is used to create the axis upon which we are going to plot the coordinates.

Linspace-The linspace command is useful in creating an array of values specified by the user.

RESULT:- https://youtu.be/VhCkijFMlVg

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.