Evaulate Piecewise MATLAB function
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Rupamathi Jaddivada
le 31 Déc 2016
Commenté : Rupamathi Jaddivada
le 2 Jan 2017
Hi, I have a function with a set of equations which needs to be solved for unknowns x. However, this function consists of piecewise function of matlab. The code looks something like
function dx = myFunction(x)
a1 = x(1);
a2 = x(2);
c = piecewise(a2>0,a1^2,0)
dx(1) = a1*a2 + a2*c + a1*c;
dx(2) = a2*c;
end
When I try to use fsolve on this function, MATLAB gives an error message saying the following: Undefined function 'piecewise' for input arguments of type 'logical'
I was wondering if there is a way I can make MATLAB evaluate the piecewise expression for each iteration of fsolve.
Thanks, Rupa
0 commentaires
Réponse acceptée
Walter Roberson
le 31 Déc 2016
piecewise() is part of the Symbolic Toolbox. It does not accept logical values as its first argument, and does not even accept sym() of a logical value as its first argument (those are converted to numeric values.)
It does accept MuPAD's TRUE or FALSE as its first argument, so if it is important to you to use piecewise() instead of one of the alternatives, then you could use
FALSETRUE = [sym('FALSE'), sym('TRUE')];
c = piecewise( FALSETRUE((a2>0)+1), a1^2, 0);
but really you would be better off using just an 'if' or at most logical indexing
c = 0;
if a2 > 0
c = a1.^2;
end
or
c = (a2 > 0) .* (a1.^2); %caution: this is not valid if a1 can be inf
9 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Assumptions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!