Challenge

Week 3 - Solving second order ODEs

AIM:- Write a program in Matlab\Octave that will simulate the pendulum motion by using ODE function .

DESCRIPTION:-

In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum

The way the pendulum moves depends on the Newtons second law. When this law is written down, we get a second order Ordinary Differential Equation that describes the position of the "ball" w.r.t time.

simple pendulum has a small-diameter bob and a string that has a very small mass but is strong enough not to stretch appreciably. The linear displacement from equilibrium is s, the length of the arc. Also shown are the forces on the bob, which result in a net force of −mg sinθ toward the equilibrium position—that is, a restoring force.

GOVERNING EQUATIONS:-

(dθdt)2+bm⋅dθdt+gL⋅sin(θ)=0(dθdt)2+bm⋅dθdt+gL⋅sin(θ)=0

Parameters of the system will be defined by

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient.

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2

CODING:-

before going to start main coding we are going to define a fuction

%function for solving the ode
 
function [dtheta_dt] = ode_func_pendullum(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 code:-

main program 

 
 
close all %(to clear the command which is use in past)
clear all  %(to close all the plot)
clc %(to clear previous data)
 
 
 
%inputs
b=0.05;
g=9.81;
l=1;
m=1;
 
% initial conditions given in question
theta_0 = [0;5];
 
%time points
t_span = linspace(0,20,500);
 
%solve ode
[t,results] = ode45(@(t,theta) ode_func_pendullum(t, theta, b,g,l,m),t_span, theta_0);
 
plot(t,results (:,1))
hold on
plot(t,results(:,2))
 
ylabel('plot')
xlabel('time')
 
fig=1;
 
for i = 1:length(t)
    
    %initial position of the pendulum
    
    x0 = 0;
    y0 = 0;
    
    %final position
    
    x1 = l*sin(results(i,1));
    y1 = -1*cos(results(i,1));
    
  %plotting  
    figure(2)
    %base line
    plot([-1,1],[0,0],'linewidth',2,'color','g');
    % pendulum line
    line([x0 x1],[y0 y1],'linewidth',3,'color','b');
    hold on;
    %pendulum ball
    plot(x1,y1,'o','markers',30,'markerfacecolor','r');
    grid on
    
  axis([-2 2 -2 1]);
  pause(0.01);
  hold off;
  M(fig) = getframe(gcf);
  fig= fig+1;
   
end
%%creating the animation and movie(avi)
 
movie(M);
videofile = VideoWriter('second_ode_pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

result:- In this challenge we are successfully simulate simple pendulum by using Second order ODE by the help of MATLAB function and through which we create an simulation video .

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.