# How can I programatically change the transfer function of an LTI block in Simulink and then linearize the system?

5 views (last 30 days)
Felix Crazzolara on 29 Apr 2020
Answered: Paul on 30 Apr 2020
Please consider the code shown below. My goal is to change transfer functions for LTI system blocks and obtain linearizations of the resulting system. Unfortunately I get the the message shown below. What is the issue here? How can apply the changes programatically?
Error using dlinmod (line 198)
'p2b/P0' has unapplied changes. Please apply or cancel these changes before running
the simulation
Error in linmod (line 59)
[varargout{1:max(1,nargout)}] = dlinmod(model, Ts, varargin{:}, Args);
Error in p2bs (line 37)
[am, bm, cm, dm] = linmod('p2b'); - Show complete stack trace
% Open the model
open_system('p2b');
% Prepare variables
s = tf('s');
% Define a function handle to generate the transfer function of the plant
P0fun = @(a) (s-a)/(s+1)^2;
% Define a function handle to generate the tranfer function of the filter
Filterfun = @(wc) 10*wc^2/(wc^2 + sqrt(2)*wc*s + s^2);
% Create a vector of values for a
aVec = linspace(1,10,(10-1)/0.01);
% Compute the closed-loop bandwidth for the different values of a
for i = 1:length(aVec)
% Extract the value of
a = aVec(i);
% Generate the plant
P0 = P0fun(a);
% Assign the value to the Simulink block
set_param('p2b/P0','sys',tfToString(P0));
% Define the 2nd order Butterworth filter
filter = Filterfun(a);
% Assign the the value to the Simulink block
set_param('p2b/Butterworth','sys',tfToString(filter));
% Extract the extended plant model
[am, bm, cm, dm] = linmod('p2b');
end
function str = tfToString(tf)
str = 'tf([';
for i = 1:length(tf.numerator{1})
str = [str num2str(tf.numerator{1}(i))];
if i ~= length(tf.numerator{1})-1
str = [str ','];
end
end
str = [str '],['];
for i = length(tf.denominator{1}(i))
str = [str num2str(tf.denominator{1}(i))];
if i ~= length(tf.denominator{1})-1
str = [str ','];
end
end
str = [str '])'];
end

Paul on 30 Apr 2020
Not sure why you're getting that error message, but it seems you can use a simple approach altogether.
Just set the 'LTI system variable' parameter of the P0 block to P0 and set that parameter in the Butterworth block to filter. Then linmod will always be using the values of P0 and filter that are in the workspace. At least as you described it here there should be no need to go through the tfToString and set_param commands.

R2019b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!