Quote (SoSSoS @ Apr 17 2016 10:50pm)
I didn't understand the code in the first link about 0 and 1 logicals. Think you could explain what's going on here...and how I would use his example to code a piecewise function of the form:
g(x) = {A*exp(-x) if x>0 ; 0 if x<0}
from context, looks like "false" evaluates to 0 and "true" evaluates to 1. a quick google search confirmed it (http://www.mathworks.com/help/matlab/ref/false.html).
we can take advantage of that to strategically define your function to include all cases in such a way the "false" values of 0 will remove the cases you don't want, whereas the "true" cases will keep the cases you do want. so you multiply your constraint with its corresponding expression and add them all together
we'll look at your first example:
if x>0 => x^2
if x<0 => 0
step 1: multiply the terms together:
(x>0) * (x^2)
(x<0) * (0)
Step 2: add them together:
(x>0)*x^2 +
(x<0) * 0
looking at sample value, say x = -1:
(false)*x^2 +
(true) * 0
=
(0)*x^2 +
(1) * 0
=
(1) * 0
= 0
looking at x=1:
(true)*x^2 +
(false) * 0
=
(1)*x^2 +
(0) * 0
=
(1)*x^2
=x^2
which is what you wanted
This post was edited by carteblanche on Apr 17 2016 09:09pm