## Simulating the motion of a pendulum using 2nd order ODE in MATLAB

This project explores the simulation of the motion of a pendulum for the equation of damped vibration of a pendulum.

Below is the equation (taken from "Challenges" page):

An attempt is made to solve the above equation, and then call it using ode45 to get an array of results vs time (position vs time). These positions are coordinates that are plotted over 20 seconds and then animated.

ODE Function:

function [dtheta_dt] = odefunc(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];

endfunction

Function here takes time, angle, damping factor, g, length and mass of pendulum as arguments and returns an array of 2 variables that represent angular acceleration and velocity. Here, solving multiple equations concept is used to arrive at variables that are returned and used as arguments for ode45 function in the main code below.

Main Code:

b = 0.05;
g = 9.81;
l = 1;
m = 1;

%initial condition
theta0 = [0; 3];

%time duration
tspan = linspace(0, 20, 500);

[t, results] = ode45(@(t,theta) odefunc(t, theta, b, g, l, m), tspan, theta0);

%plot
figure(1)
%subplot(4,4, [9, 10, 13, 13])
plot(tspan, results(:,1), 'color', 'b');
hold on
plot(tspan, results(:,2), 'color', 'r');
hold off
xlabel('time')
ylabel('plot')

figure(2)
%subplot(4,4, [11, 12, 15, 16])
plot(results(:,1), results(:,2));
ylabel('Angular Velocity')

count = 1;
for i = 1:length(results(:,1))
x0 = 0;
y0 = 0;
x1 = l*sin(results(i,1));
figure(3)
y1 = -l*cos(results(i,1));
%subplot(4,4, [1, 2, 3, 4, 5, 6, 7, 8])
plot([-l l], [0 0], 'linewidth', 3, 'color', 'k')
hold on
line([x0 x1], [y0 y1], 'linewidth', 4, 'color', 'b')
hold on
plot(x1, y1, '-o', 'markers', 20, 'markerfacecolor', 'r') %plotting bob
grid on
axis([-1.5 1.5 -1.5 1.5])
pause (0.000003)
hold off
file_text = sprintf('pendode%d.png',count);
saveas(gca,file_text)
count = count + 1;

endfor

Here, there are 500 instants of time from 0 to 20 seconds for which the motion is simulated.

ode45 uses the function, tspan and initial condition as arguments. Function "odefunc" returns the dtheta1 and dtheta2 values that are solved for to get "results", which is the position of the pendulum with respect to time.

Figure 1:

Plot of results vs time to show the effect of damping.

Figure 2:

Plot of angular velocity vs position.

Figure 3:

Here, for loop is used to continually generate plots of the pendulum at different values of results for the length of "results" (which we got from ode45). Line command generates the pendulum string (assumed weightless) at different points through the entire length of results and "markerfacecolor" generates the ball at the end.

Being octave, "sprintf" and "saveas" are used to get the frames, which are compiled using ImageMagick:

magick pendode%d.png[1-500] PendulumOdeAnim.mp4

Animation:

Pendulum Animation, solved using ODE45 on Octave

Error 1: Attempting lsode in octave

Apparently lsode has been removed in version 5.1+ and ode45 has been enabled. Rest of the code was done using ode45.

Error 2: Syntax error

Second equal sign. Deleted and changed.

Error 3: Subplot experimentation

Was attempting subplot. Was unsure of the syntax until it got clarified. Eventually followed below code to get it right. But eventially subplot was dropped (hence commented out in main code) and normal plots used.

subplot(4,4, [9, 10, 13, 13])


### Simulating the motion of a pendulum using 2nd order ODE in Python Arjun Bhat · 2019-12-09 17:11:02

This project explores the simulation of the motion of a pendulum for the equation of damped vibration of a pendulum. Below is the equation (taken from "Challenges" page): The above equation is solved using ODE function, and then odeint is used to get an ar Read more

### Air standard cycle graph plotting in Python Arjun Bhat · 2019-11-20 17:11:18

Project involves plotting an Otto Cycle graph in MATLAB The goal is to plot all strokes for a petrol operating by the otto cycle from intake, compression, power to exhaust, including both adiabatic curves. Description: The Otto Cycle is the idealised cycle that descr Read more

### Forward Kinematics of a 2R Robotic Arm in Python Arjun Bhat · 2019-11-18 16:21:42

Objective: The project involves simulating a 2-joint robotic arm.   Code and Description: import math import matplotlib.pyplot as plt import numpy as nm # Lengths l1 = 1 l2 = 0.5 # Angles ang1 = nm.radians(nm.linspace(0, 90, 19)) ang2 = nm.radians Read more

### Flow over bicycle in Python Arjun Bhat · 2019-11-18 16:09:30

Objective: To calculate the drag force for various drag coefficient and velocity values for a certain geometry.   Introduction: Drag force is the force exerted by the air as the vehicle moves through it. The force exerted is directly proportional to the square Read more

### Parsing NASA Thermodynamic data for multiple specified Gas Species and Calculating the Thermodynamic Properties Arjun Bhat · 2019-09-11 20:25:18

Aim: To parse the NASA Thermodynamc Properties data file, calculate thermodynamic properties and plot the characteristics. Introduction: NASA's thermodynamic properties file consists of multiple species of gases, their respective operating temperature ranges, and Read more

### Optimization of a function using Genetic Algorithm in MATLAB Arjun Bhat · 2019-09-03 08:51:45

The project aims at optimization of a stalagmite function and finding the global maxima. For this, we will be using the concept of genetic algorithm. Following are the steps used to optimise the function: Code the function given in a separate function code. This is Read more

### Curve Fitting using MATLAB Arjun Bhat · 2019-08-19 20:20:53

Project aims at finding the best fit curve for a given data and finding the error between the original and said curve. The data being used is that of Specific heat (Cp) vs Temeperature. Plot will be made to show variation of Cp with Temp first.  Code: % Data cp Read more

### Plotting of an Otto Cycle graph in MATLAB Arjun Bhat · 2019-08-12 19:40:08

Project involves plotting an Otto Cycle graph in MATLAB The goal is to plot all strokes for a petrol operating by the otto cycle from intake, compression, power to exhaust, including both adiabatic curves. Description: The Otto Cycle is the idealised cycle that descr Read more

### 2R Robotic Arm Simulation using MATLAB Arjun Bhat · 2019-07-28 08:19:59

Code and Description: The project involves simulating a 2-joint robotic arm. % Lengths l1 = 1; l2 = 0.5; % Angles ang1 = linspace (0, 90, 20); ang2 = linspace (0, 90, 20); count = 1; % loop for i = 1:length(ang1) ANG1 = ang1(i); for j = 1:length(ang2 Read more