solving second order ODE and animation of pandulum
Here in this program I solve second order ODE for pendulum. also plot graph of plot vs time.
how oscillation would change with the time.
Here i solve above given equation by creating function for it.
Creat animation for for the same ODE how it work when length of thred is 1m, mass = 1Kg,
and b = 0.5.
Animation video link: https://youtu.be/tnXF57sZJzM
clear all
close all
clc
b = 0.5;
g = 9.81;
l = 1;
m = 1;
%initial condition
theta_0 = [0;3];
%time points
t_span = linspace(0,20,500);
%solve ODE
[t, results] = ode45(@(t,theta) ode_function(t,theta,b,g,l,m), t_span, theta_0)
ct = 1
for i = 1:1
plot(t, results(:,1))
hold on
plot(t, results(:,2))
xlabel(\'time\')
ylabel(\'plot\')
ct = ct + 1;
end
figure(ct)
x = results(:,1);
%animation
%origine
O = [0,0];
axis([-1.5 1.5 -1.5 1.5]) %the limits of the plot
grid on
count = 1;
%loop for animation
for i = 1:length(t)
%mass point
P = l*[sin(x(i,1)) -cos(x(i,1))];
%circle in origin
O_circle = viscircles(O,0.01);
%pendulum
pend = line([O(1) P(1)], [O(2) P(2)]);
%ball
ball = viscircles(P,0.05);
%time interval to upadate the plot
pause(0.0001);
R(count) = getframe(gcf);
count = count + 1;
%delete previous object if it is not final loop
if i<length(t)
delete(pend);
delete(ball);
delete(O_circle);
end
end
movie(R)
videofile = VideoWriter(\'pandulum.avi\',\'Uncompressed AVI\');
open(videofile);
writeVideo(videofile,R)
close(videofile)