Week 3 - Solving second order ODEs
REPORT:
To create the motion of an oscillation pendulum, one has to solve the 2nd degree differential equation the oscillation with the given conditions of time, length, mass and damping coefficient to solve for the displacement and velocity.
Matlab has an inbuilt solver known as ode45 with which we can solve diffential equations of required order with ease. Hence, at the begining of my programme, i specified the required constraints as inputs and follwing it, i used the ode45 solver and stored the values of time span in a matrix "t", and the displacement and velocity solutiuons in another matrix, "results".
Following that, i stored the values of displacement and velocity in individual arrays to easily access them and then i plotted a graph visualizing the variation of displacement and velocity with time.
After solving the ODE and obtaining the solution vector for displacement and velocity, all i had to do was, fix one end point of the rod and place the other end according to the variation of displacement that had been obseved via solving the ODE.
To plot the instantenous position of the rod, i set the cordinates of the bob to:
x = l(sin(theta))
y = l(cos(theta))
where, theta represents the collection of data that represents the variation of angular displacement at the intervals of t=(20/500) seconds.
After pausing the code for t=(20/500)seconds, i successfully managed to get the animation i desired.
Finally, i used the getframe function to collect all the ventured positions of the system throughtout the motion and then i used the movie function to create a movie out of all those stored frames.
ERRORS I FACED:
Warning: Ignoring extra legend entries.
> In legend>process_inputs (line 578)
In legend>make_legend (line 322)
In legend (line 273)
In ODE (line 34)
Dot indexing is not supported for variables of this type.
HOW I COUNTERED THE ERROR:
The error occured because i had used the legend function for velocity curve before plotting the velocity curve. After positioning the legend function in corrent order i managed to tackle the error.
SCREENSHOT OF ERROR:
MAIN CODE:
% program to simulate pendulum motion
close all
clear all
clc
% inputs
b = 0.05;
g = 9.81;
m = 1;
l = 1;
% initial conditions
theta_0 = [0;3];
% timespan
t_span = linspace(0,20,500);
% solving the ODE using ode45 solver
[t, results] = ode45(@(t,theta) rhs(t,theta,b,g,l,m),t_span,theta_0);
% creating the data variable for storing velocity and displacement points
angular_displacement = results(:,1);
angular_velocity = results(:,2);
% plotting
figure(1)
plot(t,angular_displacement)
grid on
ylabel("variation")
xlabel("time")
hold on
plot(t,angular_velocity)
legend('displacement','velocity')
title("displacement and velocity variation")
hold off
% animating pendulum oscillation
% centre of the oscillation
x0 = 0;
y0 = 0;
ct = 1;
% creating for loop to access the 500 values of displacement
for i = 1:500
current_x = l*sin(angular_displacement(i));
current_y = -(l*cos(angular_displacement(i)));
% forming the instantenous position of rod and bob
figure(2)
plot([x0 current_x],[y0 current_y],'linewidth',3)
hold on
% creating the bob sircle with marker command
plot(current_x,current_y,'or','MarkerSize',10);
hold off
grid on
axis([-2, 2,-2, 2])
title('pendulum animation')
% pausing the process for the unit interval of time
pause(20/500);
% storing the instantenous positions as frames in a variable M
M(ct) = getframe(gcf);
ct = ct+1;
end
% creating a movie out of the frames
movie(M)
videofile = VideoWriter('oscillation animation.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
FUNCTION CODE:
function second_vector = rhs(t,theta,b,g,l,m)
second_vector = [theta(2);-(g/l)*sin(theta(1))-(b/m)*theta(2)];
YOUTUBE LINK:
https://www.youtube.com/watch?v=LvD_CanPcuI&feature=youtu.be
SCREENSHOT OF OUTPUT:
YOUTUBE LINK:
https://www.youtube.com/watch?v=LvD_CanPcuI&feature=youtu.be