Challenge

Week 3 - Solving second order ODEs

Simple Pendulum

Aim:

To simulate the motion of a simple pendulum with damping.

Governning equation:

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.

The above equation represents, Damped Simple Harmonic Motion.

A simple pendulum is a good example of damped Simple Harmonic Motion (SHM).

Here, the amplitude reduces, because of damping caused by air friction.

Objective of project:

  • To simulate motion of simple pendulum with damping in MATLAB.
  • To solve second order ODE.

Workflow:

  • Reduce the second order ode to first order by substitution by a variable in place of first-derivative term.
  • Input the geometrical parameters and initial conditions.
  • create seperate function to reduce differential equation's order to 1.
  • solve ode using in-built ode45 function.
  • plot amplitude vs time graph.
  • simulate the motion of pendulum and save it as a movie file.

Code:

function program:

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

main program:

clear all
close all
clc

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


theta_0=[0;3];
t_span=linspace(0,20,500);

[t,results]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
plot(t,results(:,1),'color','b');
hold on;
grid on;
plot(t,results(:,2),'color','r');
axis([0 20 -3 3]);
ylabel('plot');
xlabel('time');
grid on;

ct=1;
for i=1:length(results(:,1))
    figure(2);
    x0=0;
    y0=0;
    x1=l*sin(results(i,1));
    y1=-l*cos(results(i,1));
    plot([-1 1],[0 0],'linewidth',4,'color','k');
    hold on;
    line([x0 x1],[y0 y1],'linewidth',2,'color','b');
    hold on;
    plot(x1,y1,'-o','markers',20,'markerfacecolor','r');
    grid on;
    axis([-1.5 1.5 -1.5 0.5]);
    pause(0.003);
    hold off;
    M(ct)=getframe(gcf);
    ct=ct+1;
end

movie(M);
pendulum_anim=VideoWriter('Pendulum.avi','Uncompressed AVI');
open(pendulum_anim);
writeVideo(pendulum_anim,M);
close(pendulum_anim);
    

Explanation of code:

  1. Create a function to reduce given ode to first order ode.
  2. Specify the geometric properties like length, acceleration due to gravity, damping co-efficient,etc.
  3. solve ode using in-built ode45 function.
  4. plot amplitude vs time graph.
  5. simulate the motion of pendulum and save it as a movie file.

Errors:

Had saved program file name and movie file name as the same.

  • Changed the movie file name to different name as that of program to overcome the error.

Had misspelled the in-built function name as 'writevideo'.

  • Had changed it to correct name 'writeVideo'

Output:

Video file:

https://youtu.be/kl5IujhX68k

Results:

  • Successfully simuated simple pendulum's motion with damping in MATLAB.
  • Successfully solved Second Order ODE.
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.