YouTube Link
https://youtu.be/KENwGCXh254
Main Code:
clear all
close all
clc
b = 0.05;
g = 9.81;
l = 1;
m = 1;
%initial conditions
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);
%ODE45 is a pre-installed solver which performs a direct numerical integration of a set of differential equations. Hence, here we are giving the angular displacement and time interval for each angular displacement the time interval. And in function we are giving the values and the time span and angle.
plot(t, results(:,1))
hold on
plot(t, results(:,2))
ylabel('velocity')
xlabel('time')
ct = 1;
for i = 1:length(results(:,1))
x0 = 0
y0 = 0
x1 = 1*sind(results(i,1));
y1 = 1*cosd(results(i,1));
plot([-1 1], [0,0],'linewidth',3,'color','g');
hold on
line([x0 x1], [y0 y1],'linewidth',3,'color','b');
hold on
grid on
axis([-1.5 1.5 0 1.5])
pause(0.0001);
hold off
M(ct) = getframe(gcf);
ct = ct + 1;
end
movie(M)
videofile = VideoWriter('pendulum_ode.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
function code:
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)*sind(theta1);
dtheta_dt = [dtheta1_dt; dtheta2_dt];
end
%I'm facing trouble with the inverted pendulum. I have tried many ways but was unable to solve it. Could you explain where I'm facing the issue?