function simulador_primer_orden() % ========================================================================= % SIMULADOR INTERACTIVO DE UN SISTEMA DE PRIMER ORDEN % ------------------------------------------------------------------------- % Esta GUI permite al usuario manipular la ganancia (K) y la constante de % tiempo (τ) de un sistema de primer orden G(s) = K / (τs + 1) y % observar la respuesta al escalón en tiempo real. % % Estrategia: GUI programática clásica para máxima compatibilidad. % % Autor: Gemini % ========================================================================= % --- 1. DEFINICIÓN DEL TAMAÑO DE LA VENTANA --- fig_width = 900; fig_height = 600; screen_size = get(groot, 'ScreenSize'); screen_width = screen_size(3); screen_height = screen_size(4); fig_pos_x = (screen_width - fig_width) / 2; fig_pos_y = (screen_height - fig_height) / 2; fig = figure('Name', 'Simulador de Sistema de Primer Orden', ... 'Units', 'pixels', 'Position', [fig_pos_x, fig_pos_y, fig_width, fig_height], ... 'NumberTitle', 'off', 'MenuBar', 'none', 'ToolBar', 'none'); % --- 2. Crear el Panel de Controles --- panel_margin = 20; control_panel_width = 250; control_panel_height = fig_height - 2*panel_margin; controlPanel = uipanel(fig, 'Title', 'Parámetros del Sistema', ... 'FontWeight', 'bold', 'Units', 'pixels', ... 'Position', [panel_margin panel_margin control_panel_width control_panel_height]); % --- 3. Crear los Componentes Interactivos --- y_pos = control_panel_height - 60; % Posición vertical inicial default_bg_color = get(fig, 'Color'); % Slider para la Ganancia (K) % ***** AJUSTE DE LAYOUT FINAL ***** uicontrol(controlPanel, 'Style', 'text', 'String', 'Ganancia (K)', 'FontWeight', 'bold', 'HorizontalAlignment', 'left', 'Position', [20 y_pos 140 22], 'BackgroundColor', default_bg_color); k_label = uicontrol(controlPanel, 'Style', 'text', 'String', '1.0', 'FontSize', 14, 'FontWeight', 'bold', 'HorizontalAlignment', 'right', 'Position', [160 y_pos-5 70 22], 'BackgroundColor', default_bg_color); y_pos = y_pos - 30; % Más espacio vertical k_slider = uicontrol(controlPanel, 'Style', 'slider', 'Min', 0.1, 'Max', 10, 'Value', 1.0, 'Position', [20 y_pos 210 20]); y_pos = y_pos - 60; % Slider para la Constante de Tiempo (τ) % ***** AJUSTE DE LAYOUT FINAL ***** uicontrol(controlPanel, 'Style', 'text', 'String', 'Constante de Tiempo (τ)', 'FontWeight', 'bold', 'HorizontalAlignment', 'left', 'Position', [20 y_pos 140 22], 'BackgroundColor', default_bg_color); tau_label = uicontrol(controlPanel, 'Style', 'text', 'String', '1.0 s', 'FontSize', 14, 'FontWeight', 'bold', 'HorizontalAlignment', 'right', 'Position', [160 y_pos-5 70 22], 'BackgroundColor', default_bg_color); y_pos = y_pos - 30; % Más espacio vertical tau_slider = uicontrol(controlPanel, 'Style', 'slider', 'Min', 0.1, 'Max', 10, 'Value', 1.0, 'Position', [20 y_pos 210 20]); % --- 4. Crear el Panel de Gráficas --- plot_panel_left = control_panel_width + 2*panel_margin; plot_panel_width = fig_width - plot_panel_left - panel_margin; plotPanel = uipanel(fig, 'Title', 'Respuesta al Escalón Unitario', 'FontWeight', 'bold', 'Units', 'pixels', 'Position', [plot_panel_left panel_margin plot_panel_width control_panel_height]); ax = axes('Parent', plotPanel, 'Units', 'normalized', 'Position', [0.1 0.1 0.85 0.85]); % --- 5. Definir la Función de Callback --- function updatePlot(~, ~) % Leer valores actuales de los sliders K = get(k_slider, 'Value'); tau = get(tau_slider, 'Value'); % Actualizar etiquetas de texto set(k_label, 'String', sprintf('%.2f', K)); set(tau_label, 'String', sprintf('%.2f s', tau)); % Crear la función de transferencia s = tf('s'); sys = K / (tau*s + 1); % Simular la respuesta T_final = 8 * tau; % Simular por 8 constantes de tiempo para ver la estabilización t = 0:T_final/1000:T_final; [y, t] = step(sys, t); % Graficar plot(ax, t, y, 'b', 'LineWidth', 2); hold(ax, 'on'); yline(ax, K, 'r--', 'LineWidth', 1.5); % Valor final % Marcar la constante de tiempo (63.2% del valor final) y_tau = 0.632 * K; plot(ax, tau, y_tau, 'go', 'MarkerFaceColor', 'g', 'MarkerSize', 8); hold(ax, 'off'); grid(ax, 'on'); % Configurar la apariencia de la gráfica title(ax, sprintf('Sistema de Primer Orden: K = %.2f, τ = %.2f s', K, tau)); xlabel(ax, 'Tiempo (s)'); ylabel(ax, 'Amplitud de Salida'); legend(ax, 'Respuesta del Sistema', 'Valor Final (K)', 'Punto en τ (63.2%)', 'Location', 'SouthEast'); % Ajustar límites para una mejor visualización xlim(ax, [0 T_final]); ylim(ax, [0 max(K*1.2, 0.1)]); % Evitar que el eje Y colapse si K es muy pequeño end % --- 6. Asignar la función de callback a los sliders --- callback_fcn = @updatePlot; addlistener(k_slider, 'ContinuousValueChange', callback_fcn); addlistener(tau_slider, 'ContinuousValueChange', callback_fcn); % --- 7. Llamada Inicial para Dibujar la Gráfica --- updatePlot(); end