Challenge

Week 3 - Solving second order ODEs

Second order ODE function

Introduction :

A simple pendulam consists of a mass 'm' handling from a string of length 'l' and fixed at a pivot. when initial angle 'θ' and relased.

The equation is given by,

Explanation of the code :

Inputs are taken as given in the problem

L = 1 meter,

m = 1kg,

b = 0.05,

g = 9.81 m/s^2

Initial conditions :

Angular displacement = 0, angular velocity = 3 rad/sec, at time t = 0

the motion is simulated by = 0-20 sec.

uploaded link :

video can be uploaded by youtube is given below,

https://youtu.be/-o4RX5X-PV8

Main program :

clear all
close all
clc

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

% initial condition
theta_0 = [0;3];

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

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

plot(t,result(:,1))
hold on
plot(t,result(:,2))

Animated motion of the pendulam :

clear all
close all
clc

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

% initial condition
theta_0 = [0 3];

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

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

ct=1;
for i = 1:length(result(:,1))
    x0 = 0;
    y0 = 0;
    x1 = l*sin(result(i,1));
    y1 = -l*cos(result(i,1));
    subplot(5,4,[1,2,3,4,5,6,7,8,9,10,11,12]);
    plot([-1 1],[0 0],'linewidth',4,'color','k');
    hold on
    line([x0 x1],[y0 y1],'linewidth',2,'color','b');
    hold on;

 % plotting circle and then mapping the center to the string
 % plot(x1+xc,y1+yc,'linewidth',2)
 % hold off;


 plot(x1,y1,'o','Markersize',20,'MarkerFaceColor','r');
 grid on;
 axis([-1.5 1.5 -1.5 0.5])
 pause(0.0003);
 hold off
 M(ct) = getframe(gcf);
 ct=ct+1;
end

movie(M)
videofile = VideoWriter('second_ODE.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile) 

ODE 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];

Output :

Error output :

Animated video :

https://youtu.be/-o4RX5X-PV8

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.