Challenge

Week 3 - Solving second order ODEs

Program to solve ODE for the equation of motion of a simple pendulum with damping:

clear all
close all
clc

%SOLVING 2ND ODE

b= 0.05;%damping coefficient
g= 9.81;%gravity in m/s2
l= 1;%length of the pendulum in m
m= 1;%mass of the ball in kg

%intial condition
theta_0= [0;3];
% time points
t_span= linspace(0,20,200);
% solving ODE
[t,results]= ode45(@(t,z1)ode_func(t,z1,b,g,l,m),t_span,theta_0);

plot(t,results(:,1))
hold on
plot(t,results(:,2))    
xlabel('time')
ylabel('plot')
legend('angular displacement','angular velocity');


    

%%ANIMATION FOR A SIMPLE PENDULAM
ct=1
for i=1:length(t_span)
    x0=0;
    y0=0;

    x1=l*sin(results(i,1));
    y1=-l*cos(results(i,1));
    figure(2)


    plot([x0 x1],[y0 y1],'linewidth',3);
axis([-2 2 -2 2]);
hold on;
plot([-1 1],[0 0],'linewidth',5,'color','b');
plot(x0,y0,'o','markersize',10,'markerfacecolor','y');
plot(x1,y1,'o','markersize',30,'markerfacecolor','r');
title('MOTION OF A PENDULUM');
xlabel('x-axis');
ylabel('y-axis');

M(ct)=getframe(gcf)
ct=ct+1
hold off;


end
movie(M);
Videofile=VideoWriter('simple_pendulum_challenge');
open(Videofile);
writeVideo(Videofile,M)
close(Videofile);

PROGRAM TO SHOW THE FUNCTION "ode_func":

function [dtheta_dt]= ode_func(t,z1,b,g,l,m)
theta1= z1(1)
theta2= z1(2)
dtheta1_dt= theta2;
dtheta2_dt= -(b/m)*theta2-(g/l)*sin(theta1);
dtheta_dt= [dtheta1_dt;dtheta2_dt];

end

Graphical representation as shown below:

THE SIMULATION LINK IS GIVEN BELOW:

https://drive.google.com/file/d/14yl8MYTvD71cdezxLB3xJ9CvYZkoAPeu/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.