Challenge

Week 3 - Solving second order ODEs

1. OBJECTIVE - SOLVING 2ND ORDER ODE OF SIMPLE PENDULUM WITH DAMPING.

code :

clear all
close all
clc

%inputs
%Damping coefficient
b=0.05;

%gravity in m/s^2
g=9.81;

%length of the pendulum in m;
l=1;

%mass of the ball in kg
m=1;

%initial condition of angular displacement & angular velocity 
theta=[0;3];

%time points ;
time=linspace(0,20,180);

%solve ODE
[t, results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),time,theta);

%plotting
subplot(2,1,1)
plot(t, results(:,1));
xlabel('time')
ylabel('Angular displacement')
grid on
subplot(2,1,2)
hold on;
plot(t,results(:,2));
xlabel('time')
ylabel('Angular velocity')
grid on

%ANIMATION OF SIMPLE PENDULUM

%inputs
X0 = 0;
Y0 = 0;

ct=1;

%for loop
for i=1:length(t)
    
    %coordinates
    X1=l*sin(results(i,1));
    Y1=-l*cos(results(i,1));

    %plotting
    figure(2)
    plot([-1 1],[0 0],'linewidth',5,'Color','g')
    axis([-2 2 -2 2]);
    line([X0 X1],[Y0 Y1],'linewidth',2,'color','b');
    hold on;
    plot(X0,Y0,'O','markersize',5,'markerfacecolor','y')
    plot(X1,Y1,'O','markersize',20,'markerfacecolor','r')
    title('DAMPING OF SIMPLE PENDULUM');
    legend('Support rod','length of pendulum','Fixed point','Bod of pendulum')
    grid on
    hold off
    M(ct)=getframe(gcf);
    ct=ct+1; 

end

movie(M)
videofile = VideoWriter('simple_pendulum.avi','Uncompressed AVI');
open(videofile);
writeVideo(videofile,M);
close(videofile);

RESULTS:https://drive.google.com/file/d/1waWvDl7_INLljMYEk_wUGGj6Yr9soQ7X/view?usp=sharing

EXPLANATION :

code :

1.clear all - it is used to remove all the variables from the current workspace,clearing from them from system memory.

2.close all - deletes all the figures whose handles are not hidden.

3.clc - it is used to clear the command window.

4.inputs- it includes the variables such as damping coefficient, length of the pendulum, gravity & mass of the ball.

5.initial condition of angular displacement & angular velocity.

6.Time array is created from 0sec to 20 sec with 180 values in between using linspace command.

7. calling of ODE45 function with the input parameters t,theta,b,g,l,m followed by time array & intial condition corresponding to starting of the time.

8.plot- plotting is used to plot the graph between time vs angular displacement and time vs angular velocity using the subplot.

9.xlabel is used to to name the graph in x-axis & ylabel to name the graph in the y-axis.

10.grid on- it is used to on the grid on the graph.

Animation of simple pendulum :

1.coordinates x0 and y0 are assinged to 0 which is fixed.

2.ct is set to 1.

3.for loop is used in which i is iterated from 1 to the length of t in which coordinates x1 and y1 are assigned.

4.plot-plot is used to plot the coordinates with linewidth of 5 & color 'green'.

5.axis is provided with the given coordinates.

6. line is plotted using the coordinates with the linewidth of 2.

7. A shape 'o' is created in the fixed coordinates having a size of '5' by using 'markersize' command and having a 'yellow' color by using 'markerfacecolor' command.

8.A shape 'o' is created at the end of the pendulum having a size of '20' by using 'markersize' command and having a 'red' color by using 'markerfacecolor' command.

9. legend is used to differentiate between support rod, lenght of the pendulum, fixed point & bod of pendulum.

10. getframe commant is used to saved the figures in the array 'M'.

11.movie(M) is used to create a videofile with the help of VideoWriter commant in AVI format.

12. openvideo command is used to open the file, closevideo command is used to close the file.

result :

it is the animation of simple pendulum with damping by solving 2nd order ODE.

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.