COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### How defining discontinuous function

• Email
• 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

See related articles to this posting

```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