Week 3 - Solving second order ODEs
#Report:-
1) Step wise explanation of my code:
In this challenge the motive was to create a programe which would solve the given ODE and create an animation and plot of the same.
So, we start with our usual clear all, close all and clc commands
Then, we insert the given inputs namely (damping coefficient, gravity, length and mass)
Initial conditions are added which include (angle and velocity) angle is 0 whereas velocity is 3m/s
t_span & linspace command's are used to include time of 0 to 20 secs with 300 values in between.
ode45 is an inbuilt solver of matlab which is used in this case for solving differential equations.
Seperate script is created for function commands.
Two coloums are created for theta1 and theta2
After that initial position of the pendulum is added which is x0 and y0, followed by trignometric functions which is used to find out the transient and maximum motion of the pendulum.
'O' command is used to create a marker and 'markersize' command is used to increase the size and make it look like a pendulum.
#Errors Faced:-
Variable 'm' was used for mass as well as movie command which had led to an error.
Plot and animation windows were overlapping, which were later fixed by naming the plots individually (fig 1 and fig 2)
#Errors Overcome:-
Errors were overcomed with the help of one on one session with the sir.
#youtube_video_link_for_animation
clear all
close all
clc
%damping coefficient
b = 0.05;
%gravity
g = 9.81;
%length
l = 1;
%mass
m = 1;
% Initial Conditions (disp = 0 & vel = 3m/s)
theta_0 = [0;3];
% Time points ( 0 to 20 with 300 values in between)
t_span = linspace(0,20,300);
% Solve ODE
[t,results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
ylabel('plot')
xlabel('time')
hold off
%contour command
ct = 1;
figure(2)
for i = 1:length(results(:,1))
%points of the origin
x0 = 0;
y0 = 0;
x1 = l*sin(results(i,1));
y1 = -l*cos(results(i,1));
%plot
plot([-1.5 1.5],[0 0])
hold on
plot([x0 x1],[y0 y1],'LineWidth',1, 'color', 'b')
axis([-2 2 -2 2])
plot(x1,y1,'o','MarkerSize',20,'color','r')
pause(0.001)
hold off
M(ct) = getframe(gcf)
ct = ct+1
end
%Animation
movie(M)
videofile=VideoWriter('simple_pendulum.avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
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