Write a code in Matlab to show Routh stability criterion with GUI and show it on a graph please. thank you. (please explain it clearly)
The Correct Answer and Explanation is:
✅ MATLAB Code (Routh Stability with GUI and Graph)
Save this file as routh_gui.m and run it:
function routh_gui
% Create a simple GUI for Routh-Hurwitz criterion
% Main figure
f = figure('Name','Routh Stability Criterion','Position',[500 300 500 400]);
% Input label and field
uicontrol('Style','text','Position',[50 340 400 30],'String','Enter the coefficients of the characteristic polynomial (highest power first):');
coeffBox = uicontrol('Style','edit','Position',[50 310 400 30],'String','1 3 2 4');
% Button to compute stability
uicontrol('Style','pushbutton','Position',[200 270 100 30],'String','Check Stability',...
'Callback',@checkStability);
% Axes for plot
ax = axes('Units','pixels','Position',[75 50 350 200]);
function checkStability(~,~)
% Get user input and convert to numeric
coeffStr = get(coeffBox, 'String');
coeffs = str2num(coeffStr); %#ok<ST2NM>
if isempty(coeffs) || ~isnumeric(coeffs)
msgbox('Please enter valid numeric coefficients.','Error','error');
return;
end
% Routh-Hurwitz calculation
[stable, rhTable] = routh_criterion(coeffs);
% Display results
if stable
msg = 'System is STABLE: All first column elements have same sign.';
else
msg = 'System is UNSTABLE: Sign change(s) detected in first column.';
end
msgbox(msg, 'Stability Result');
% Plot poles
roots_poly = roots(coeffs);
cla(ax);
plot(ax, real(roots_poly), imag(roots_poly), 'rx', 'MarkerSize',10, 'LineWidth',2);
hold(ax, 'on');
plot(ax, [0 0], ylim(ax), 'k--'); % y-axis
title(ax, 'Pole Locations (Root Locus)');
xlabel(ax, 'Real Axis');
ylabel(ax, 'Imaginary Axis');
grid(ax, 'on');
hold(ax, 'off');
end
end
% ---------- Helper Function: Routh Table ----------
function [isStable, routhTable] = routh_criterion(coeffs)
n = length(coeffs);
routhTable = zeros(n, ceil(n/2));
% First two rows
routhTable(1, :) = coeffs(1:2:end);
routhTable(2, 1:min(length(coeffs)-1, ceil(n/2))) = coeffs(2:2:end);
for i = 3:n
for j = 1:size(routhTable,2)-1
a = routhTable(i-2,1);
b = routhTable(i-2,j+1);
c = routhTable(i-1,1);
d = routhTable(i-1,j+1);
if c == 0, c = 1e-6; end
routhTable(i,j) = ((c*b)-(a*d))/c;
end
end
first_column = routhTable(:,1);
first_column = first_column(~isnan(first_column) & ~isinf(first_column)); % Clean
isStable = all(sign(first_column) > 0) || all(sign(first_column) < 0);
end
📘 Explanation
The Routh-Hurwitz Stability Criterion is a mathematical test used in control systems to determine whether a linear time-invariant (LTI) system is stable, without explicitly calculating the roots of its characteristic equation. The test is based on constructing a Routh array (table) from the coefficients of the characteristic polynomial. The system is stable if and only if all the elements in the first column of the Routh table have the same sign (no sign changes).
This MATLAB GUI allows users to input the coefficients of a polynomial such as 1 3 2 4 (which represents s3+3s2+2s+4s^3 + 3s^2 + 2s + 4). Once entered, pressing the “Check Stability” button:
- Parses the polynomial.
- Constructs the Routh table using a loop, handling zeros carefully.
- Checks for sign changes in the first column.
- Displays a message indicating stability or instability.
- Plots the roots (system poles) on the complex plane, aiding visual understanding.
Graphically, the real parts of the poles determine stability. If any pole lies in the right half-plane (RHP), the system is unstable. This visualization complements the Routh test, which may be less intuitive for students or engineers unfamiliar with the method.
This tool bridges theory and practice, providing both numeric analysis (Routh table) and visual feedback (root plot) — an effective educational and engineering tool for system stability analysis.
Here’s a MATLAB GUI application that demonstrates the Routh-Hurwitz Stability Criterion, allowing a user to input a polynomial (characteristic equation) and see whether the system is stable. It also includes a graphical visualization of pole locations.
Graphically, the real parts of the poles determine stability. If any pole lies in the right half-plane (RHP), the system is unstable. This visualization complements the Routh test, which may be less intuitive for students or engineers unfamiliar with the method.
This tool bridges theory and practice, providing both numeric analysis (Routh table) and visual feedback (root plot) — an effective educational and engineering tool for system stability analysis.
