Use of Matlab Function in State Space Modeling gives Dimension Errors - S-Function needed?
Ältere Kommentare anzeigen
Dear Community,
I am modeling a heat transfer problem using a state space model. For that I used the Matlab Function block, which looks approximately like this:
function [x1p, x2p, x3p] = fcn(x1, x2, x3, u1, u2, u3, u4, u5, u6, u7, u8, u11, u12)
parameter1 = ...;
parameter2 = ...;
parameter3 = ...;
etc.
[...]
x1p = ...; %ode1
x2p = ...; %ode2
x3p = ...; %ode3
The list of parameters is quite long. What I want to do is exclude them from the matlab function and initialize them once via the accompanying script. Apparently because of the loop that is necessary (see the attached picture), I get dimension errors, when I do delete the constant parameters in the matlab function and initialize them from my script.

What do I need? Is it an S-function, or a Level-2 Matlab S function? I have zero experience with those, but I found via the search that the Matlab function treats its parameters as "persistent variables", in other words local to the function, if I understand correctly.
Can you give me any advice on how to treat this problem?
Thanks in advance
Felix
Edit: Error message looks like this:
Simulation
1
Clear
05:16 PM Elapsed: 1 sec
Compilation of model 'test' failed while trying to resolve underspecified signal dimensions.
Suggested Actions
Enable 'warning' or 'error' diagnostics for the list of underspecified signal dimensions.Open
Caused by:
- Error in default port dimensions function of S-function 'test_sfunc/Subsystem/Matlab Function'. This function does not fully set the dimensions of output port 2
6 Kommentare
Felix Schönig
am 8 Apr. 2020
darova
am 8 Apr. 2020
I have no knowledge about Simulink. This might be usefull
function xp = fcn(x,u) %x1, x2, x3, u1, u2, u3, u4, u5, u6, u7, u8, u11, u12)
% a = u(1);
% b = u(2);
% ...
parameter1 = ...;
parameter2 = ...;
parameter3 = ...;
etc.
[...]
xp = zeros(3,1);
xp(1) = ...; %ode1
xp(2) = ...; %ode2
xp(3) = ...; %ode3
Usually if you have something to do with ode solvers. The function header looks like this
function xp = fcn(t,x,parameters) % time, x variable, parameters
- What I want to do is exclude them from the matlab function and initialize them once via the accompanying script
If your parameters don't change during simulation you don't need persistent
Felix Schönig
am 8 Apr. 2020
darova
am 8 Apr. 2020
If you have .mat file with parameters. Try this
function xp = fcn(x,u)
S = load('parameters.mat'); % load parameters into structure
par1 = S.par1;
par2 = S.par2;
% ...
Wher you are using dynamic command
load parameters.mat
It tries to load it to yourmain workspace. But doesn't do this into function workspace
Felix Schönig
am 8 Apr. 2020
There is one more way (but is not recommended): using global variables. See of one of topics: LINK
somewhere in main script
global a b c
a = 2;
b = 3;
c = 4;
then to use them in a function
function dx = f(t,x)
global a % don't forget declaring
dx(1) = a*x(1);
end
Antworten (0)
Kategorien
Mehr zu Simulink Coder finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!