AIM:
To solve the ode and make the pendulum animation
GOVERNING EQUATIONS:
A second oder ode is solved using the mat lab fo a pendulum.
OBJECTIVES OF THE PROGRAM:
The ode can be solved using the ode command this can only be used to solve first order ode.
Thus a function is used to break the 1st ordr ode to single order ode and thus the both he ode are solved.
The pendulum can be moved around by tye angle using this the the sin and cos the adjacent and hypotenuse can be found to the making of the length at the side.
THE BODY OF THE CONTENT:
clear all used to clear the screen
close all
clc
b=0.05; velocity
g=9.81; gravity
l=1; length
m=0.1; mass
% initial condition.
theta_0 = [0;5];
% time points
t_span= linspace(0,10,500); time at the pendulum is moving is defined
% solve ODE
[t, results] = ode45(@(t,theta) ode_func(t,theta,b,g,l,m),t_span,theta_0);
Ode is used to solve the 1st order equation a function is splited to 2 odes to solve and called to the amin program.
Plotting of the pointz of the pendulum:
plot(t, results (:,1))
hold on
plot(t, results (:,2))
ylabel('Plot')
xlabel('time')
% animation of the pendulum
ct=1; initial is given as 1
figure(2)
for i=1:length(results (:,1)); the for loop is defined with the results
Initial coordinates conditions are defined:
x_0=0;
y_0=0;
The next points are from the results
x_1=1*sin (results(i,1));
y_1=1*cos (results(i,1));
plot ([-1.5 1.5],[0 0]) the points are plotted
hold on
plot ([x_0 x_1], [y_0 y_1], 'linewidth',1,'color', 'b') the pendulum dimensions are plotted
axis ([-1.5 1.5 -1.5 1.5])
plot (x_1, y_1,'o','MarkerSize', 20,'color','r')
pause (0.0001)
Pause the value
M(ct) =getframe(gcf)
hold off
ct=ct+1
end
%Making the video file
The animation od the pendulum can be obtained from the following command
movie (M)
videofile =VideoWriter('simple_pendulum.avi')
open(videofile)
writeVideo(videofile,M)
close (videofile)
The function to call to the main program is defined BELOW
THE SECOND order ode is breAK DOWN INTO single order ode and called to the main 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/l)*sin(theta1);
dtheta_dt = [dtheta1_dt;dtheta2_dt];
end
End of the function
conclusion
the movie animation is obtained.
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
clear all
close all
clc
b=0.05;
g=9.81;
l=1;
m=0.1;
% initial condition.
theta_0 = [0;5];
% time points
t_span= linspace(0,10,500);
% 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))
ylabel('Plot')
xlabel('time')
% animation of the pendulum
ct=1;
figure(2)
for i=1:length(results (:,1));
x_0=0;
y_0=0;
x_1=1*sin (results(i,1));
y_1=1*cos (results(i,1));
plot ([-1.5 1.5],[0 0])
hold on
plot ([x_0 x_1], [y_0 y_1], 'linewidth',1,'color', 'b')
axis ([-1.5 1.5 -1.5 1.5])
plot (x_1, y_1,'o','MarkerSize', 20,'color','r')
pause (0.0001)
M(ct) =getframe(gcf)
hold off
ct=ct+1
end
%Making the video file
movie (M)
videofile =VideoWriter('simple_pendulum.avi')
open(videofile)
writeVideo(videofile,M)
close (videofile)