%--------------------------------------------------------------------------
% Simulation avec Correcteur PI (Version Simplifiée)
%--------------------------------------------------------------------------
% Effacer l'environnement
clear; close all; clc;
% --- Paramètres ---
numHd = [1, -3]; % Numérateur de Hd(z)
denHd = [1, -1.3, 0.7]; % Dénominateur de Hd(z)
T = 1; % Période d'échantillonnage (s)
Kp = 0.1; % Gain Proportionnel du PI
Ki = 0.05; % Gain Intégral du PI
Tfinal = 100 * T; % Temps final de simulation
% --- Création des Fonctions de Transfert ---
Hd = tf(numHd, denHd, T);
% Correcteur PI: C(z) = Kp + Ki*T/(z-1) = (Kp*z + Ki*T - Kp) / (z-1)
C = tf([Kp, Ki*T - Kp], [1, -1], T);
% --- Système en Boucle Fermée ---
% G_BF(z) = C(z)Hd(z) / (1 + C(z)Hd(z))
G_BF_new = feedback(C * Hd, 1);
% --- Simulation de la Réponse Indiciaire ---
% step calcule la réponse à un échelon unitaire
[y_new, t] = step(G_BF_new, Tfinal);
% --- Calcul de l'Erreur ---
% Erreur = Référence (Échelon = 1) - Sortie
epsilon_p = 1 - y_new;
% --- Affichage de l'Erreur Statique (pour vérification) ---
% L'erreur statique devrait être proche de zéro si stable
static_error = 1 - dcgain(G_BF_new);
fprintf('Erreur de position statique calculée : %.4f\n', static_error);
% --- Représentation Graphique ---
figure;
% Graphique 1: Réponse y(k)
subplot(2, 1, 1);
stairs(t, y_new, 'b', 'LineWidth', 1.5); % Utilisation de stairs pour signal discret
hold on;
plot(t, ones(size(t)), 'r--', 'LineWidth', 1); % Ligne de référence
hold off;
grid on;
title('Réponse Indiciaire y(k) avec Correcteur PI');
xlabel('Temps (s)');
ylabel('Amplitude y(k)');
legend('Sortie y_{new}(k)', 'Référence = 1', 'Location', 'best');
axis tight; % Ajuste les axes
% Graphique 2: Erreur epsilon_p(k)
subplot(2, 1, 2);
stairs(t, epsilon_p, 'm', 'LineWidth', 1.5);
hold on;
plot(t, zeros(size(t)), 'k:', 'LineWidth', 1); % Ligne d'erreur nulle
hold off;
grid on;
title('Erreur de Position \epsilon_p(k)');
xlabel('Temps (s)');
ylabel('Erreur \epsilon_p(k)');
legend('Erreur \epsilon_p(k)', 'Erreur Cible = 0', 'Location', 'best');
axis tight;
% Titre global pour la figure
sgtitle(sprintf('Réponse Système avec PI (Kp=%.2f, Ki=%.2f, T=%.1fs)', Kp, Ki, T));