Unexpected Perturbation of Constant Inputs During Full-System Linearization
49 views (last 30 days)
Show older comments
I am working with a fully functional, complex dynamic system model in Simulink, structured into three main subsystems, each of which contains many nested subsystems. My objective is to linearize the entire system, so I define input and output analysis points at the root level in order to obtain a full-system linear model.
However, I am encountering unexpected behavior during linearization related to constant signals used within the subsystems.
In particular, some of the internal subsystems contain MATLAB Function blocks that receive constant values as inputs. These constants are used inside the functions to implement simple switching logic depending on the current configuration. During standard simulation, this setup works correctly. However, during linearization — even though these constants are not exposed as root-level inputs — they appear to be perturbed by the linearization process. This causes the switching logic to fail and results in errors within the MATLAB Function blocks.
A similar issue occurs in masked subsystems connected to a library. Some constants are defined as mask parameters and passed as inputs to MATLAB Function blocks inside the masked subsystem. These constants also seem to be perturbed during linearization, which again leads to incorrect behavior inside the functions.
To further investigate, I tried linearizing one of the top-level subsystems independently (still defining input and output points at the root level), and the same problem occurred. I also extracted one of the problematic internal subsystems and inserted it into a very simple test model, and the issue persisted there as well — confirming that the behavior is not specific to the complexity of the full model.
I would appreciate clarification on how the linearization engine handles such constant signals, especially when they are not explicitly set as tunable parameters or external inputs. Is there a recommended way to prevent these constants from being perturbed during the linearization process?
Thank you in advance for your support.
0 Comments
Answers (4)
Paul
on 28 Jul 2025
Hi Gabriele,
I wasn't able to create a simple model to replicate the problem, so I can't say for sure that what follows is 100% accurate.
Assuming you're using the default, "blockbyblock", linearization, the linearizer will try to linearize the MATLAB Function block by perturbing the inputs to the block and computing the resulting perturbation to the output. At this point, the linearizer doesn't know that an input is connected to a Constant block. So it goes ahead and perturbs the associated input to the block and proceeds, which is basically what you've stated.
Options for solution (there may be others?):
Instead of using a Constant block as input to the Matlab Function, make the constant a parameter to the function so it's not an input. Use Data in Multiple MATLAB Function Blocks by Defining Parameter Variables
Try setting the perturbation level to zero for the input that comes from Constant block: Change Perturbation Level of Blocks Perturbed During Linearization. I confess that I don't fully understand how this works, but it worked for me in a simple case.
If you know what the linearization should be for the Matlab Function block, you can specify it directly: Specify Individual Block Linearization
The nuclear option would be to change the linearization method from blockbyblock to one of the numerical perturbation options (which should be applicable in this case because you're linearizing from root inputs to root outputs): linearizeOptions
7 Comments
Paul
on 1 Aug 2025
Edited: Paul
on 1 Aug 2025
If the output of the Constant block must be double for some other reason, then you can add a Data Type Conversion block between the Constant and the Matlab Function so that it sees an int, but all other users of the Constant block see a double.
bdclose('all');
hsys = new_system('lintest');
hconstant = add_block('simulink/Sources/Constant','lintest/Constant');
hdtc = add_block('simulink/Signal Attributes/Data Type Conversion','lintest/dtc');
set_param(hdtc,'OutDataTypeStr','int32');
hinput = add_block('simulink/Sources/In1','lintest/u');
hfunc = add_block('simulink/User-Defined Functions/MATLAB Function','lintest/func');
hout = add_block('simulink/Sinks/Out1','lintest/y');
mfc = get_param(hfunc,'MatlabFunctionConfiguration');
functionscript = [
"function y = fcn(u1,u2)";
"u2";
"if u2 == 1";
"y = 10*u1;";
"else";
"y = 100*u1;";
"end";
"end"];
mfc.FunctionScript = join(functionscript,newline);
add_line(hsys,get_param(hinput,'PortHandles').Outport, ...
get_param(hfunc,'PortHandles').Inport(1));
add_line(hsys,get_param(hconstant,'PortHandles').Outport,...
get_param(hdtc,'PortHandles').Inport);
add_line(hsys,get_param(hdtc,'PortHandles').Outport,...
get_param(hfunc,'PortHandles').Inport(2));
add_line(hsys,get_param(hfunc,'PortHandles').Outport,...
get_param(hout,'PortHandles').Inport);
% command to open the model on the canvas
% open_system('lintest');
format long e
format compact
set_param(get_param('lintest/func','PortHandles').Inport(2),'PerturbationForJacobian','1e-5');
sys = linearize('lintest',[linio('lintest/u',1,'input'),linio('lintest/func',1,'output')])
"I have some project where this is clear but I think that using a simulink model and the model linearizer is a little different but the behaviours seems to be the same how can we discuss on that?"
I'm afraid I don't understand this question. Is it related to the present discussion? If not, consider opening up a new Question.
Paul
on 13 Aug 2025 at 19:45
Tech Support confirmed a bug when using that parameter with a Matlab Function block.
Gabriele
on 11 Aug 2025 at 13:56
1 Comment
Paul
on 12 Aug 2025 at 20:20
Seems like the configuration function with the BlockData input should allow you to implement whatever you want, but I haven't actually tried anything. Presumably if the independent variable is not outside the breakpoints then the function would resturn the slope of the data, and return 0 otherwise (though I guess you'd have to make a decision about the case when the input is exactly on a breakpoint).
See Also
Categories
Find more on Schedule Model Components in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!