Second order ODE
Aim:
To solve the second order of ODE(Pendulum) using matlab
Plot:
Formulae:
%2nd Order ODE
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=0.1
% initial condition
theta_0 = [0;3];
% time points
t_span = linspace(0,20,200);
% solve ODE
[t, results] = ode45(@(t,theta) ode_func(t, theta, b,g,l,m),t_span, theta_0);
plot(t,results(:,1))
hold on
plot(t,results(:,2))
xlabel('time')
ylabel('plot')
ct=1;
for i = 1:length(results(:,1))
x0=0; y0=0;
angle=results(i,1);
x1=1*sin(angle);
y1=-1*cos(angle);
plot([-1 1],[0,0],'linewidth',2,'color','g')
hold on
plot([x0 x1],[y0 y1],'linewidth',2,'color','b')
hold on
plot(x1,y1,'-o','markers',20,'markerfacecolor','r');
axis([-1.5 1.5 -1.5 1.5])
pause(0.03)
hold off
M(ct) = getframe(gcf);
ct=ct+1;
end
movie(M)
videofile=VideoWriter('pendulumn.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
%2Order ODE Function
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/1)*sin(theta1);
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end
Output:
Discription:
We have to solve the second order of ODE using Matlab. The coding used in matlab are defined above. The ODE defines the movement of pendulum in fixed point. Swinging movement of pendulum in subject to gravity is measured in angle theta. The function of theta is defined in another fromula shown above using dtheta.
Conclusion:
Thus we have successfully calculated the second order of ODE in matlab.