Está en la página 1de 4

clear all

close all
M = 0.5;
m = 0.3;
L = 0.5;
g = 9.8;
 
global epsilon l0 l1 l2 l3
epsilon = M/m;
 
disp('Los (cuatro) polos se colocan en s = -1')
l3 = 4;
l2 = 6;
l1 = 4;
l0 = 1;
roots([1 l3 l2 l1 l0])
 
function dy = f(y,t)
global epsilon l0 l1 l2 l3
 
  x1 = y(1);
  x2 = y(2);
  th1 = y(3);
  th2 = y(4);
  v = -l3*(-th2)-l2*(-th1)-l1*(x2-th2)-l0*(x1-th1);
  u = (1+epsilon)*(-th1)-epsilon*v;
  dx = x2;
  d2x = ((u-th2*th2*sin(th1))+cos(th1)*sin(th1))/(epsilon+sin(th1)*sin(th1));
  dth = th2;
  d2th = (cos(th1)*(u-th2*th2*sin(th1))+(1+epsilon)*sin(th1))/(epsilon+sin(th1)*sin(th1));
  dy = [dx;d2x;dth;d2th];
end
 
# condiciones iniciales para la animacion, en coordenadas normalizadas
y0 = [2.0 0 0.1 0];
 
 
np = 1000;
ti = 0;
tf = 2*sqrt(g/L);
 
t = linspace(ti,tf,np);
y = lsode("f",y0,t);
 
set (0, "defaultlinelinewidth", 1.0, "defaulttextfontname", "Helvetica", "defaultaxesfontname", "Helvetica",
"defaultaxesfontsize", 12.0);
 
h = 0.1;
Carro = [-0.2 0.2 0.2 -0.2 -0.2;0 0 h h 0]
Base = [-1 1;0 0]
Pendulo = [0 0;0 L]
 
figure
c = 1;
for n = 1:40:np,
plot(Carro(1,:)+y(n,1)*L,Carro(2,:),'k')
hold on
G = [cos(y(n,3)) -sin(y(n,3)); sin(y(n,3)) cos(y(n,3))];
NPendulo = G*Pendulo;
plot(NPendulo(1,:)+y(n,1)*L,NPendulo(2,:)+h,'r')
plot(Base(1,:),Base(2,:),'b')
axis([-1.5 1.5 -0.5 1])
axis equal
print (sprintf("out/%05d.png", c), "-S640,480")
clf
c ++
end

Este código se usa para generar el conjunto de gráficas que se almacenan en el


directorio “out/“. Este conjunto de gráficas se transforman a un solo gif, usando el
comando convert  de Imagemagick:

convert out/*.png -loop 0 -layers Optimize carro.gif

1 convert out/*.png -loop 0 -layers Optimize carro.gif

Usando el siguiente código:


figure
plot(Base(1,:),Base(2,:),'b')
hold on
n = 1;
plot(Carro(1,:)+y(n,1)*L,Carro(2,:),'b')
G = [cos(y(n,3)) -sin(y(n,3)); sin(y(n,3)) cos(y(n,3))];
NPendulo = G*Pendulo;
plot(NPendulo(1,:)+y(n,1)*L,NPendulo(2,:)+h,'b')
for n = 100:150:np,
plot(Carro(1,:)+y(n,1)*L,Carro(2,:),'k')
G = [cos(y(n,3)) -sin(y(n,3)); sin(y(n,3)) cos(y(n,3))];
NPendulo = G*Pendulo;
plot(NPendulo(1,:)+y(n,1)*L,NPendulo(2,:)+h,'k')
end
plot(Carro(1,:),Carro(2,:),'r')
plot(Pendulo(1,:),Pendulo(2,:)+h,'r')
axis([-1.5 1.5 -0.5 1])
axis equal

se pueden apreciar varias posiciones durante el movimiento del péndulo:


Posición inicial del péndulo en azul, final en rojo.

Posición inicial del péndulo en azul, final en rojo.


Las gráficas de cada uno de los estados del sistema se muestran a continuación:

Simulación de la respuesta dinámica del péndulo invertido: variables de


estado en el tiempo
figure
subplot(221),plot(t/sqrt(g/L),y(:,1)*L)
title('Posicion x')
xlabel('tiempo')
ylabel('x')
 
subplot(222),plot(t/sqrt(g/L),y(:,2)*L)
title('Velocidad dx/dt')
xlabel('tiempo')
ylabel('dx/dt')
 
subplot(223),plot(t/sqrt(g/L),y(:,3))
xlabel('tiempo')
ylabel('Posicion angular \theta')
 
subplot(224),plot(t/sqrt(g/L),y(:,4))
xlabel('tiempo')
ylabel('Velocidad angular d\theta/dt')

Este interesante ejemplo es parte del material adicional de nuestro nuevo texto Control
por linealización.

Una animación final, agregando otros elementos con subplot:

También podría gustarte