Challenge

Week 3 - Solving second order ODEs

2nd order ODE(ordinary differential equation):-

A second order ordinary differential equation is an ardinary differential equation in which any deravitives with respect to the independent variable have order number grater than 2.

Second order liear differential equations are used to model many systems in engineering.

Simple pendulum:-

Let us consider a simple pendulul consists of a mass 'm' hanging from a string of length 'l' and fixed at a pivot point. when displaced on a initial angle 'theta' and released, the pendulum will swing back and forth with periodic motion.

then, the equation is given by,

Let. theta=theta1

dtheta_dt=dtheta1_dt=theta2--------(1)eq

d^2theta_dt^2=d^2theta1_dt^2=d_dt(dtheta1_dt)=d_dt(theta2)-------(2)eq

  • A function is created to call the ordinary differential equation of the simple pendulum. The code for function is given below:-
    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 for the movie:-

movie(M)
videofile=VideoWriter('pendulum.avi','Uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)

By using this function code and movie syntax the matlab code for simple pendulum is given below:-

clear all
close all
clc

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


% initial condition
theta_0=[0;5];

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

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

figure(1)
plot(t,results(:,1));
hold on
plot(t,results(:,2));

xlabel('time')
ylabel('velocity')

legend('displacement','angular velocity')
grid on
hold off

displacement=results(:,1);
velocity=results(:,2);


% count command for iterations
ct=1;
x0=0;
y0=0;

for i=1:length(displacement)
   

   
    
 % fixed coordinates of the bob

 x1=l*sin(displacement(i));
 y1=-l*cos(displacement(i));

% plotting the bob motion
figure(2)
plot([-1 1],[0 0],'LineWidth',5,'Color','g');
hold on;
plot([x0 x1],[y0 y1],'linewidth',3);
plot(x1,y1,'o','markersize',15,'MarkerFaceColor','b');
hold off;
axis([-2 2 -2 2])
grid on
M(ct)=getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('pendulum.avi','Uncompressed avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Youtube video link:-

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.