Week 3 - Solving second order ODEs
Report on Pendulum challenge
AIM:
To write a code on a given ODE function and to make an animation of a pendulum.
GOVERNING EQUATIONS:
Some of the major equation and formulae used in this program coding are
d2Θ/dt2 + (b/m) dΘ/dt + (g/L) sinΘ = 0
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.
OBJECTIVES:
- To create a code and animation of the given function.
- The output should show an oscillating pendulum.
GIVEN DATA:
L=1 m,
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.
REPORT:
This program was done using MATLAB language.
Supplementary code:
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
Main code:
% Inputs for the program
b = 0.05;
g = 9.81;
l = 1;
m = 1;
% Initial value
theta_0 = [0;3];
% Time span
t_span = linspace(0,20,100);
% Calling the function for result
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
% Plotting
figure(1)
title('result plotting')
subplot(2,1,1)
plot(t,results(:,1),'color','r')
ylabel('plot')
subplot(2,1,2)
plot(t,results(:,2),'color','b')
ylabel('plot')
xlabel('time')
figure(2)
plot(results(:,1),results(:,2),'color','g')
% Giving counter for the value
ct = 1;
% Creating a loop of values using for function
for i = 1:length(results(:,1))
% Starting points/ origin points are as follows
x0=0;
y0=0;
% End point rod/string is given as
x1=l*sin(results(i,1));
y1=-l*cos(results(i,1));
% Plotting for animation
figure(3)
% Line plotting
plot([-1 1],[0 0],'linewidth',6,'color','r');
% String plotting
plot([x0 x1],[y0 y1],'linewidth',2,'color','b');
hold on
% Marker plotting
plot(x1,y1,'Marker','o','Markersize',10,'Markerfacecolor','g','Markeredgecolor','g');
% Axis is changed to desired value
axis([-1.5 1.5 -1.5 1.5])
% Pause is given for getting a clear animation between intervals
pause(0.03)
% movie is specified here according to ct
M(ct)= getframe(gcf);
hold off
% The value of ct should be changed with the loop
ct=ct+1;
end
% For making of animation we need to specify code, file name, open command,
% write command, close command.
movie(M)
v_f=VideoWriter('Pendulum_Challenge.avi','Uncompressed AVI')
open(v_f)
writeVideo(v_f,M)
close(v_f)
EXPLANATION:
Supplementry code:
- Function command is done by using the syntax " [dtheta_dt] = ode_func(t,theta,b,g,l,m)"
- Second order function is changed to two first order funcions.
- [dtheta_dt] is function of both dtheta1_dt and dtheta2_dt.
Main code:
- Inputs are given for required variables.
- The initial value of theta is given.
- The time span required is given.
- The supplementary function is called and plotting for the results is done.
- figure(1) is result plotting.
- figure(2) is the plotting of found results.
- for loop is created for values at different times.
- plotting for animation is done.
ERRORS OCCURRED:
- While plotting for animation the pendulum was facing up to overcome this error the y1 value was changed to negative.
- Giving the marker to the pendulum was difficult.
- At first, animation was not having a smooth transaction but when "sind and cosd" where changed to "sin and cos" the error did not happen.
CONCLUSION:
Thus the coding for ODE function and the making of pendulum animation was done.
ANIMATION LINK
https://drive.google.com/file/d/1Fzai4MchbuupomBQ2VQNxLlbNKmfv2tu/view?usp=sharing