All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
Video for simulation of motion of a simple pendulum is attached below: Solving 2nd order ODE\'s in matlab: an example of second order differential equation is a simple pendulum with damping. In order to solve a 2nd order ODE in mat lab we need to first split it into two 1st order ODE. in this process the above 2nd…
Tilak S
updated on 14 May 2019
Video for simulation of motion of a simple pendulum is attached below:
Solving 2nd order ODE\'s in matlab:
an example of second order differential equation is a simple pendulum with damping.
In order to solve a 2nd order ODE in mat lab we need to first split it into two 1st order ODE.
in this process the above 2nd order ODE is simplified into following two equations:
let θ=x(1)
x(1)\'=dθ/dt=x(2)
x(2)=dθ/dt (1st equation)
x(2)\'=d^2θ/dt^2=-(b/m)*x(2)-(g/l)*sin(x(1))(2nd equation)
L.H.S is put into an array to make it for the matlab to understand:
therefore x=[x(2),x(2)\']=[x(2);-(b/m)*x(2)-(g/l)*sin(x(1))]
using the above function as a anonymous function in matlab the damped pendulum motion is simulated in matlab.
the parameters used for simulation are:
L=1 metre,
m=1 kg,
b=0.05.
g=9.81 m/s2.
the motion is simulated btetween a time period t=o to t=20s.
the initial conditions provided are x0=(0 3).
the function x is solved in matlab as a anonymous function using ode45 code:
f=@(t,x)[x(2);-(b/m)*x(2)-(g/l)*sin(x(1))];% function
[t,x]=ode45(f,t,x0);%ode45 function
the matlab automatically takes the time interval based on the problem.
ode 45 function gives us two outputs:
one is a time intervals array, t (1x1matrix)
other is a solution array,x(2X2 matrix)
here the first column represents the displacement and second column indicates angular acceleration.
plotting the results:
here two plots are generated for both dispalcement and acceleration using subplot function:
subplot function helps us to display two plots in a single figure .
subplot function is used as follows:
%plotting
figure(1)
subplot(2,1,1)
plot(t,x(:,1),\'color\',\'r\',\'linewidth\',3)
xlabel(\'time\')
ylabel(\'anguar velocity\')
%hold on
subplot(2,1,2)
plot(t,x(:,1),\'color\',\'b\')
hold on
xlabel(\'time\')
ylabel(\'amplitude\')
plot(t,x(:,2),\'color\',\'r\')
legend(\'angle(rad)\',\'angular speed(rad/s)\');
%hold on
Finally the animation is done by drawing a pendulum for all values of t :
code for animation:
ct = 1;
for i=1:length(t)
%origin points
X0=0;
Y0=0;
%mass points
X1=1*sin(x(i,1));
Y1=-1*cos(x(i,1));
figure(2)
plot([X0,X1],[Y0,Y1]);
hold on
plot(X1,Y1,\'.\',\'markersize\',35);
hold on
grid on
axis([-1.5 1.5 -1.5 0.5])
pause(0.003)
hold off
frame(ct) = getframe(gcf);
ct = ct+1;
end
%Creating animation
movie(frame)
videofile= VideoWriter(\'simple_1.avi\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile, frame)
close(videofile)
Full program is attached below:
%solvng an second order ode
clear all
close all
clc
%parameters
g=9.81;
l=1;
m=1;
b=0.05;
%second order ode function(anonymous)
f=@(t,x)[x(2);-(b/m)*x(2)-(g/l)*sind(x(1))];
%conditions
t=linspace(0,20,40);
x0=[pi/2 0];
%solve second order ode
[t,x]=ode45(f,t,x0);
%plotting
figure(1)
subplot(2,1,1)
plot(t,x(:,1),\'color\',\'r\',\'linewidth\',3)
xlabel(\'time\')
ylabel(\'anguar velocity\')
%hold on
subplot(2,1,2)
plot(t,x(:,1),\'color\',\'b\')
hold on
xlabel(\'time\')
ylabel(\'amplitude\')
plot(t,x(:,2),\'color\',\'r\')
legend(\'angle(rad)\',\'angular speed(rad/s)\');
%hold on
ct = 1;
for i=1:length(t)
%origin points
X0=0;
Y0=0;
%mass points
X1=1*sin(x(i,1));
Y1=-1*cos(x(i,1));
figure(2)
plot([X0,X1],[Y0,Y1]);
hold on
plot(X1,Y1,\'.\',\'markersize\',35);
hold on
grid on
axis([-1.5 1.5 -1.5 0.5])
pause(0.003)
hold off
frame(ct) = getframe(gcf);
ct = ct+1;
end
%Creating animation
movie(frame)
videofile= VideoWriter(\'simple_1.avi\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile, frame)
close(videofile)
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...
Week 6
Objective: The goal is to clean the surface geometry to avoid errors and carry on the solid meshing (3-D) process and provide connections using connection managers that accurately captures the input domain geometry(side door) with high-quality cells so as to make subsequent calculations intractable.…
23 Jul 2020 03:36 PM IST
Week 5
Objective: The goal is to clean the surface geometry to avoid errors and carry on the solid meshing (3-D) process and create a mesh that accurately captures the input domain geometry(rear view mirror) with high-quality cells so as to make subsequent calculations intractable. The…
13 Jul 2020 02:14 PM IST
Mid-surface mesh generation of plastic bottle cap using ANSA
Objective: The goal is to clean the surface geometry to avoid errors and carry on the hemming process and create a mesh that accurately captures the input domain geometry(A plastic bottle cap) with high-quality cells so as to make subsequent calculations intractable. The above-said…
23 Jun 2020 09:43 AM IST
Analysis of air standard cycle with python
objective: To code a python program to obtain the presssure-volume curve of a four-stroke si engine. (otto - cycle) Otto cycle: An Otto cycle is an idealized thermodynamic cycle that describes the functioning of a typical spark ignition piston engine. It is the thermodynamic cycle most commonly found in automobile engines.…
07 May 2020 09:28 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.