Piecewise function as condition in another piecewise function

조회 수: 8 (최근 30일)
Hello everyone,
I am using Symbolic Math Toolbox in order to define a matlabFunction to be used in Simulink.
I am having problem when evaluating the code that follows:
x = sym('x',[N 1]);
L = 100;
z(1,1) = x(2) - x(1); % delta x(N)
z(1,1) = piecewise(z(1)<0,L+z(1),z(1)>=0,z(1));
V(1,1) = piecewise(z(1)<=3,0,3<z(1)<5,z(1),10);
matlabFunction(V,'File','FUNCTION','Vars',{z});
The original code is more complex, but a simplified version is this one.
I get the following error message:
Error using symengine
Unable to evaluate to Boolean.
Error in sym/mupadmexnout (line 1057)
out = mupadmex(fcn,args{:});
Error in sym/matlabFunction>optimize (line 468)
[tvalues,f,tnames] = mupadmexnout('symobj::optimizeWithIntermediates',f{:});
Error in sym/matlabFunction>writeMATLAB (line 443)
[f,tvalues,tnames] = optimize(f,optim);
Error in sym/matlabFunction (line 183)
g = writeMATLAB(funs,file,varnames,outputs,body, opts.Optimize, opts.Sparse, opts.Comments);
Error in main (line 94)
matlabFunction(V,'File','FUNCTION','Vars',{z});
Where (line 94) refers to the line where I call the matlabFunction.
I'm not sure, but I tried to modify something and I think that what gives problems is that in the 2nd piecewise function I use the evaluation of the output of another piecewise function, because in the 2nd piecewise function I use z(1) that comes out from the 1st piecewise.
Do you think it may be the problem? Because if I exchange the order ot the two piecewise functions it works. Unfortunately I want to evaluate z(1) and then modify it before evaluating V(1).
z(1) is a symbolic variable, but maybe it doesn't like it because from the 1st piecewise z(1) is a piecewise function and it is not good to use it to evaluate a condition in another piecewise.
I was trying to use an alternative to the 2nd piecewise, that implements a saturation function, but I don't know if there's another way, maybe smarter. What do you think?
Thank you in advance

채택된 답변

Walter Roberson
Walter Roberson 2020년 11월 28일
matlabFunction(V,'File','FUNCTION','Vars',{x});
worked for me. You cannot use z as your Vars in matlabFunction because you need to list the names of the independent variables.
Caution: in some recent releases, the optimization done by default in matlabFunction writing to a file is broken. Until that is known to be fixed I recommend turning off optimization:
matlabFunction(V,'File','FUNCTION','Vars',{x}, 'optimize', 0);
Note: when you use matlabFunction on an expression that includes piecewise() then the resulting code is not vectorized in any variable appearing in the piecewise() conditional tests.
  댓글 수: 5
Cristina Magnetti Gisolo
Cristina Magnetti Gisolo 2020년 11월 29일
Thank you, you're very kind.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by