Evaulate Piecewise MATLAB function
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rupamathi Jaddivada
el 31 de Dic. de 2016
Comentada: Rupamathi Jaddivada
el 2 de En. de 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 comentarios
Respuesta aceptada
Walter Roberson
el 31 de Dic. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Assumptions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!