Solving second order ODEs using MATLAB
Objective-
- Solving an ODE(Ordinary Differential Equation) of equation of motion of a simple pendulum with damping using MATLAB.
- Create an Animation Video of the simple pendulum.
Program-
close all
clear all
clc
b=0.05; %damping Coefficient
g=9.81; %Acceleration due to Gravity
l=1; %length of the string
m=1; %Mass of the object
%initial Condition (Angular Velocity)
th0=[0;3];
%time for which pendulum is moving
ts=linspace(0,20,2000);
[t, res]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),ts,th0); %ode45 is the inbuilt ODE solver in MATLAB and calling personal ode function
%Plotting the time and result
figure(1);
plot(t,res(:,1));
hold on
plot(t,res(:,2));
hold off
%Plotting the result
figure(2);
plot(res(:,1),res(:,2));
a=1;
j=1;
for i=1:length(res(:,1))
x=0;
y=0;
x1=l.*sin(res(i,l));
y1=-l.*cos(res(i,l));
%Plotting the Pendulum Motion
figure(3);
plot([-l l],[0 0]);
hold on
line([x x1],[y y1]);
hold on
plot(x1,y1,'-o');
axis([-1.5 1.5 -1.5 1.5]);
pause(0.003);
hold off
%Saving the figures as gcf with counter a
Text(i)=sprintf("pend%d.jpg",a);
saveas(gcf,Text(i));
k(j)=getframe(gcf); %storing frames in k with counter j
a=a+1;
j=j+1;
end
movie(k) %creating movie out of the frames stored in k
v=VideoWriter('pendulum.avi','uncompressed avi');
open(v);
writeVideo(v,k);
close(v);
ODE solving function
function[dth_dt]=ode_func(t,theta,b,g,l,m)
th1=theta(1);
th2=theta(2);
dth1_dt=th2;
dth2_dt= -(b/m)*th2-(g/l)*sin(th1);
dth_dt=[dth1_dt; dth2_dt];
end
Explanation-
-In the program we introduced the damping coefficient, acceleration due to gravity, length of the string, Mass of the pendulum.
-After that we introduced initial conditions like angular velocity and the time which is 0-3rad/sec and 0-20 sec respectively.
-Calling an inbuilt Ode solver ode45 which is used for the solving the non stiff ode like pendulum movement. In which we mentioned the two variables (t and theta) it means this function is dependent of this two variables.
[t, res]=ode45(@(t,theta) ode_func(t,theta,b,g,l,m),ts,th0)
In this ode solver we have to mention the varibles on which equation dependant , after that we mentioned a ode solver that we created and finally we nmentioned time span and angular velocity for which the pendulum is going to move in the variables ts and th0.
During the ode45 solver ts and th0 gets stored in the variables t and theta.
In function ode_func, we divided the the second order equation in two first order equations both values are stored separated and given as two separate columns in matrix.
-After that we plotted the result columns separately with respect to time and similarly in next plot we plotted the both column results with respect to each other
-Now to our main objective to generate video for the pendulum motion we created the for loop from i =1 to the length of the results
-after that we calculated the string motion with intial co-ordinates with (0,0).
-Now for plotting
plot([-l l],[0 0]); we first plotted the horizontal rigid surface which is equivalent to string length in both positive and negative direction
line([x x1],[y y1]); (line command creates the line out of two different points)
after that we plotted the string with fixed starting points(0,0) and variable end points as each iteration gives different end points.
plot(x1,y1,'-o');
after that we plotted the pendulum mass with the points (x1,y1) which are basically end points of the string
-All the graphs and figures that are generated are created in the double format thats why the getframe (gcf )commands does not work here that's why we took different approach.
This time we created the two counters 'a' and 'j' , and then we created each graph generated as .jpg file and after that we again saved this file as gcf which is of struct format and each of this gcf file in variable k with counter as j where j counter is incremented by +1 each time the loop executes.
-After that we created movie outof the variable k in which we stored all the frames of the graphs generated.
-By command VideoWriter we created file pendulum.avi and by WriteVideo command we generated video file .
Result And Conclusion
1.We solved the second order differential equation of motion of pendulum with MATLAB programme
2.We successfully generated the video of pendulum.
Link For The Video