All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
OBJECTIVE-To Write A Program That Solve A second Order ODE Using MATLAB. GOVERNING EQUATION- WHERE- g = gravity in m/s^2 l = length of pendulum in meter b = damping coefficient m = mass of ball in kg GIVEN- g = 9.81 m/s^2 l = 1 meter b = 0.05 m = 1 kg THEORY- A pendulum is a body suspended…
Saurabh Dubey
updated on 09 Jan 2021
OBJECTIVE-To Write A Program That Solve A second Order ODE Using MATLAB.
GOVERNING EQUATION-
WHERE-
g = gravity in m/s^2
l = length of pendulum in meter
b = damping coefficient
m = mass of ball in kg
GIVEN-
g = 9.81 m/s^2
l = 1 meter
b = 0.05
m = 1 kg
THEORY-
A pendulum is a body suspended from a fixed support so that it swings freely back and forth under the influence of gravity. When a pendulum is displaced sideways from its resting, equilibrium position, it is subject to a restoring force due to gravity that will accelerate it back toward the equilibrium position. When released, the restoring force acting on the pendulum's mass causes it to oscillate about the equilibrium position, swinging it back and forth.
CODE-1
Function Code-
Function program is called in programe to solve the ODE
%Function program is called in programe to solve the ODE
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/l)*sin(theta1);
dtheta_dt = [dtheta1_dt ;dtheta2_dt ];
end
Explanation-
Function handles are simple function codes that can be created without using the regular function definition using keywords ‘function’ and ‘end’. They do not require any separate ‘.m’ files or not needed to be declared separately below/above the main code. Function handles are directly declared in the main code itself and also accessible from any point in that particular code, i.e they are localized to a particular conditional.
The declaration of a function handle is very similar to that of a function declaration. It can be distinguished by the means of ‘@’ symbol before your value/expression allocation. The syntax for creating a function handle is,
Func_handle=@(input_argument) func_definition
here,
Func_handle is the name of the particular handle
Input argument is the one which will be fed into the function
Func_definition will have the equation/expression which will use the input to give us the output
The syntax for calling this particular function handle is
Variable_name=Func_handle(input_argument)
CODE-2
%Code For Solving Second Order ODE In Matlab
clear all % Removes data from work space
close all % Is used to close all figure
clc % Clears all text from command windows
%Given values to solve ODE problem
b = 0.05;
g = 9.81;
l = 1;
m = 0.1;
%initial condition
theta_0 = [0;3];
%time points 0-20 seconds
t_span = linspace(0,20,500);
%solve ODE
[t,results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
figure(1) %Represents the graph between angular velocity and angular displacement
hold on %hold on command retains plot on current axis so that new plots added to axis do not delete existing plots.
plot(t,results(:,1))
plot(t,results(:,2))
legend('Angular displacement','Angular velocity') %Allows us to add descriptive labels to our plot
xlabel('Time(sec)')
ylabel('Plot')
%For Animation
%For Loop is used
ct = 1; %counter value
for i = 1:length (results(:,1))
%Initial position of pendulum
x_start = 0
y_start = 0
%Final position of pendulum
x_end = l*sin (results(i,1))
y_end = -l*cos (results(i,1))
%Plotting
figure(2) %Represents the figure of simple pendulum
plot ([-1,1], [0,0],'linewidth',10 )
hold on
plot ([x_start x_end],[y_start y_end], 'linewidth',5)
hold on
plot (x_end,y_end,'o','markersize',20,'markerFacecolor','r')
axis([-2 2 -2 1])
pause(0.03)
M(ct)= getframe(gcf) %get frame command is used for play records and play movie once
ct = ct+1;
end
%Creating Movie
movie(M)
videofile = VideoWriter('Pendulum Animation.avi','Uncompressed AVI');%Open video file
open(videofile)
writeVideo(videofile,M) %WriteVideo Command Insert The Frame Used In Array 'M'
close(videofile)
EXPLANATION-
ODE 45-
The function ode45 takes 3 inputs. The 3 inputs are the function to be integrated, the time span and the initial conditions.
The function would be one like shown in the above example. The time span would be the integration limits. This can be provided as a vector of 2 values, the initial and final time or with a spacing using the linspace function. The solver will choose a step size that is most suitable for the solution to obtain the solution in the most optimal time as possible. The initial condition would be the value of the function at t=0s.
The function can be provided in the form of an anonymous function,
[t,results] = ode45(@(x) x^2, tspan,y0)
RESULT-
Start and End Values Of ' x ' & ' y'
x_start =
0
y_start =
0
x_end =
-0.0045
y_end =
-1.0000
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Project 1- Traffic Jam Assistant Feature
AIM - To develop a project on Traffic Jam Assistant System. OBJECTIVE - To develop one specific requirement of Traffic Jam Assistant algorithm which would predominantly feature in the IPC algorithm. The Project must be done keeping in mind the following processes & steps. This model must be developed…
20 Dec 2021 06:32 AM IST
Project 2 Adaptive Cruise Control
AIM - To develop a MATLAB Simulink Model Of Adaptive Cruise Control . OBJECTIVE - 1). To Develop Adaptive Cruise Control feature as per the Requirement Document using MATLAB Simulink. 2). To Follow all the MBD related processes: Requirement Tagging & Traceability, SLDD creation, Configuration…
08 Dec 2021 10:30 AM IST
Project 2 Adaptive Cruise Control
AIM - To develop a MATLAB Simulink Model Of Adaptive Cruise Control . OBJECTIVE - 1). To Develop Adaptive Cruise Control feature as per the Requirement Document using MATLAB Simulink. 2). To Follow all the MBD related processes: Requirement Tagging & Traceability, SLDD creation, Configuration…
08 Dec 2021 10:30 AM IST
Project 1 (Mini Project on Vehicle Direction Detection
AIM - To Develop a Simulink Model on Vehicle Direction Detection . OBJECTIVE - To Develop a MATLAB Simulink model as per requirement. To Tag the requirements to the simulink model. To Create Data Dictionary & generate code of a model . For code generation choose , Storage class for Input…
06 Dec 2021 12:02 PM IST
Related Courses
0 Hours of Content
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.