Piecewise function as condition in another piecewise function
8 views (last 30 days)
Show older comments
Cristina Magnetti Gisolo
on 28 Nov 2020
Commented: Cristina Magnetti Gisolo
on 29 Nov 2020
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
0 Comments
Accepted Answer
Walter Roberson
on 28 Nov 2020
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 Comments
More Answers (0)
See Also
Categories
Find more on Assumptions 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!