Project

Simulation-of-pendulum-by-solving-second-order-ODE-using-MATLAB

Objective:

To write a program that solves following ODE. Equation 1 represents the motion of a simple pendulum with damping, and to animate it to have a swinging pendulum.

\"\"

Eq.1 Second order differential equation to simulate a pendulum

Procedures:

The first step is to convert equation 1 into two first order differential equation. The following equations are obtained:

\"\"

Eq.2 First order differential equations

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.

Simulating the motion between 0-20 sec, for angular displacement=0, angular velocity=3 rad/sec at time t=0.

Given:

L=1 meter,

m=1 kg,

b=0.05.

g=9.81 m/s2.

The following code defines and set up the first order differential equation.

function [dtheta_dt]= ode_func(t, theta, b,g,l,m)

theta1= theta(1);

theta2= theta(2);

dtheta1_dt= theta(2);
dtheta2_dt=(-b/m)* theta2 -(g/l)*sin(theta1);


dtheta_dt= [ dtheta1_dt; dtheta2_dt];

end

The main code to simmulate the pendulum motion.

close all 
clear all
clc



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


%% initial condition

 theta_0= [0;3];
 
 
 %% time points
 
 t_span= linspace (0,20,500);
 
 %% x solve ode
 
 [t , result] = ode45(@(t, theta) ode_func(t, theta, b,g,l,m), t_span, theta_0);
 
 %result(i,1)
 %hold on
 %plot( t, result(:,2))
 %xlabel(\' time \')
 %ylabel(\'plot\')
 
  ct=1;
%% calculation of the location
for i = 1:length(result)
    
    
    
    x_0= 0;
    y_0= 0;

    x_1= l*sin(result(i));
    y_1= -l*cos(result(i));

  
    %% plot
    
    plot([x_0 x_1],[y_0 y_1], \'linewidth\',4\')
   axis ([ -2 2 -2 2]);
    hold on
plot([-1 1],[0 0], \'linewidth\',4\')
hold on
    plot ( x_1 ,y_1  ,\' -o\' ,\'MarkerSize\',10, \'MarkerFaceColor\',\'r\')

hold off
    pause(0.2)

   
  
     M(ct)= getframe(gcf);
   ct=ct+1;
   
   
end


%%  create a movie from the frames
movie(M)
videofile= VideoWriter(\'Pendulum.avi\',\'uncompressed AVI\')
open(videofile)  
writeVideo(videofile,M)  
close(videofile)

Code Explanation

The command ode45 integrates the system of differential equations theta′=f(t,theta) from t initial to t final with initial conditions theta (displacement and velocity). Each row in the solution array y corresponds to a value returned in column vector t.

The following lines creates a movie of all the frames (‘writeVideo’). It gives a file name and the profile.

-videofile= VideoWriter(\'pedulum.avi\',\'uncompressed AVI\')

-open(videofile)

-writeVideo(videofile,M)

-close(videofile)

The following line defines the displacement and velocity of the pendulum.

-theta_0= [0;3];

The Following line defines the time the pendulum swings.

-t_span= linspace (0,20,500);

I had few errors like: misspelling of writevidoe, multiples ct=ct+1, write “Movie” instead of ‘movie.

The for-loop in the main code is just to be able to plot the results from the Ode command and be able to create a movie of the movement of the pendulum.

Errors faced while programming

I had the following errors in matlab,

\"\"

\"\"

The simulation obtained as follows: https://youtu.be/RUSWB55z_Qs

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.