How defining discontinuous function

  • Follow


I 'd like know how I can define a discontinuous function (such as f(x)=5 if x>5, f(x)=6x if x<5) and how I can evaluate it in a interval (such as [0 6]).
0
Reply Stefanos 9/19/2010 8:06:03 PM

On 19/09/10 3:06 PM, Stefanos Teliso wrote:
> I 'd like know how I can define a discontinuous function (such as f(x)=5
> if x>5, f(x)=6x if x<5) and how I can evaluate it in a interval (such as
> [0 6]).

function y = f(x)
   y = nan(size(x));
   y(x>5) = 5;
   y(x < 5) = 6 * x(x < 5);
end


Note: using zeros instead of nan is more efficient, but using nan has 
the advantage of making clear the problem you face when x is exactly 5.

If you do not mind using 0 for the undefined values, you could also use

f = @(x) (x>5) .* 5 + (x<5) .* 6 .* x;

This form can be used for all cases in which the formulas are 
well-behaved at all points (i.e., not nan and not infinite) including 
the points the sub-formulae are not intended to be applied to.


Note: if you intend to optimize or integrate over this function, then 
you have to be very careful about how you do that. Most of the 
optimizers assume continuous first derivatives at the very least.
0
Reply Walter 9/19/2010 8:15:10 PM


1 Replies
599 Views

(page loaded in 0.022 seconds)

Similiar Articles:













7/20/2012 2:32:55 PM


Reply: