Week 3 - Solving second order ODEs
% 2nd order ode
clear all
close all
clc
%inputs
%damping coefficeint
b=0.05;
%gravity in m/s2
g=9.81;
%length of pendulum in m
l=1;
%mass of the ball
m=1;
%initial condition
theta_0=[0;3];
% time points
t_span=linspace(0,50,500);
%solve ode
[t,results]=ode45(@(t,theta)ode_func(t,theta,b,g,l,m),t_span,theta_0);
%plotting
plot(t,results(:,1));
hold on
plot(t,results(:,2));
xlabel('time');
ylabel('plot');
grid on;
%simulation of pendulum
ct=1;
for i=1:length(results(:,1))
x0=0;
y0=0;
x1=1*sin(results(i,1));
y1=-1*cos(results(i,1));
plot([-0.5,0.5],[0,0],'linewidth',3,'color','r');
line([x0 x1],[y0 y1],'linewidth',2,'color','b');
hold on;
grid on;
axis([ -1.5 1.5 -1.5 0.5]);
pause(0.01);
hold off;
M(ct)=getframe(gcf)
ct=ct+1;
end
%creating movie
movie(M)
Videofile=VideoWriter('pendulum_animation.AVI','uncompressed AVI')
open(Videofile)
writeVideo(Videofile,M)
close(Videofile)
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
Report writing for 2order of a simple pendulum
We are going to use second order differential equation to solve simple pendulum
d^2/dt^2+b/m*dtheta/dt+g/lsin(theta)=0
By this equation, we have to split it into two ode
Code for calling function:
Where in function[dtheta_dt] is given with required input values
For gravity=9.81
B=0.05
L=1
M=1
T=0
By splitting the equation for two ode then given inputs are run and values are provided
Codes for simple pendulum:
By giving clear all ,close all,clc
And inputs for g,b,l,m
Initial condition is [0;3]
Time point is [linspace 0,50,500]
Now to solve ode
T and results are given by calling function
Then plotting the values
Plotfot (t results 1 and t results 2)
To make the pendulum
Counter is given as=1
For
i=1:length(results(:,1))
x0=0;
y0=0;
x1=1*sin(results(i,1));
y1=-1*cos(results(i,1));
As given
And for axis plot axis([ -1.5 1.5 -1.5 0.5]
plot([-0.5,0.5],[0,0] and for lin line([x0 x1],[y0 y1],
Frame is given to get
Creating a movie for the pendulum animation
Movie[m]
Creating a file for the movie
To open the file and to play the file
And close the file
youtube link:


