Challenge

Week 3 - Solving second order ODEs

Link for the Execution of the code:- https://youtu.be/U6yFSV8m0cU

Code:-

clear all
close all
clc

%Given Data
l = 1;       %length in meter
m = 1;       %mass in kg
b = 0.05;    %damping cofficient
g = 9.81;    %gravitation force in meter per sec. sq.

%initial condition
T_0 = [0;3];    %[displacement; velocity];

%time points
t_span = linspace(0,20,500);  %intial time, final time, no. of space


%defining the ODE
[t,T] = ode45(@(t,T) [T(2); -(b/m)*T(2)-(g/l)*sin(T(1))],t_span,T_0);

disp = T(:,1)


%ploting the animation for pendulem
ct=1;
for i=1:length(t)

%Defining the intial position of the Pendulum.
x1 = 0;    
y1 = 0;

%Calculating the Position of the Pendulum at the given period of time.
x2 =  l * sin(disp(i));
y2 = -l * cos(disp(i));

%Ploting the arm movement.
plot([x1 x2],[y1 y2],'linewidth',2)
hold on;

plot(x2,y2,'-o','markersize',15)
axis([-1 1 -1.5 0.5])
grid on
pause(0.01)
hold off;
M(ct)= getframe(gcf);
ct=ct+1;
end

%animating the arm movement as a video file
movie(M)
videofile= VideoWriter('Pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Explanation of Code:-

-The given values have been defined(i.e. values of length, mass, damping coefficient, gravitational force).

-The values of displacement and velocity have been defined as a [2*1] matrix for the time t=0.

-Using the linspace command 500-time points have been created between 0 to 20sec.

-Using the function 'ode45' the time, displacement, and velocity at the given time have been calculated.

-We Defined the value of the first point(x1,y1) of the pendulum as the origin(0,0) of the drawn graph and then calculated the value of the suspended end(x2,y2) for the given time.

-Using the plot command we plotted the string and using the marker command we plotted the mass of the pendulum.

-Using the for loop command we repeated the given process to plot the position of the pendulum at different points in time.

-By using the movie command the animation for the given motion of the pendulum has been created.

Errors:-

I didn't face any major errors during the time of my writing the code other than some typo errors.

Screenshot of the Code:-

Result:-

Hence the Code for the motion of the pendulum has been written and exicuted.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.