Solving for second ODE and simulation of Pendulum
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,100);
% 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\')
ct = 1;
for i = 1:length(results(:,1))
x0 = 0;
y0 = 0;
x1 = -l*sin(results(i,1));
y1 = -l*cos(results(i,1));
figure(2)
plot([-1 1],[0,0],\'linewidth\',4,\'color\',\'b\');
hold on
plot([x0 x1],[y0 y1],\'linewidth\',4,\'color\',\'r\');
hold on
plot(x1,y1,\'-o\',\'markersize\',30,\'markerfacecolor\',\'g\',\'markeredgecolor\',\'r\');
axis([-1.5 1.5 -1.5 1]);
pause(0.03);
M(ct) = getframe(gcf);
ct = ct + 1;
end
movie(M)
videofile = VideoWriter(\'Pendulum.avi\',\'Uncompressed AVI\');
open(videofile)
writeVideo(videofile,M)
close(videofile)
In this challenge we have solved the second ODE of a pendulum and simulated the same. We have started the code by defining the parameters for the pendulum and then gave the initial conditions.
As per the challenge, the initial velocity is 3m/s and the time span is 20 second. ode45 function is used to solve the second order differential equation and the result is plotted with respect to time. Finally, the simulation of the pendulum is done.