Matrix Laboratory
Matrix Laboratory
MATLAB, which stands for Matrix Laboratory, is a high-level programming language and
interactive environment used for numerical computation, visualization, and programming. It is widely
utilized in academia and industry for various applications, ranging from data analysis and simulation
to algorithm development and scientific research.
Key Features of MATLAB
1. High-Level Language: MATLAB provides a high-level language with built-in functions for
complex mathematical calculations, data analysis, and visualization.
2. Interactive Environment: The MATLAB environment includes tools for managing
variables, importing and exporting data, and developing scripts and functions interactively.
3. Extensive Function Library: MATLAB comes with a comprehensive library of pre-built
functions for linear algebra, statistics, Fourier analysis, filtering, optimization, and numerical
integration.
4. Plotting and Visualization: MATLAB offers powerful tools for creating 2D and 3D plots,
graphs, and visualizations, making it easy to interpret and present data.
5. Toolboxes: MATLAB provides specialized toolboxes for different applications, such as signal
processing, control systems, neural networks, and machine learning. These toolboxes extend
MATLAB's functionality and provide additional tools for specific tasks.
6. Simulink: A complementary environment to MATLAB, Simulink is used for modeling,
simulating, and analyzing dynamic systems. It provides a graphical editor for building block
diagrams and integrates seamlessly with MATLAB.
Why Use MATLAB for Control Systems?
1. Modeling and Simulation: MATLAB allows engineers to create mathematical models
of control systems and simulate their behavior. This helps in predicting how the system
will respond to various inputs and disturbances without building a physical prototype.
2. Design and Analysis: MATLAB provides functions and toolboxes specifically for
control system design and analysis. Engineers can design controllers (such as PID
controllers), analyze system stability, and optimize performance using tools like Bode
plots, Nyquist plots, and root locus diagrams.
3. Visualization: MATLAB's extensive plotting capabilities help visualize system
responses, control signals, and other important data, making it easier to diagnose issues
and verify performance.
4. Optimization: MATLAB includes optimization tools to fine-tune system parameters
and achieve the desired performance. This is crucial for control systems that need
precise adjustments.
5. Control System Toolbox: This toolbox includes functions for creating and
manipulating transfer functions, state-space models, and performing various control
design tasks. It simplifies the process of designing, analyzing, and tuning control
systems.
6. Real-Time Implementation: MATLAB, together with Simulink, can generate real-
time code for control systems. This allows the designed controllers to be implemented
on hardware platforms for real-world applications.
Code:
% Define the numerator and denominator coefficients of the transfer function
numerator = [1 5];
% Example: Replace with the actual numerator coefficients
denominator = [1 10 20];
% Example: Replace with the actual denominator coefficients
% Create the transfer function
sys = tf(numerator, denominator);
% Display the transfer function
disp('The transfer function of the system is:');
%disp(sys);
sys
% Plot the step response to visualize the system behavior (optional)
figure;
step(sys);
title('Step Response of the System');
xlabel('Time (seconds)');
ylabel('Amplitude');
output:
Observation :
1.Initial Response:
The response starts at 0 since the system is initially at rest.
The system's reaction begins immediately due to the step input.
2.Transient Behavior:
The response will show an initial rise that may include:
o Overshoot: The response might exceed the final steady-state value before
settling down.
o Oscillations: The system could exhibit minor oscillations depending on the
damping ratio.
3.Steady-State Value:
The response eventually settles at a steady-state value, which depends on the system's
poles and zeros.
For this transfer function, the system stabilizes as it has no poles on the right-hand
plane (indicating a stable system).
4.Key Characteristics:
Rise Time: The time taken for the response to go from 10% to 90% of the final value.
Settling Time: The time it takes for the response to remain within a specified
tolerance (e.g., ±2% of the final value).
Overshoot: If present, how much the response exceeds the final steady-state value.
Damping: The system's poles determine the damping ratio. Given the denominator
s2+10s+20s^2 + 10s + 20s2+10s+20, the system likely exhibits an underdamped
response.
5.Shape of the Response:
The response is likely smooth with some curvature due to the second-order dynamics
of the system.
2. Write a MATLAB program to determine the response of first-order and
second-order systems for step input for various values of constant 'K'
using a linear simulator unit, and compare theoretical and practical
results.
Code:
K_values = [1, 2, 5, 10]; % Example values of K
% Define the time vector for simulation
t = 0:0.01:5; % Time range from 0 to 5 seconds with a step of 0.01
% First-order system: H(s) = K / (?s + 1)
tau1 = 1; % Time constant for the first-order system
for K = K_values
% Transfer function for the first-order system
num1 = [K];
den1 = [tau1, 1];
sys1 = tf(num1, den1);
sys1
% Compute the step response
[y1, t1] = step(sys1, t);
% Plot the response
figure;
plot(t1, y1);
title(['Step Response of First-Order System with K = ', num2str(K)]);
xlabel('Time (seconds)');
ylabel('Response Amplitude');
grid on;
end
% Second-order system: H(s) = K?_n^2 / (s^2 + 2??_n s + ?_n^2)
omega_n = 5; % Natural frequency
zeta = 0.5; % Damping ratio
for K = K_values
% Transfer function for the second-order system
num2 = [K * omega_n^2];
den2 = [1, 2 * zeta * omega_n, omega_n^2];
sys2 = tf(num2, den2);
sys2
% Compute the step response
[y2, t2] = step(sys2, t);
% Plot the response
figure;
plot(t2, y2);
title(['Step Response of Second-Order System with K = ', num2str(K)]);
xlabel('Time (seconds)');
ylabel('Response Amplitude');
grid on;
end
Output:
Observations:
1. First-Order System:
Code:
numerator = [1 3]; % Example numerator coefficients, modify as per the given system
denominator = [1 5 6]; % Example denominator coefficients, modify as per the given
system
% Create the transfer function
sys = tf(numerator, denominator);
% Display the transfer function
disp('The transfer function of the system is:');
disp(sys);
sys
% Plot the step response
figure;
step(sys);
title('Step Response of the System');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid on;
% Plot the impulse response
figure;
impulse(sys);
title('Impulse Response of the System');
xlabel('Time (seconds)');
ylabel('Amplitude');
grid on;
% Time vector for custom input response
t = 0:0.01:10; % Time from 0 to 10 seconds
u = sin(t); % Example input signal: sine wave
% Simulate system response to the custom input
[y, t_out] = lsim(sys, u, t);
% Plot the custom input response
figure;
plot(t_out, y, 'r', 'LineWidth', 1.5);
hold on;
plot(t, u, 'b--', 'LineWidth', 1);
title('System Response to Custom Input (Sine Wave)');
xlabel('Time (seconds)');
ylabel('Amplitude');
legend('System Output', 'Input Signal');
grid on;
Output:
Observation:
1. Step Response
Graph: The step response shows how the system reacts to a unit step input u(t)= 1
applied at t=0.
Key Observations:
Initial Response: The system output starts at zero since no initial output exists
at t=0.
Transient Phase: The system output gradually rises toward its steady-state
value, indicating the dynamics of the system (speed, damping, etc.).
Steady-State Value: The final value depends on the transfer function's
numerator and denominator:
Steady-State Value=limt→∞y(t) Calculated using the final value theorem.
2. Impulse Response
Graph: The impulse response shows the system’s reaction to a unit impulse input
δ(t), which represents a sudden "shock" to the system.
Key Observations:
The response decays as the system stabilizes over time.
The amplitude and duration of the response depend on system poles and
damping:
Overdamped: Decay without oscillations.
Underdamped: Oscillatory decay.
Critically Damped: Fast decay with no oscillations.
3. Custom Input (Sine Wave) Response
Graph: The system's output to a sine wave input u(t)= sin(t) is plotted alongside the
input signal.
Key Observations:
o The output amplitude and phase shift depend on the system's transfer function
and the input frequency.
o At low frequencies, the system output closely follows the input (minimal
phase lag and amplitude attenuation).
o At higher frequencies, the system may attenuate the input significantly due to
filtering properties.
4.Write a MATLAB program to study P, PI, and PID controllers and
compare their characteristics.
Code:
% Define the plant transfer function
numerator = [1];
denominator = [1, 5, 6]; % Example plant: G(s) = 1 / (s^2 + 5s + 6)
plant = tf(numerator, denominator);
% Define controller parameters
Kp = 2; % Proportional gain
Ki = 1; % Integral gain
Kd = 1; % Derivative gain
% Create P, PI, and PID controllers
controller_P = Kp; % P controller
controller_PI = tf([Kp, Ki], [1, 0]); % PI controller
controller_PID = tf([Kd, Kp, Ki], [1, 0]); % PID controller
% Closed-loop transfer functions
system_P = feedback(controller_P * plant, 1);
system_PI = feedback(controller_PI * plant, 1);
system_PID = feedback(controller_PID * plant, 1);
% Time vector for simulation
t = 0:0.01:10;
% Step responses
[y_P, t_P] = step(system_P, t);
[y_PI, t_PI] = step(system_PI, t);
[y_PID, t_PID] = step(system_PID, t);
% Plot step responses
figure;
plot(t_P, y_P, 'r', 'LineWidth', 1.5); hold on;
plot(t_PI, y_PI, 'g', 'LineWidth', 1.5);
plot(t_PID, y_PID, 'b', 'LineWidth', 1.5);
title('Comparison of P, PI, and PID Controllers');
xlabel('Time (seconds)');
ylabel('Amplitude');
legend('P Controller', 'PI Controller', 'PID Controller');
grid on;
output:
Observation:
1. P Controller Response (Red Line):
The Proportional (P) controller generally improves the speed of the system compared
to the open-loop response.
However, it may still exhibit steady-state error since it lacks the ability to eliminate it
completely.
The response may show overshoot and some oscillations, depending on the plant
dynamics and gain KpK_pKp.
2. PI Controller Response (Green Line):
The Proportional-Integral (PI) controller introduces an integral action that helps
eliminate the steady-state error.
The response is typically slower compared to the P controller because the integral
term increases the system's order, which can reduce the overall speed.
There may still be some overshoot and possible oscillations, but steady-state error is
reduced or eliminated.
3.PID Controller Response (Blue Line):
The Proportional-Integral-Derivative (PID) controller combines proportional, integral,
and derivative actions to balance speed, accuracy, and stability.
The derivative term improves the system's response by reducing overshoot and
damping oscillations.
This often leads to a faster settling time and a more stable response with minimal
steady-state error compared to the P and PI controllers.
Code:
% Define system parameters
omega_n = 5; % Natural frequency (rad/s)
zeta = 0.7; % Damping ratio
% Define the transfer function of the second-order system
num = omega_n^2; % Numerator (ω_n^2)
den = [1 2*zeta*omega_n omega_n^2]; % Denominator (s^2 + 2ζω_ns + ω_n^2)
sys = tf(num, den); % Transfer function
% Time vector for simulation
t = 0:0.01:10; % Time from 0 to 10 seconds with 0.01s intervals
% Step response
[y, t] = step(sys, t);
% Plot the step response
figure;
plot(t, y, 'b', 'LineWidth', 2);
grid on;
xlabel('Time (seconds)');
ylabel('Response');
title('Step Response of Second-Order System');
% Performance Parameters
% Peak time (Tp)
[~, idx_peak] = max(y);
Tp = t(idx_peak);
% Settling time (Ts)
Ts_idx = find(abs(y - 1) <= 0.02, 1, 'last'); % 2% criteria
Ts = t(Ts_idx);
% Overshoot (Mp)
Mp = (max(y) - 1) * 100; % Percentage overshoot
% Steady-state value
steady_state_value = y(end);
% Display performance parameters
fprintf('Peak Time (Tp): %.2f seconds\n', Tp);
fprintf('Settling Time (Ts): %.2f seconds\n', Ts);
fprintf('Overshoot (Mp): %.2f%%\n', Mp);
fprintf('Steady-State Value: %.2f\n', steady_state_value);
% Theoretical calculations for verification
Mp_theory = exp(-pi*zeta / sqrt(1 - zeta^2)) * 100;
Ts_theory = 4 / (zeta * omega_n);
fprintf('Theoretical Overshoot (Mp_theory): %.2f%%\n', Mp_theory);
fprintf('Theoretical Settling Time (Ts_theory): %.2f seconds\n', Ts_theory);
output:
Observation:
1.General Shape:
The step response shows a smooth rise towards the final value, characteristic of a
second-order system with moderate damping (ζ=0.7\zeta = 0.7ζ=0.7).
The system does not exhibit sustained oscillations due to the damping ratio being less
than 1 but greater than 0.5.
2. Overshoot:
The response briefly overshoots the final steady-state value before settling.
This overshoot is moderate due to the damping ratio being less than 1 but not
excessively small.
3.Settling Time:
The response settles within 2% of the final value relatively quickly, indicating good
damping characteristics.
The response reaches a stable value without significant oscillations.
6.Write a MATLAB program to plot the root locus diagram of an open-
loop transfer function and determine the range of gain 'K' for stability.
Code:
num = [1]; % Numerator coefficients (Example: 1)
den = [1 3 2]; % Denominator coefficients (Example: s^2 + 3s + 2)
% Create the transfer function
sys = tf(num, den);
% Plot the root locus
figure;
rlocus(sys);
title('Root Locus Diagram');
grid on;
% Calculate the root locus data points and corresponding gains
[r, k] = rlocus(sys);
% Determine the range of gain K for stability
% The system is stable if all poles have negative real parts
stable_range = all(real(r) < 0, 1); % Check stability across each gain value
% Find the maximum stable gain
if any(stable_range)
K_stable_max = max(k(stable_range));
fprintf('The system is stable for K in the range: 0 < K < %.2f\n', K_stable_max);
else
fprintf('No stable gain K found for the given system.\n');
end
output:
Observation:
1.Root Locus Plot:
The root locus plot displays the trajectories of the system's poles as the proportional
gain KKK varies from 000 to ∞\infty∞.
For the denominator s2+3s+2s^2 + 3s + 2s2+3s+2, the characteristic equation is:
1+KG(s)=0 ⟹ s2+3s+(2+K)=01 + K G(s) = 0 \implies s^2 + 3s + (2 + K) =
01+KG(s)=0⟹s2+3s+(2+K)=0
2. Key Features of the Plot:
The poles of the system (with K=0K = 0K=0) are initially at: s=−1ands=−2s = -1
\quad \text{and} \quad s = -2s=−1ands=−2
As KKK increases, the poles move along the root locus branches.
3. Stability Analysis:
The system is stable if all poles have negative real parts (i.e., are located in the left-
half plane).
The plot shows that the poles initially lie on the left-half plane and move along
trajectories depending on KKK.
4. Maximum Stable Gain KKK:
The maximum value of KKK for which all poles remain in the left-half plane
determines the range of stable gains.
From the analysis:
o The maximum stable gain is found to be finite, after which the poles cross into
the right-half plane, causing instability.
5.Graph Observations:
Initial poles: Located at −1-1−1 and −2-2−2.
Movement: As KKK increases, the poles move toward each other and eventually
diverge.
Stability limit: There exists a maximum gain KKK where both poles remain in the
left-half plane. Beyond this, the system becomes unstable.
Code:
Observation:
1. Magnitude Plot :
The magnitude starts at a relatively high value for low frequencies and decreases as
the frequency increases.
Since the system is a second-order low-pass filter with two poles, the magnitude
decreases at a rate of -40 dB/decade beyond the breakpoints.
The poles at s=−1s = -1s=−1 and s=−2s = -2s=−2 introduce breakpoints at
corresponding frequencies:
o Breakpoints: Approximately at ω=1\omega = 1ω=1 rad/s and ω=2\omega =
2ω=2 rad/s.
2.Phase Plot :
At low frequencies, the phase starts near 0°.
As the frequency increases, each pole contributes a -90° phase shift, leading to a total
phase shift of approximately -180° at high frequencies.
The phase transitions occur around the break frequencies of 1 rad/s and 2 rad/s.
3.Low-Frequency Behavior:
o The magnitude is nearly flat at low frequencies, indicating that the system
allows low-frequency signals to pass through with little attenuation.
4.High-Frequency Behavior:
o The magnitude decreases significantly due to the two poles, indicating that the
system attenuates high-frequency signals.
o The phase approaches -180° at higher frequencies.
5.Breakpoints:
o The system has noticeable changes in slope and phase around the breakpoints
at ω=1\omega = 1ω=1 rad/s and ω=2\omega = 2ω=2 rad/s.
6.System Characteristics:
o The system behaves like a typical second-order low-pass filter:
Allows low frequencies to pass.
Reduces gain and introduces phase lag at higher frequencies.
8.Write a MATLAB code to plot a Nyquist diagram for an open-loop
transfer function and examine the stability of the closed-loop system,
verifying it theoretically.
Code:
% Define the transfer function coefficients
num = [1]; % Numerator coefficients (Example: 1)
den = [1 3 2]; % Denominator coefficients (Example: s^2 + 3s + 2)
% Create the transfer function
sys = tf(num, den);
% Plot the Nyquist diagram
figure;
nyquist(sys);
grid on;
title('Nyquist Diagram of the Open-Loop Transfer Function');
% Check stability using Nyquist criterion
[re, im, w] = nyquist(sys);
% Determine the number of encirclements of the point (-1,0)
encirclements = sum((re < 0) & (re > -1) & (im == 0));
if encirclements == 0
fprintf('The closed-loop system is stable.\n');
else
fprintf('The closed-loop system is unstable.\n');
end
% Theoretical verification using Nyquist criterion
% Stability is determined by the number of right-half plane poles (P)
P = 0; % Number of poles in the right-half plane for the open-loop system
Z = P + encirclements; % Number of right-half plane poles for the closed-loop system
if Z == 0
fprintf('Theoretical Verification: The closed-loop system is stable.\n');
else
fprintf('Theoretical Verification: The closed-loop system is unstable.\n');
end
output:
Observation:
1.Nyquist Diagram:
o The Nyquist plot will show the frequency response of the transfer function
over a range of frequencies ω\omegaω.
o Since both poles are in the left-half plane, the plot should:
Start and end in the left-half plane of the complex plane.
Not encircle the point (−1,0)(-1, 0)(−1,0) because the system has no
poles in the right-half plane.
2.Encirclements:
o The code attempts to determine the number of encirclements around the
critical point (−1,0)(-1, 0)(−1,0).
o For this system, the Nyquist plot does not encircle the point (−1,0)(-1,
0)(−1,0), indicating the closed-loop system is stable.
3.Stability Criterion:
o According to the Nyquist criterion:
Since the open-loop system has no right-half plane poles (P=0P =
0P=0), the system is stable if there are no encirclements of (−1,0)(-1,
0)(−1,0).
o The code verifies this by checking encirclements, which should be 0,
confirming stability.
4.Nyquist Curve:
o The curve will be a continuous path in the complex plane that does not pass
through or encircle the point (−1,0)(-1, 0)(−1,0).
5.Grid:
o The grid helps in identifying the position of the curve relative to the real and
imaginary axes.
6.Title:
o The title "Nyquist Diagram of the Open-Loop Transfer Function" helps in
identifying the plot.
Conclusion:
In conclusion, MATLAB serves as an indispensable tool in the realm of control
systems engineering. Its robust computational power, extensive function libraries, and
specialized toolboxes facilitate the entire spectrum of control system activities, from
modeling and simulation to design, analysis, and real-time implementation.
MATLAB's ability to visualize complex data, optimize system parameters, and
integrate seamlessly with Simulink for dynamic system modeling makes it a
comprehensive solution for both educational and professional applications. By
leveraging MATLAB, engineers and researchers can efficiently develop, test, and
refine control systems, ultimately leading to improved system performance and
reliability