Challenge

Week 3 - Solving second order ODEs

Objective: 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.

Input Values

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2

Step 1

Convert 2nd Order ODE order in to 1st degree ODE

theta=theta1

dtheta/dt=dtheta1/dt=theta2

dtheta1/dt+b*e/m+g*sin(theta1)/L

Step 2

Create a Function of ODE

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

Step 3: Create simple pendulum program by using ODE function

(a): Initial conditions are provided:- pi/2 is a displacement and 0 is the angular velocity

(b): Time interval 0 to 50 seconds in 200 steps

(c): Solving ODE equation by using ode45 function

clear all

close all

clc

%Input

b=0.05;

g=9.81;

l=1;

m=0.1;

%Initial condition

theta_0= [0;5];

%Time

t_span=linspace(0,50,200);

%Solve ODE

[t result]=ode45(@(t, theta)ode_func(t,theta,b,g,1,m),t_span,theta_0);

%Plotting

figure(1)

hold on

plot(t,result(:,1))

plot(t,result(:,2))

xlabel('TIME')

ylabel('PLOT')

hold off

save as(1,'Time_plot.png')

% Animation

x0=0

y0=0

x1=-1

y1=0

x2=2

y2=0

ct=1

for i=1:length(t)

x=1*sin(result(i))

y=-1*cos(result(i))

plot([x1 x0 x2],[y1 y0 y2],'linewidth',8)

hold on

plot([x0 x],[y0 y],'linewidth',2)

hold on

plot(x,y,'o','markersize',15,'markerfacecolor','b')

axis([-3 3 -3 0])

hold off

pause(0.1)

d(ct)=getframe(gcf);

ct=ct+1;

end

movie(d)

videofile=Videowriter('Simple_pendlum.avi',"Uncompressed AVI");

open(videofile)

writeVideo(videofile,d)

close(videofile)

Plots are shown below:

Animation video link is shown: https://youtu.be/Txq-oVnlZsU

Explanation of the Code:--

(a): At first convert second degree ODE order in to first degree ODE

(b): ODE function is created in another tab

(c): Creating simple pendulum program using ODE function

(d): we begin with clear all, close all, clc command

(e): then the inputs are written according to the given data from the question given.

(f): time span is also given

(g): write the code using ode45

(h): plot the first figure (1) which is shown below

(i): code for animation of the simple pendulum

(j): code the movie and save it

(k): play the video by running the program

Conclusion: The above plots show how displacement and angular velocity changes with respect to time.

Errors or problems faced: Some errors happened while coding for animating the simple pendulum but it is resorted at the end with some help from the technical team

Report: Technical team helped by re explaining the animation coding part for the simple pendulum. The character car had only the issue. After rewritting the code the error was solved.

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.