All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Object: Your objective is to write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping Code: clear all close all clc b=0.05; g=9.81; l=1; %length is m m=1; %mass is kg %Initial condition theta_0=[0;3]; %time point t_span=linspace(0,20,150); %Slove…
KURUVA GUDISE KRISHNA MURHTY
updated on 19 Apr 2022
Object:
Your objective is to write a program that solves the following ODE. This ODE represents the equation of motion of a simple pendulum with damping
Code:
clear all
close all
clc
b=0.05;
g=9.81;
l=1; %length is m
m=1; %mass is kg
%Initial condition
theta_0=[0;3];
%time point
t_span=linspace(0,20,150);
%Slove ODE
[t,result]=ode45(@(t,theta)ode_function(t,theta,b,g,l,m),t_span,theta_0);
plot(t,result(:,1))
hold on
plot(t,result(:,2))
xlabel('time')
ylabel('angular displacement')
%Animation
X0=0;
Y0=0;
ct=1;
for i=1:length(t)
X1=l*sin(result(i,1));
Y1=-l*cos(result(i,1));
figure(2)
plot([-1 1],[0 0],'LineWidth',6,'Color','g');
axis([-2 2 -2 2]);
line([X0 X1],[Y0 Y1],'linewidth',4,'color','b');
hold on;
plot(X0,Y0,'O','MarkerSize',3,'MarkerFaceColor','y');
plot(X1,Y1,'O','MarkerSize',20,'MarkerFaceColor','r');
title('Damping of simple pendulum');
grid on;
hold off;
M(ct)=getframe(gcf);
ct=ct+1;
end
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,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
output:
figure:1
figure:2
youtube link: https://youtu.be/-WqDj_m9Iog
Report:
Solving the second order ODE to find the oscillation of simple pendulum.
Using terms:
b-damping ratio
m-mass
g-gravity
l-length
The length of bob is 1m, mass of bob is 1kg, gravity is 9.81 and damping ratio is 0.05
Theta_0 as contain the displacement and velocity is [ 0; 3]
Timespan is taken linspace 0 to 20 it as divided in 150 times.
%Solve ODE
Ode45 is the inbuilt function of solved in Matlab and ODE45 is usually the function of choice among the ODE solvers. it compares methods of orders four and five to estimate error and determine the step size. ODE45 is so accurate that its default behavior is to use its interpolant to provide result at intermediate point.
It as represents in [t, result] equal to ode45 off main function, ode function definition, time array and initial conditions
[t, result] = ode45(@ (t, theta)ode_function(t, theta, b, g, l, m), t_span, theta_0)
Function
Function is nothing but a piece of code that we can reuse by just calling its name and passing in right parameters.
This is the Function code
function [dtheta_dt] = ode_function(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
Plotting:
Plot the points in time and results of first column hold on and plot the points of second column.
Xlabel is time and ylabel is angular displacement.
%ANIMATION
Starting point of pendulum is X0, Y0.
Use ct=1
Counter actually represents the each and every frame
For loop
For statements loop a specific number of times and keep track of each iteration with an incrementing index variable.
Using for loop I should be taken as 1 to length of t
X1=length*sin(result(I,1)) and y1=-l*cos(result(I,1))
Plot the horizontal line x-axis from –1 to 1, length of support is 6 in color of green and the axis range of x-axis is –2 to 2 and y-axis is –2 to 2.
Line as represent the length of connecting rod starting point [X0 X1], [Y0 Y1] and the length is 4 in color of blue
Plot the point of X0, Y0 In represent of circle, markersize of 3 and markerfacecolor in yellow
Plot the point of X1, Y1 In represent of circle, markersize of 20 and markerfacecolor in red
The title is Damping of simple pendulum and grid on
Creating the plot animation to use M(CT)= getframe(gcf), ct=ct+1 and the commands used is
movie(M)
videofile=VideoWriter('simple_pendulum.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
the file should be saved as Simple_pendulum.avi is as saved in video format.
when i run the program i didn't face any errors in command window
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 : CFD Meshing for Tesla Cyber Truck
CFD Meshing for Tesla Cyber Truck Initial Model First of all…
09 Nov 2022 05:46 PM IST
Week 10 - Simulating Combustion of Natural Gas.
COMBUSTION Combustion is defined as a chemical reaction in which a hydrocarbon reacts with an oxidant to form products, accompanied by the release of energy in the form of heat. Combustion manifests as awode domain during the design, analysis, and performance characteristics stage by being an integral part of various…
08 Nov 2022 07:38 PM IST
Week 9 - Parametric study on Gate valve.
Theory: Introduction: Gate valves are designed for fully open or fully closed service. They are installed in pipelines as isolating valves and should not be used as control or regulating valves. Operation of a gate valve is performed doing an either clockwise to close (CTC) or clockwise…
07 Nov 2022 05:07 PM IST
Week 12 - Validation studies of Symmetry BC vs Wedge BC in OpenFOAM vs Analytical H.P equation
Angles to test: θ=100 θ=250 θ=450 Expected Results Validate Hydro-dynamic length with the numerical result Validate the fully developed flow velocity profile with its analytical profile Validate maximum velocity and pressured drop for fully developed flow Post-process Shear stress and validate…
06 Nov 2022 06:53 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.