All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Function for 2nd order ODE :- % function for 2nd order ode function [dtheta_dt] = ode_simple_pendulum(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 Program to solve 2nd order ODE taking simple pendulum…
MANAPRAGADA BHANU MURTHY
updated on 11 Dec 2021
Function for 2nd order ODE :-
% function for 2nd order ode
function [dtheta_dt] = ode_simple_pendulum(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
Program to solve 2nd order ODE taking simple pendulum as example :-
%--------------------SIMPLE PENDULUM-------------------%
% Program to solve a 2nd order ODE (d^2theta/dt^2)+(b/m)*(dtheta/dt)+(g/L)*sin(theta)=0
clear all
close all
clc
b = 0.5; % b is the damping factor
g = 9.81; % g is the acceleration due to gravity
L = 1; % L is the length of the rod
m = 1; % m is the mass attached to the rod
% Initial conditions
S_V = [0;3]; % S is the angular dispalcement, V is the angular velocity
% timepoints
t_span = linspace(0,20,500); % taking time period of 0 to 20 sec for 1000 points
% solve ode
[t, results] = ode45(@(t,theta) ode_simple_pendulum(t, theta, b, g, L, m), t_span,S_V);
% Plot for angular displacement and angular velocity w.r.t time
figure(1)
plot(t,results(:,1))
hold on
grid on
plot(t, results(:,2))
xlabel('time')
ylabel('plot')
title('projection of angular velocity and angular dispalcement w.r.t time for simple pendulum')
legend('displacement', 'velocity')
% Creating an animation to demonstrate simple pendulum using for loop and get frame
theta2 = results(:,1);
ct = 1;
for i = 1:length(theta2)
THETA_1 = theta2(i);
X0 = 0;
Y0 = 0;
X1 = L*sin(THETA_1);
Y1 = -L*cos(THETA_1);
figure(2)
plot([-0.2 0.2], [0 0], 'color', 'r','linewidth',3) % plot for fixed end
hold on
plot([X0 X1], [Y0 Y1], 'color', 'b', 'linewidth',1.5) % plot for rod connecting fixed end and mass
hold on
plot(X1,Y1,'O','markers',10,'markerfacecolor', 'y') % plot for a spherical mass
hold off
grid on
axis([-2 2 -1.2 0.1]) % taking axis dimensions from xaxis -2 to 2 and y axis from -1.2 to 0.1
pause(0.003)
M(ct) = getframe(gcf);
ct = ct+1;
end
% Creating a videofile
movie(M)
videofile = VideoWriter('Simple_pendulum.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Plot :-
Animation link :-
Explanation of Code :-
Step_1 :-
-> To solve a 2nd order ODE, we are creating a function.
-> In this function we are converting 2nd order ODE into 1st order ODE to get two results and assigning them into an array dtheta_dt.
-> IN order to covert we taking theta _1 = theta(1), theta_2 as theta(2).
Step_2 :-
-> Now we are writing the main code to solve the 2nd order ode.
-> In this step we are giving the inputs and initial conditions provided in the question.
-> Also taking the time span of pendulum oscillations up to 20sec for 500 values.
Step_3 :-
-> Writing ode45 syntax
[TOUT,YOUT] = ode45(ODEFUN,TSPAN,Y0)
-> ODEFUN is a function handle. For a scalar T and a vector Y, ODEFUN(T,Y) must return a column vector corresponding to f(t,y).
-> TSPAN is a two-element vector [T0 TFINAL] or a vector with several time points [T0 T1 ... TFINAL].
-> YO is a column vector of initial conditions, one for each equation.
Step_4 :-
-> Plotting the results in a single figure using hold on command. And labeling the plot.
Step_5 :-
-> Creating an animation to demonstrate simple pendulum using for loop and get frame.
-> taking theta_2 = results(:,1), giving ct = 1;
-> Creating a for loop and taking i = length(theta2).
-> Assigning theta2(i) to THETA1.
-> giving the initial values x0 = 0; y0 = 0;
-> and taking the end point x1 = L*sin(THETA1); y1 = -L*cos(THETA1);
Step_6:-
-> Now we are creating a plot to get the frames.
-> firstly, we are plotting the fixed points and giving hold on command.
-> for the second one we are plotting the length of the rod.
-> for the third one we are creating a plot to make a circular anime using marker command which is considered as weight connected to the rod.
-> Now setting the axis using axis command and giving a pause limit of 0.003.
-> Creating the movie frame using getframe command and giving ct = ct+1, ending the for loop.
Step_7 :-
-> After the plot is converted into frames now, we are creating it into a movie(M).
-> Creating a video file by writing it as simple_pendulum.avi which is uncompressed avi.
-> Hence video file is created and we can open it as given in the youtube link.
Error's found :-
Explanation of error :-
-> In the above fig we can see the animation having lot of frames which are fixed while oscillating.
-> this error is found due to misplacing of hold on as marked with red ink as shown in above fig.
-> This this way of presentation will be not good so we need to place the hold on command at write place to overcome those errors.
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 - Parsing NASA thermodynamic data
Aim: - Write a Matlab program that extracts 14 co-efficient and to calculate the entropy, enthalpy, and specific heats for all the species in the given NASA Thermodynamic data file data. Also, to calculate the molecular weight of each species. File Parsing: - Parsing, which is the process of identifying…
03 May 2022 06:52 PM IST
FINAL INDEPENDENT PROJECT
Final Independent Project: - Topic: - Multi-objective optimal design of cycloid speed reducer based on genetic algorithm. Platform used to Solve the problem: - Matlab Introduction: - Cycloid speed reducers have found wide applications in the automation field as industry robots, machine tools, automatic machinery and other…
10 Apr 2022 10:32 AM IST
Project 2 - Rankine cycle Simulator
Aim: To create a Rankine Cycle simulator using Matlab. Objective: To calculate the state points of the Rankine cycle. To plot the T-S, H-S Diagrams of Rankine cycle. Theory: The Rankine cycle is an idealized thermodynamic cycle describing the process by which certain heat engines, such as steam turbines…
03 Apr 2022 07:27 PM IST
Week 4.1 - Genetic Algorithm
Genetic Algorithm: - A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation. …
23 Jan 2022 07:40 AM 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.