Week 3 - Solving second order ODEs
AIM:
To write a program in MATLAB/OCTAVE to simulate the motion of a simple pendulum.
EQUATION/FORMULA USED:
`(d^2theta)/dt^2 + b/m (d theta)/dt + g/L sintheta = 0`
`theta = theta1`
`(d theta)/dt = (d theta1)/dt = theta2`
`(d^2theta)/dt = (d^2theta1)/dt^2 = d/dt((d theta1)/dt) = (d theta2)/dt`
`d/dt((d theta1)/dt) + b/m (d theta1)/dt + g/L sintheta = 0`
`(d theta2)/dt + b/m theta2 + g/L sintheta = 0`
g = gravity in m/s2
L = length of the pendulum in m
m = mass of the ball in kg
b=damping coefficient
OBJECTIVE:
- To solve the second-order differential equation and plot the graph.
- To understand the movement of a simple pendulum by creating an animation.
THEORY:
Ordinary Differential Equations(ODE) are a set of equations which can explain the motion of many mechanical systems which include springs and dampers.
A simple pendulum is an ideal pendulum consisting of a point mass suspended by a weightless inextensible perfectly flexible thread and free to vibrate without friction.
BODY:
Code:
- Function:
function [dtheta_dt] = pendulum_damped_ode(theta,t)
%inputs
b = 0.05;
g = 9.81;
l = 1;
m = 1;
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
2. Main code:
clear all
close all
clc
% Intial condition
theta_0 = [0;3];
l = 1;
% time points
t_span = linspace(0,20,500);
% solve ODE
[results] = lsode('pendulum_damped_ode', theta_0, t_span);
%plotting
plot(t_span,results(:,1))
hold on
plot(t_span,results(:,2))
xlabel('time')
ylabel('plot')
hold off
figure(2)
plot(results(:,1),results(:,2))
xlabel('angular displacement')
ylabel('angular velocity')
%simulation of simple pendulum
ct=1;
for i=1:length(results(:,1));
theta3=results(:,1);
THETA3=theta3(i);
x0=0;
y0=0;
x1=l*sin(THETA3);
y1=-l*cos(THETA3);
% horizontal support for string
plot([-0.5 0.5],[0 0],'linewidth',2,'color','k');
hold on
grid on
% string
plot([x0,x1],[y0,y1],'linewidth',1,'color','r');
hold on
% bob tied at the end of string
plot(x1,y1,'-o','markersize',30,'markerfacecolor','k');
axis([-1.5 1.5 -1.5 1]);
file_text = sprintf("armanimation%05d.png",ct)
saveas(gca, file_text)
pause(0.03);
hold off
ct=ct+1;
end
STEP-WISE EXPLANATION OF THE CODE:
- This program is written and executed using octave.
- Starting with all the general command e. clear all, close all & clc.
- Inputting all the code comment at all the required location starting with %.
- Then writing the function code for solving the 2nd Also inputting all the given data.
- Then the initial condition and time points are inputted in the main code.
- The output from the function code is inputted in the main code in order to get the result.
- Then the graph is plotted.
- Now the code is written for the stimulation of simple pendulum.
- Code is written for horizontal support for the string.
- Code is written for the string.
- Code is written for bob tied at the end of the string.
- Note: Since the program is executed in octave therefore the process of stitching the images and saving the output video is little different than that in MATLAB.
ERROR FACED WHILE PROGRAMMING:
- Since the tutorial video was explained in MATLAB. The first error was that I was not knowing the process of stitching the image and saving the output in the form of video is different in case of the octave. Also in the video, I was not aware of this change.
- The second error was made while saving the file I was selecting a wrong file format due to a minor error in my code i.e. %05d screenshot is attached below.
- The third error was in CMD I could not able to change the directory.
STEP TAKEN TO OVERCOME THOSE ERROR:
- The first error was rectified with the help of my instructor over a call.
- The second error was fixed by amending the code.
- The third error was rectified by adding cd before pasting my file location in CDM.
SCREENSHOT OF ERROR:
SECOND ERROR:
THIRD ERROR:
OUTPUT:
STIMULATION OF SIMPLE PENDULUM:
RESULT:
After rectifying all the error, we have got the output plot and the video of the output plot.