Está en la página 1de 5

function double_pendulum_simulation_2()

% Parámetros del péndulo doble


l1 = 1; % Longitud del primer péndulo
l2 = 1; % Longitud del segundo péndulo
m1 = 1; % Masa del primer péndulo
m2 = 1; % Masa del segundo péndulo
g = 9.81; % Aceleración debida a la gravedad

% Condiciones iniciales
theta1_0 = pi/4; % Ángulo inicial del primer péndulo
theta2_0 = pi/2; % Ángulo inicial del segundo péndulo
theta1_dot_0 = 0; % Velocidad angular inicial del primer péndulo
theta2_dot_0 = 0; % Velocidad angular inicial del segundo péndulo

% Tiempo de simulación
t_start = 0; % Tiempo inicial
t_end = 5; % Tiempo final
dt = 0.1; % Paso de tiempo

% Número de pasos de simulación


num_steps = floor((t_end - t_start) / dt) + 1;

% Matrices para almacenar los resultados


theta1 = zeros(num_steps, 1);
theta2 = zeros(num_steps, 1);
theta1_dot = zeros(num_steps, 1);
theta2_dot = zeros(num_steps, 1);
theta1_ddot = zeros(num_steps, 1);
theta2_ddot = zeros(num_steps, 1);

% Condiciones iniciales
theta1(1) = theta1_0;
theta2(1) = theta2_0;
theta1_dot(1) = theta1_dot_0;
theta2_dot(1) = theta2_dot_0;

% Simulación del péndulo doble utilizando el método de Euler


for i = 1:num_steps-1
% Cálculo de las aceleraciones angulares
theta1_ddot(i) = calculate_theta1_ddot(theta1(i), theta2(i),
theta1_dot(i), theta2_dot(i), l1, l2, m1, m2, g);
theta2_ddot(i) = calculate_theta2_ddot(theta1(i), theta2(i),
theta1_dot(i), theta2_dot(i), l1, l2, m1, m2, g);

% Actualización de las velocidades angulares


theta1_dot(i+1) = theta1_dot(i) + dt * theta1_ddot(i);
theta2_dot(i+1) = theta2_dot(i) + dt * theta2_ddot(i);

% Actualización de los ángulos


theta1(i+1) = theta1(i) + dt * theta1_dot(i+1);
theta2(i+1) = theta2(i) + dt * theta2_dot(i+1);
end

1
% Cálculo de las coordenadas x, y para cada péndulo
x1 = l1 * sin(theta1);
y1 = -l1 * cos(theta1);
x2 = x1 + l2 * sin(theta2);

y2 = y1 - l2 * cos(theta2);

% Gráfica del movimiento del péndulo doble


figure;
for i = 1:num_steps
% Dibuja los péndulos en cada posición
plot([0, x1(i)], [0, y1(i)], 'r-', 'LineWidth', 2); % Péndulo 1
hold on;
plot(x1(i), y1(i), 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r'); %
Punto de masa del péndulo 1
plot([x1(i), x2(i)], [y1(i), y2(i)], 'b-', 'LineWidth', 2); % Péndulo
2
plot(x2(i), y2(i), 'bo', 'MarkerSize', 10, 'MarkerFaceColor', 'b'); %
Punto de masa del péndulo 2
xlim([-2, 2]); % Rango del eje x
ylim([-2, 2]); % Rango del eje y
xlabel('x');
ylabel('y');
title('Movimiento del péndulo doble');
grid on;
drawnow; % Actualiza la figura en cada iteración
hold off;
end

% Gráfica de la trayectoria del péndulo


figure;
plot(x2, y2, 'b-', 'LineWidth', 2);
hold on;
plot(x2(1), y2(1), 'ro', 'MarkerSize', 5, 'MarkerFaceColor', 'r');
plot(x2(end), y2(end), 'ko', 'MarkerSize', 5, 'MarkerFaceColor', 'k');
xlabel('x');
ylabel('y');
title('Trayectoria del péndulo doble');
grid on;

% Gráfica de las aceleraciones angulares


figure;
plot(0:dt:t_end, theta1_ddot, 'r-', 'LineWidth', 2);
hold on;
plot(0:dt:t_end, theta2_ddot, 'b-', 'LineWidth', 2);
xlabel('Tiempo');
ylabel('Aceleración Angular');
legend('Péndulo 1', 'Péndulo 2');
title('Aceleraciones angulares del péndulo doble');
grid on;
end

% Función para el cálculo de la aceleración angular del primer péndulo

2
function theta1_ddot = calculate_theta1_ddot(theta1, theta2, theta1_dot,
theta2_dot, l1, l2, m1, m2, g)
num1 = -g * (2 * m1 + m2) * sin(theta1) - m2 * g * sin(theta1 - 2 *
theta2) - 2 * sin(theta1 - theta2) * m2 * (theta2_dot^2 * l2 + theta1_dot^2 *
l1 * cos(theta1 - theta2));
den1 = l1 * (2 * m1 + m2 - m2 * cos(2 * theta1 - 2 * theta2));
theta1_ddot = num1 / den1;
end

% Función para el cálculo de la aceleración angular del segundo péndulo


function theta2_ddot = calculate_theta2_ddot(theta1, theta2, theta1_dot,
theta2_dot, l1, l2, m1, m2, g)
num2 = 2 * sin(theta1 - theta2) * (theta1_dot^2 * l1 * (m1 + m2) + g * (m1
+ m2) * cos(theta1) + theta2_dot^2 * l2 * m2 * cos(theta1 - theta2));
den2 = l2 * (2 * m1 + m2 - m2 * cos(2 * theta1 - 2 * theta2));
theta2_ddot = num2 / den2;
end

3
4
Published with MATLAB® R2023a

También podría gustarte