d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Matlab Function Handle Piecewise
Add Reply New Topic New Poll
Member
Posts: 769
Joined: Apr 1 2016
Gold: 0.00
Apr 17 2016 08:10pm
I need to take the convolution with a function f and g, where g is a piecewise defined function.
Could anyone direct me how to define a piece-wise function handle in MATLAB?
i.e.

g(x) = { x ^2 : x>0 ; 0 : x<0}

Pretty elementary stuff I'd assume but it's something I've never done before.
Thanks.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Member
Posts: 769
Joined: Apr 1 2016
Gold: 0.00
Apr 17 2016 08:50pm
Quote (carteblanche @ Apr 17 2016 10:35pm)


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}

Link 2 wasn't too helpful as I'm sure it doesn't need to be that complicated.

This post was edited by SoSSoS on Apr 17 2016 08:51pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 17 2016 09:03pm
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
Member
Posts: 769
Joined: Apr 1 2016
Gold: 0.00
Apr 21 2016 07:36pm
Quote (carteblanche @ Apr 17 2016 11:03pm)
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


I didn't need up ending this but thank you
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll