Project

Matlab Program to solve Second order Ordinary Differential Equation(ODE) and to simulate the pendulum motion

Aim

To write a Program in Matlab that can solve a second order Ordinary Differential Equation(ODE) and to simulate the pendulum motion.

Objective

1.The main objective is to write a code to solve a second order Ordinary Differential Equation(ODE)

2.And to simulate the pendulum motion.

Equation to be solved:

Simple pendulum equation is,

where,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b = damping coefficient.

For solving this ODE function in MATLAB, we have to convert the above second order ODE into two first order ODE which will then be specified to two column vectors.

These two columns vectors would be used in the function 'ode45' that is predefined in MATLAB.

It is provided that,

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

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

Thus, the second order ODE is conveted into first oder which can now be used in the function.

Software used:

MATLAB R2020(Online)

CODE:

%Solving Second Order ODE pendulam equation using provided parameters

clear all
close all
clc

%Inputs as provdied
b = 0.05;
g = 9.81; 
l = 1;
m = 1;

%Initial condition
theta_0 = [0;5];

% timespan of 0-20 seconds
t_span = linspace(0,20,500);

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

%plotting the values
subplot(2,1,1);
plot(t,results(:,1),'color','r')
hold on
plot(t,results(:,2),'color','b')
hold off
subplot(2,1,2)
plot(results(:,1),results(:,2))
xlabel('position_radians')
ylabel('displacement & velocity ')


ct=1;                           %using counter variable
for i=1: length(results(:,1))   %for loop
    x0 = 0;
    y0 = 0;
    x1 = l*sin(results(i,1));
    y1 = -l*cos(results(i,1));
    figure(2)
    plot([-1 1],[0 0], 'linewidth',5,'color','r');
    axis([-2 2 -2 2]);
    line([x0 x1],[y0 y1],'linewidth',3,'color','g');
    hold on;
    plot(x1,y1,'o','markersize',25,'markerfacecolor','b','markeredgecolor','b');
    hold off;
    M(ct) = getframe(gcf);
    ct=ct+1;    %incrementing counter variable
end             % end of for loop


movie(M)
videofile = VideoWriter('pendulam_ode.avi','Uncompressed AVI');
open(videofile);        %opening the file
writeVideo(videofile,M) %writing the file
close(videofile)        %closing the file

Code for Function:

%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/1)*sin(theta1);
 dtheta_dt = [dtheta1_dt; dtheta2_dt];
end     %end of function

Code Explanation:

1.First assign the input values such as Damping coefficient(b), Gravity(g), Length of pendulum(l) and Mass of the ball(m) as provided in the challenge.

2.The oscillation of the pendulum is depend on mass of the ball, damping coefficient, Gravity. Then give displacement range from 0 to 3,which is on y-axis .

3.Now give the value of time span as 0 to 20 sec and is given by linspace command ‘linspace(0,20,500)’ and between those range taking 500 values for plotting the graph.

4.Now to solve the ODE we call the function as “ode_func” since the values are passed via function thus calculations are done by the formula

6.And the plotting is done between time and result that is got via function by passing the command of plot .

7.To understand the graph the labels are given to it by passing command xlabel & ylabel.

8.Now for different values of time and angle the different position of the pendulum is found by using the for loop. By initializing counter variable ‘ct’ value as 1.

9.Next is to create a movie, So videowriter command is used for creating the movie. Here (ct) represent the individual frame.

10.Now the “getframe” command to get the current figure. Movie(M) indicate the movie file and the video format is given as .avi that is “pendulum_ode.avi”

Output:

Youtube Link for Animation:

https://youtu.be/k1IVdrjGi68

Errors faced:

1)

Mentioned the function(ode_func) name incorrectly as "ode_function"

2)

Forgot to enclose linewdith with '...'

Conclusions:

Finally, the ODE is solved using MATLAB and the animation for a simple pendulum is generated.Similarly using programs like these the higher order equations can also be solved.

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.