Use of Matlab Function in State Space Modeling gives Dimension Errors - S-Function needed?

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

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
Hello darova, thanks for replying. Indeed the parameters dont change during the simulation. However as there are many identical (function) blocks chained to another I want to keep the functions arguments at an absolute minimum, thus excluding the parameters. Like this:
function xp = fcn(x,u)
*NOTHING*
xp = parameter1 * x + parameter2 * u^2; %for example
But apparently the parameters can not be initialized separately in a script and then used from the workspace but have to be initialized inside the function body. Why is that? I have also tried loading them inside the function from a .mat-file and declaring them as global. It didn´t help.
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
Thank you! Even though that is not the solution I had wished for (without having to initialize another list of parameters with par1 = ... par2 = ...), it sounds like a good workaround to centralize my parameter list via the .mat file. I will try it tomorrow. :) Thanks so much and have a good evening!
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

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Simulink Coder finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2019a

Gefragt:

am 19 Nov. 2019

Bearbeitet:

am 8 Apr. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by