Clear Filters
Clear Filters

Error using matlabfunction with piecewise functions and intervals

8 views (last 30 days)
I am working with the symbolic toolbox and I am trying to generate a matlab function using the command matlabfunction() with gg as an input, where gg is a vector of piecewise functions as follows:
gg =
piecewise(x8 in Dom::Interval(-10, 10), x8, 10 <= x8, 10, x8 <= -10, -10)
x5
piecewise(10 < x8 | x8 < -10, 1, x8 in Dom::Interval([-10], [10]), 0)
piecewise(10 < x8 | x8 < -10, 1, x8 in Dom::Interval([-10], [10]), 0)
piecewise(80 <= t, 1, symtrue, 0)
Where t, x5, and x8 are symbolic variables.
When using the matlabfunction() command, as follows:
g = matlabFunction(gg,'Vars',{xNum,t},'File','genEqs/g');
I get the following errors and warnings:
Warning: Intervals ('DOM_INTERVAL' and 'Dom::Interval') not supported. Using element '0' instead.
Error using symengine
Unable to generate code for object 'x8 in t4'.
Error in sym/matlabFunction>mup2mat (line 404)
res = mupadmex('symobj::generateMATLAB',r.s,ano,spa,0);
Error in sym/matlabFunction>writeBody (line 516)
body = mup2mat(eqnk,false,sparseMat);
Error in sym/matlabFunction>writeMATLAB (line 452)
writeBody(fid,tvalues,sparseMat);
Error in sym/matlabFunction (line 183)
g = writeMATLAB(funs,file,varnames,outputs,body, opts.Optimize, opts.Sparse, opts.Comments);
Error in antiwindup_RUN_rrrr (line 241)
g = matlabFunction(gg,'Vars',{xNum,t},'File','genEqs/g');
However, if I use the following vector of piecewise functions, then the code works perfectly fine.
gg =
piecewise(x8 in Dom::Interval(-10, 10), x8, 10 <= x8, 10, x8 <= -10, -10)
x5
piecewise(10 < x8 | x8 < -10, 1, x8 in Dom::Interval([-10], [10]), 0)
piecewise(10.1 < x8 | x8 < -10.1, 1, x8 in Dom::Interval([-10.1], [10.1]), 0)
piecewise(80 <= t, 1, symtrue, 0)
What would be the reason of this? I can't figure it out. Is there a way to bypass this error?
Thank you in advance.
Nicolas

Accepted Answer

Uday Pradhan
Uday Pradhan on 25 May 2021
Hi Nicolas,
Referring to the documentation for matlabFunction, one can see that there exists an argument called "Optimize". By default, this is set as true. To bypass the above error, you will need to set this flag as 0, as such:
syms x5 x8 t;
vec = [ piecewise(-10 < x8 < 10, x8, 10 <= x8, 10, x8 <= -10, -10),...
x5, piecewise(10 < x8 | x8 < -10, 1, -10 <= x8 <= 10, 0),...
piecewise(10 < x8 | x8 < -10, 1, -10 <= x8 <= 10, 0),...
piecewise(80 <= t, 1 ,symtrue, 0)]
g = matlabFunction(vec,'Vars',{x5,x8,t},'File','g1','Optimize',0);
Note: The above error occurs when matlabFunction tries to optimize the generated code in g1.m. Whenever we include two functions in the vector which have the same domain of definition with finite endpoints (intervals of type (a,b),(a,b],[a,b] or [a,b)), the above error will be generated if Optimize flag is turned on.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!