Challenge

Week 4.1 - Solving second order ODEs

Aim :- A program to second order Ordinary Differential Equation

.

Equations :- The equation used to determine the second order Ordinary Differential Equation

.`d^2θ/dt^2 + (b/m)* dθ/dt + (g/l)* sin θ = 0`

where,

g = gravity in m/s2,

L = length of the pendulum in m,

m = mass of the ball in kg,

b=damping coefficient,

Sin & Cos= Trigonometry terms.

Objective of these project :-

The objective of this project is to find the various theta along with the different angular velocity and to measure the acceleration due to gravity by using simple pendulum.

Body of the content :-

Writing a program in Matlab that will simulate the pendulum motion, just like the one shown in the start of this challenge.

use,

L=1 metre,

m=1 kg,

b=0.05.

g=9.81 m/s2.

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

After solving this program I will get the position of the pendulum as a function of time.

clear all
close all 
clc

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

% Initial condition for displacement and velocity
theta_0 = [0;3];

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

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

plot(t,result(:,1))
hold on
plot(t,result(:,2))
xlabel('plot')
ylabel('time')

for i=1:length(result(:,1));
    x0=0;
    y0=0;
    
    x1= l*sin(result(i,1));
    y1= -l*cos(result(i,1));
   
    plot([-1.5 1.5] ,[0 0]);
    hold on
   plot([x0 x1],[y0 y1] ,'linewidth',3,'Color','r');
   axis([-1.5 1.5 -1.5 1.5]);
   plot(x1,y1,'o','Markersize',20','color','b');
   pause(0.0001);
    M(ct) = getframe(gcf);
    hold off
    ct=ct+1
end
movie(M);
Videofile = VideoWriter('pendulum.avi','Uncompressed AVI');
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)

>Function  
function [dtheta_dt] = ode_fun (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

Here we can say that A program of second order differential equation is ready to go with a proper execution, well in these we have used the commands like clc ,clear all ,Inputs , linspace , plot , x-label ,y-label.

Commands use for: -

  • Clear all = used to clear the workspace.
  • CLC = used to clear the command window.
  • Close all = used to close the background program.
  • % Inputs = used this command for better clarification to enter or add new terms.
  • Linspace = used to make sure the interval is constant.
  • Plot = used for plotting a graph, terms used in bracket as shown above , always the same terms means exact words or letters used in Inputs so that we won’t get errors will running program.
  • x-label & y-label = used for name recognition in x-axis & y-axis
  • sind & cosd = It’s not a command but it is useful for understanding because if we need values in degree then we had to use these term “d” after any trigonometry function or else we get the answer in the form of radians.
  • Function = used for recall
  • Movie = used to Play recorded movie frames.
  • Getframe = used to capture the figure or axes as movie frames.
  • gcf = used to handle current figures.
  • Uncompressed AVI = used to create a HD video clip and it’s enlarges the size of that particular file.
  • Videowriter = used to create a video data in an AVI file in a JPEG compression.
  • Open = used to open a file in appropriate place in file.
  • Writevideo = used to writes data from an array to the video file associated with v.
  • Closed = used to close a file in appropriate place in file.
  • Hold on = Adding new plots or frame.
  • Hold off = Hold the new plots or frame.
  • For loop = To repeat the specified number of times.
  • Axis = We used this command to inbuilt the plot or frame inside it , we can say like a frame of this dimensions.
  • Errors found while running this program.
  • Faced error in parameter ‘THETHA’ insert of using the capital alphabets I have used small alphabets, so we had to use the exact alphabets used in program. Got so much errors due to this mistake I have done in this program.
  • Got error while using the plot command i.e., syntax & bracket error.
  • Plots / Video clip
  • A video clip of a plot is uploaded in you-tube and the link is shared.

https://youtu.be/72ueJxx7wYA

Conclusion:-

In this we can conclude that the program is the movement of the simple pendulum of a second order differential equation in the various theta along with the different angular velocity.

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.