Challenge

Week 3 - Solving second order ODEs

AIM

To write a program to solve second order differential equation(ODE) for simple pendulum using ODE45 solver in Matlab

OBJECTIVES

1) To define intial conditions for initial displacement and velocity

2) To specify the time limits for calculus derrivations

3) To create a seperate program for defining derrivative equations calling out in ODE syntax

4) Calling a loop for tracing out a frames of every instance of a pendulum for a given limit of time

EQUATION & SOLVING

Where,

g= Gravity in m/s²

l=length of Pendulum in m

m=mass of the ball in kg

b=damping coefficient

INPUTS

Damping coefficients,b=0.005

Gravity,g=9.81m/s²

Mass of the Pendulum = 1kg

Length of Pendulum = 1m

SIMULATIONAL PARAMETERS

Time limits = 0 to 20 sec

Angular Displacement = 0

Angular Velocity = 3 rad/sec at 0th second

EQUATION BREAKDOWN AND SUPPORT FUNCTION

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

SYNTAX OF THE PROGRAM

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);

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

a=results(:,1)
ct=1

for c=1:length(a)
    k=a(c)
%coordinates
x0=0   
y0=0   
%formula for locating pendulum ball position polar coordinate system
x=l*sin(k)
y=l*cos(k)

%As the pendulum swings downwards, converting from positive to negative
%values for graphical representation
x1=-x
y1=-y

%plotting
figure(1)
plot([x0 x1],[y0 y1],'linewidth',3)
hold on %to hold the pendulum line
plot(x1,y1,'marker','.','markersize',25) %to represent Pendulum Ball
axis([-2 2 -2 2]) 
pause(0.03) 

M(ct)=getframe(gcf); %to get current frame in M(ct)
ct=ct+1; %incrementing to avoid overwritting the frame in same variable
 
hold off %to ensure the position of the Ball is not carry forwarded to next frame
end 

movie(M) %for creating film using frames stored in M(ct)
videofile = VideoWriter('Pendulum.avi','uncompressed AVI') %defining the format to be stored
open(videofile) %opening the video file
writeVideo(videofile,M) %writting the frames
close(videofile) 

OUTPUT OF THE PROGRAM

Video Link - https://youtu.be/g20yGfehG4U

While Running

CONCLUSION

Here we had found how to breakdown and solve Differential equation.

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.