Hi @ Matthew Pompa ,
You mentioned, “ I have a created a model array of transfer functions using stack() and they are labeled with their varying parameters using SamplingGrid.
Yarray = feedback(Garray,1); Yarray.SamplingGrid = struct('zeta',dr); figure; set(gcf,'Visible','on') step(Yarray, 60)
How do I apply a visible legend to this graph? I like the normal legend() function that applies the titles and automatically changes the colors. However, calling this function either with my desired labels or empty just has the default color line labeled as 'Yarray.' I would love if they could be labeled dynamically following the SamplingGrid structure.”
Please see my response to your comments below.
To address yours request for a dynamic legend in step response plot, I will break down the provided example code snippet and explain how it accomplishes the goal of labeling the graph according to the SamplingGrid structure.
% Define your parameters dr = [0.1, 0.2, 0.3]; % Example damping ratios for SamplingGrid numTransferFunctions = length(dr); % Number of transfer functions
% Create an array of transfer functions (Garray) s = tf('s'); % Define Laplace variable Garray = arrayfun(@(zeta) 1/(s^2 + 2*zeta*s + 1), dr, 'UniformOutput', false);
% Compute feedback for each transfer function Yarray = cellfun(@(G) feedback(G, 1), Garray, 'UniformOutput', false);
% Store sampling grid information in a separate structure SamplingGrid.zeta = dr;
% Plotting figure; set(gcf,'Visible','on'); hold on; % Hold on to plot multiple lines
% Step response for each transfer function in Yarray for i = 1:numTransferFunctions step(Yarray{i}, 60); % Plot step response for each transfer function end
% Dynamic legend based on SamplingGrid legendLabels = arrayfun(@(zeta) sprintf('\\zeta = %.1f', zeta), dr, 'UniformOutput', false); legend(legendLabels, 'Location', 'best'); % Apply legend with dynamic labels hold off; % Release hold after plotting
The following sections will detail the code's functionality, focusing on the creation of transfer functions, computation of feedback, and the implementation of a dynamic legend.
Define Parameters
The first step in the code is to define the parameters that will be used for the transfer functions. In this case, the damping ratios are specified in the array dr.
dr = [0.1, 0.2, 0.3]; % Example damping ratios for SamplingGrid numTransferFunctions = length(dr); % Number of transfer functions
Here, dr contains three damping ratios, and numTransferFunctions calculates the total number of transfer functions to be created.
Create Transfer Functions
Next, the code creates an array of transfer functions using the arrayfun function. This function applies a specified operation to each element of an array,in this case, generating a transfer function for each damping ratio.
s = tf('s'); % Define Laplace variable Garray = arrayfun(@(zeta) 1/(s^2 + 2*zeta*s + 1), dr, 'UniformOutput', false);
The transfer function is defined as ( G(s) = 1/(s^2 + 2*zeta*s + 1} ), where ( zeta ) is the damping ratio. The UniformOutput parameter is set to false to allow the output to be a cell array, which is necessary for storing multiple transfer functions.
Compute Feedback
The next step involves computing the feedback for each transfer function using the cellfun function, which applies a function to each cell in a cell array.
Yarray = cellfun(@(G) feedback(G, 1), Garray, 'UniformOutput', false);
This line computes the closed-loop transfer function for each open-loop transfer function in Garray with unity feedback.
Store Sampling Grid Information
The SamplingGrid structure is created to store the damping ratios, which can be useful for referencing the parameters later.
SamplingGrid.zeta = dr;
Plotting the Step Response
The plotting section begins by creating a new figure and setting it to be visible. The hold on command allows multiple plots to be displayed on the same graph.
figure; set(gcf,'Visible','on'); hold on; % Hold on to plot multiple lines
The step response for each transfer function in Yarray is plotted in a loop:
for i = 1:numTransferFunctions step(Yarray{i}, 60); % Plot step response for each transfer function end
Dynamic Legend Creation
To create a dynamic legend that reflects the damping ratios, the code uses arrayfun to generate labels based on the values in dr.
legendLabels = arrayfun(@(zeta) sprintf('\\zeta = %.1f', zeta), dr, 'UniformOutput', false); legend(legendLabels, 'Location', 'best'); % Apply legend with dynamic labels
For more information on arrayfun, please refer to
https://www.mathworks.com/help/matlab/ref/arrayfun.html
In this section:
- sprintf is used to format the labels, ensuring that each damping ratio is displayed with one decimal place.
- The legend function is then called with the dynamically generated labels, which will automatically adjust the colors to match the corresponding step response lines.
Finalizing the Plot
Finally, the hold off command is executed to release the hold on the current figure, allowing for new plots to be created in future commands without overlapping.
hold off; % Release hold after plotting
This refined approach successfully generated a plot with a dynamic legend reflecting your specified parameters. Please see attached.
If you encounter any specific errors or unexpected behavior during execution, please provide those details for further assistance!