Week 3 - Solving second order ODEs
clear all
close all
clc
% providing the default values provided in the question for execution of the ODE
l=1;
m=1;
b=0.05;
g=9.81;
% l is lenght of pendulum in Meter
% m is mass of ball in Kg,
% g is acceleration due to gravityin m/s
% b is the damping coefficient
% initial conditions for ODE execution
theta_0= [0;3];
% the value 0 in first coloumn in the initial diplacement
% the value 3 in second column is the initial velocity 3m/s
% providing a time interval of 20 seconds with 500 time points
t_span=linspace(0, 20, 500);
% solving ODE
[t, results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
% result stores the displacement and velocity of pendulum in the respective coloumns at time t
% ode45 is a default solver present in Matlab and Octaven
% it takes 3 inputs as the function to be integrated given as ode_func, as first one
% timespan as t_span, as second one and
% initial condition as theta_0 , as third one.
theta_pend=results(:,1);
Ang_vel= results(:,2);
% theta_pend stores the values of dipacements
%Ang_vel stores values of angular velocity
% loop for animating pendudlum
ct=1
for i=1:length(theta_pend)
theta1=theta_pend(i);
x0=0;
y0=0;
x1=sin(theta1);
y1= -cos(theta1);
% -cos is used because if +cos is ued it would create inverse image of pendudlum motion
plot(1.5*[x0 x1], 1.5*[y0,y1],'linewidth',1,'color','r',1.5*x1,1.5*y1,"o", "markersize", 10);
% a facror of 1.5 is multiplied to make to pendulum motion bigger for easy presentation.
grid on ;
axis([-2 2 -2 2]);
pause(0.000003)
P(ct)= getframe(gcf);
ct=ct+1;
end
% creating movie file
movie(P)
videofile = VideoWriter('Pendulum_motion.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,P)
close(videofile)
Function Used for ODE Calculation
% Function used for solving the ode using ode45
function [dtheta_dt] = ode_func(t,theta,b,g,l,m)
thetal = theta(1);
theta2 = theta(2);
dtheta1_dt = theta2;
dtheta2_dt = -(b/m)*theta2 - (g/l)*sin(thetal);
dtheta_dt = [dtheta1_dt; dtheta2_dt];
% dtheta1_dt is the first single oder differential equation and dtheta2_dt is the second single oder differential equation
% dtheta_dt returns the vaules to the t and result coloumns in the main program
end
AIM :
Developing a program to simulate pendulum motion by solving a second degree ODE
STEPS :
- Providing the default values provided in the question for execution of the ODE like length, mass, damping coefficient etc.
- Provide the initial conditions like velocity and displacement of the pendulum for calculation of the equation.
- Developing a Function that would provide as an input to the ofde45 default solver present in MATLAB and Octave
- Calling the function inside the main program as an input to the ode45 function to obtain values of time, displacement and velocity from the function.
- Using a For loop for plotting the position of pendulum at each point of time.
- Obtaining an animation of the pendulum movement with respect to time
- Extracting the animation as a movie file.
RESULT: Animation of Pendulation motion has been captured.
Video Links: https://youtu.be/EJH_0cB48ns
https://drive.google.com/file/d/1oJ52u5Xjm2fX4awBL575hVRGkpqgdmg3/view?usp=sharing
Errors:
- Mistakes in output visualization due to logical errors in codes.
- Unable to Create Movie File in octave.
Error Resolutions:
- Logical Errors were rectified using trail and error method and the final solution was derived.
- Created a Screen-recording using 3rd party software as recommended my technical support
I have attached the Screenshots for your reference.
Resources:
1.https://skill-lync.com/knowledgebase/understanding-ode45-2
2.https://skill-lync.com/knowledgebase/understanding-anonymous-functions-in-matlab-2
3.https://in.mathworks.com/help/symbolic/simulate-physics-pendulum-swing.html
4.https://octave.org/doc/v4.2.1/Two_002dDimensional-Plots.html
# I have used Octave for running the code thus was unable to obtain a movie file output. So I have submitted the screen recording of the animation as recommended by technical support team. I have also trimmed the screen recording as it forms to be a too big file to upload.