Week 3 - Solving second order ODEs
AIM:To create a program of simple pendulum with second order differential equation by using the matlab through given conditions.
EXPLANATION OF SECOND ORDER DIFFERENTIAL EQUATION:
The second order differential equation is
....equation1.
In above equation,
g=gravity in m/s2,
L=length of the pendulum in m,
m=mass of the pendulum,
b=damping co-efficient,
let us assume that,
θ=θ1
therfore
dθ/dt=(dθ1/dt)=θ2
...equation2
d²θ/dt²=(d²θ1/dt²)=d/dt(dθ/dt)=d/dt(θ2)
...equation3
substituting equation2 in equation1 we get
d²θ1/dt²+b/m(dθ/dt)+g/l(sinθ1)=0
substitute the assumed condition d/dt=θ2
dθ2/dt+b/m(θ2)+g/l(sinθ1)=0
therefore,
dθ2/dt=-b/m(θ2)-g/l(sinθ1)
EXPLANATION OF CODES:I can use the second order differential equation.
1.function command is used.function is a file named.m,that accept an input vector and calculates the average of the values,and return a single result.
2.ode45 is used which is enabled value.
3. code @ is used which is repeat the functions.
4.plot command is used to get the velocity and dispalcement along with time is plotted.
5.To create a movie by using a command of vedio file,vedio writer and collection of frames to get a vedio.
FUNCTION PROGRAM CODE:the below function program is used in the program.
function dtheta_dt = ode_func2(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
MAIN PROGRAM CODE:
%for creating a pendulum using second order differential equation
clear all
close all
clc
%condition given
%the damping co-efficient
b=0.05;
%gravity in m/s^2
g=9.81;
%length of pendulum in m
l=1;
%mass of pendulum
m=1;
%motion between them is 0-20sec
%for angular displacement=0 and angular velocity=3 at time=0
theta_0 = [0;3];
%initial tikme taken t
t_span=linspace(0,20,400);
[t,results]=ode45(@(t,theta)ode_func2(t,theta,b,g,l,m),t_span,theta_0);
figure(1)
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time taken in s')
ylabel('angular displacement and angular velocity')
legend('angular displacement and angular velocity','time')
%plotting of pendulum
figure(2)
%assume origin
x0=0;
y0=0;
ct=1
for v=1:length(t)
x1=l*sin(results(v,1));
y1=-l*cos(results(v,1));
plot([x0 x1], [y0 y1],'linewidth',4)
hold on
plot([-0.2 0.2], [0 0],'LineWidth',4)
plot (x1,y1,'o','MarkerSize',15,'MarkerFaceColor','w')
axis([-1 1 -1 1])
hold off
grid on
pause (0.0008)
M(ct)=getframe(gcf);
ct=ct+1;
end
%vedio file
movie(M);
vediofile=VideoWriter('ode_func2.avi','Uncompressed AVI')
open(vediofile);
writeVideo(vediofile,M);
close(vediofile);
OUTPUT:


ERRORS:1.The errors i have faced is missed the comma operator,identified by the error showed in the line25 with column17
2.another error i have faced is value is not a numeric scalar.
3.another error is unrecognized variable that should be identified by showing in output after identified it is solved.



after sove this error the output is generated in below image

PLOTTING:first we create a figure and use plot of angularvelocity,displacement and time.

VEDIO:finally the vedio is created.
https://drive.google.com/file/d/1NzXaN_Rpo7SHxb-2nSgreUjNOfA6cxTd/view?usp=sharing
CONCLUSION:The program has been created successfully and creating vedio of simple pendulum by using a matlab.