Challenge

Solving second order ODEs

Aim :- To Write a program in Matlab that will simulate the pendulum motion

Objective :-

  • First objective is to write a program that solves the 2 order ODE
  • To Write a program in Matlab that will simulate the pendulum motion
  • To plot the graph of displacement and velocity v/s time and animate the motion of bob.

Governing Equation :-

`(d^ 2 θ)/ (d t ^2) + b /m ⋅ (d θ)/ (d t) + g/ l ⋅ sin θ = 0`

Where;-

  • b = damping coefficient
  • m = mass of the bob (kg)
  • g = acceleration due to gravity (m/s^2)
  • l = length of the string in (m)

Theory :-

A simple pendulum consists of a mass (m) hanging from a string of length (L) and fixed at a pivot point (P). When displaced to an initial angle and released, the pendulum will swing back and forth with periodic motion.

Code Explanation :-

  • Programing Language used Matlab Online
  • starting the program by using comments
  • Providing the input variables like ( L=1 metre, m=1 kg, b=0.05 , g=9.81 m/s2. )
  • Providing the values for theta-0 & using linspace command for time span=0 to 10 for 250 values
  • Solving the ode by use of the predefined function and for the ode formula
  • Now plot the graph for the displacement & velocity w.r.to time .
  • Now Using ‘for’ commandfor iterating the values of the desired variable.
  • Assuming the values for x0 ,y0 = 0.
  • Providing the eqn for the x1,y1 as x1=l*sin(theta0); y1=-l*cos(theta0);
  • Plotting all the coordinate values and providing the axis no.
  • Using the pause command to make the plot look realistic
  • Incrementing the ct value by +1 each time it executes for loop.
  • All figures are assigned to a variable M, one at a time usingct
  • Ending the ‘for’ loop .
  • using movie command is to combine all the figures saved in the M file to make an animation/movie
  • At end saving the movie/animation in .avi format.

Matlab code :-

%program for solving 2order-ODE and obtain an animation for simle pendulum 
clear all
close all
clc

%input parameters
l = 1;
m = 1;           
b = 0.05;         
g = 9.81;         

%intial condition
theta_0 = [0;3] ; 

%time plots
t_span = linspace(0,10,250);

%solving the ode
[t,results] = ode45(@(t,theta) ode_function(t,theta,l,m,b,g),t_span,theta_0);

%plotting  
figure(1)
plot(t,results(:,1))
hold on;
plot(t,results(:,2))
xlabel('Time');
ylabel('Plot');
legend('Angular displacement','Angular velocity');


% using for loop
ct = 1;
figure(2)

for  i = 1:length(results(:,1))
     theta0 = results(i,1);

x0=0;
y0=0;

x1=l*sin(theta0);
y1=-l*cos(theta0);

 %plotting 
 plot([-1,1],[0,0],'linewidth',10,'color','black');
 line([x0,x1],[y0,y1],'linewidth',3, 'color', 'r');
 hold on
 plot(x1,y1,'O','markers',20,'markerfacecolor','b');
 axis([-2 2 -2 2]);
 pause(0.01);
 M(ct)=getframe(gcf);
 hold off
 ct = ct + 1;
 
end

%making of the movie/ Animation
movie(M)
videofile=VideoWriter('simple_pendulum.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

Function code :-

function [dtheta_dt] = ode_function(t,theta,l,m,b,g) 
    
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

Command window exicution & Errors :-

- use of semicolon results in no output in command window .

- no errors were detected during the exicution .

Result / Conclusion :-

Thus we have created a simulation of simple pendulum using matlab code and solved the second order ODE for it , & also generated the graph of displacement & velocity w.r.to time .

-link below shows the simulation of a simple pendulum .

- graph/plot of displacement & velocity vs time

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.