Piecewise function as condition in another piecewise function
Afficher commentaires plus anciens
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
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Assumptions dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!