Challenge

Week 3 - Solving second order ODEs

To write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping

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.

Pendulum codes are

clear all
close all
clc

% b-damping,g-acceleration due to gravity,l-length,m-mass of object
b=0.05;
g=9.81;
l=1;
m=1;

% for simplification
 r=g/l;
 k=b/(l*m);

 %pendulum ode

 f=@(t,x) [x(2);-k*x(2)-r*sin(x(1))];

 % initial conditions
 init = [pi/2; 0] 
 
 % solve ODE. The time interval is set from 0 to 20 seconds
 [t,x] = ode45(f,[0 20],init);

 %animation

%origin

O= [0 0];
axis(gca,'equal');% aspect ratio of the plot
axis([-2  2 -2 2]);%the limits of the plot
grid on

ct=1;
for i=1:length(t)
    P=l*[sin(x(i,1)) -cos(x(i,1))];
 %circle in origin
 O_circle = viscircles(O,0.01);
 %pendulum
 pend = line([O(1) P(1)],[O(2) P(2)]);
 %ball 
 ball=viscircles(P,0.05);
 % time interval
 pause(0.01);
 M(ct)=getframe(gcf);
        ct=ct+1;
 % delete previous objects if it is not the final loop
 if i<length(t)
        delete(pend);
        delete(ball);
        delete(O_circle);
 end
 
end
movie(M)
videofile=VideoWriter('pendulum.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

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

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.