Challenge

Week 3 - Solving second order ODEs

Aim:- To solve the ODE equation of a pendulam.

Objective:- Write a program that solves the ODE which represents the equation of motion of a simple pendulum with damping, also create the animation of it.

Theory:-

A simple pendulum consists of a ball (point-mass) m hanging from a (massless) string of length L and fixed at a pivot point P. When displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion. MOvement of the pendulam depends on Newton's second law, a second order ordinary differential eqation is formed which represent the position of the pendulam w.r.t time.

Equation of motion

Where,

g=gravity in m/s^2

L=length of the pendulam in meter

m=mass of the ball in kg

b=damping cofficient

To solve the second order differential equation we convert it to first order ODE

θ =θ1

dθ1/dθ =θ2

d^2θ /dt^2=dθ 2/dt

Inputs:-

L=1m

m=1Kg

b=0.05

g=9.81 m/s^2

Time span=0.20 sec


Matlab Programe:-

clear all
close all
clc

%Inputs
b=0.05;
g=9.81;
l=1;
m=1;

%Initial condition;
theta_0=[0;3]

%time points
t_span=linspace(0,20,500);

%Solve ODE
[t,results]= ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);

%ploting
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time in sec')
ylabel('plot')

%For Animation
ct=1;
theta_1=results(:,1);

for i=1:length(results(:,1))

%initial co-ordinates
x_start=0;
y_start=0;
theta_2= theta_1(i);

%Final Co-ordinates
x_end=1*sin(theta_2);
y_end=-1*cos(theta_2);

%ploting for animation
figure(2)
%base of pendulam
plot([-1,1],[0,0], 'linewidth',4,'color','b')
hold on
%string of pendulam
line([x_start x_end],[y_start y_end],'linewidth',6,'color','b')
hold on
%bob of pendulam
plot(x_end, y_end,'marker','o','markersize',28,'markerfacecolor','r')
axis([-2 2 -2 2])
pause(0.03)
hold off
M(ct)=getframe(gcf);
ct=ct+1
end

%Create movie

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

ODE fuction code:-

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

Code explaination:-

1. In the initial step input are define.

2. To solve the differential equation the matlab function(ode45) used, and other input parametrs also defined in the fuction.

3. ODE result plotted w.r.t. time.

4. Use for loop to create the animation.

5. Plot command used to plot pendulam string & bob.

6. Now command written to creat the animation of aoscillating pendulam.

Output:-

Angular displacement and angular velocity plot were generated for time range of 20 sec

Errors:-

1)

Sol:-Correct the wrong input.

2)

Sol:- Correct the command

3)

Sol:- Correct the command

Animation video link

https://drive.google.com/file/d/1baWxODNlufPc4f7dwykAjwMpSq-31_JI/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.