Evaulate Piecewise MATLAB function
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Rupamathi Jaddivada
il 31 Dic 2016
Commentato: Rupamathi Jaddivada
il 2 Gen 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 Commenti
Risposta accettata
Walter Roberson
il 31 Dic 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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Assumptions in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!