Not the most difficult thing to do by any means. But here’s a handy conversion from a math formula to matlab. Say you have the piecewise polynomial, *m*, defined as:

```
/ 0 if x < 0,
m(x) = | -2x²(x - 3/2) if 0 ≤ x < 1,
| 1 + (x - 1) if 1 ≤ x < 3/2,
\ x - 1/4 if x ≥ 3/2.
```

So in matlab if I have some variable *x* with some values, i.e.

`x = -1:0.01:2;`

then I can formulate the above as:

```
m = ...
(0 ) .* (x < 0 ) + ...
(-2*x.^2.*(x-3/2)) .* (0 <= x & x < 1 ) + ...
(1+(x-1).^2 ) .* (1 <= x & x < 3/2) + ...
(x-1/4 ) .* (x >= 3/2 );
```

Now, I can show a plot:

```
plot(x,m);
```

This works because the conditions in matlab are now logicals that return a vector the same size as *x*, with 1’s if the condition was true and 0’s otherwise. Multiplied against the value for the condition and added to the next gives the correct solution. I guess this method is somewhat risky in the sense that if you mess up your logicals or inequalities the addition could sum up erroneous values without recognizing the error. But I think the presentation is very nice and is easily broken up if need be.

**Note:** The polynomial above is from Higher Order Barycentric Coordinates by Torsten Langer and Hans-Peter Seidel.

Tags: math, matlab, plot, polynomial

Very nice example, thank you for sharing.

you are a fucking genius. Thank you so much. I think this will work.

Alec, would you happen to know how to sum an iterated piecewise function? Say f(x) = x+x^2 and you need to sum on an interval [a,b] the iteration of f as long as x_i is in [a,b] else the sum is zero. Thank you in adavance.

I’m not sure what you mean. Do you mean something like this:

Otherwise what do you mean by “sum an iterated piecewise function” can you explain how you would write this function with math notation?

it doesn’t work

but i used if/else conditions instead

@Tom Which doesn’t work?!?!

This didn’t work for my piecewise function

@mac What function are you trying to do?

THANK YOU SO MUCH!!!!

Many thanks , it’s very simple and useful

using your same function m(x) say now i want to plot m(-x+1) how would i do so?

m’s only a function of x, right before it’s evaluated. So to do what you want you could try:

Just what I was looking for…

Many thanks.

Awfully useful, thank you very much.

sir, thank you you are a genius

Hi!

Please, how to calculate de integral of this function on interval [-1,2] ?

Andre, remember that you can just calculate the sum of the integrals of each subfunction on its respective interval. A more detailed explanation.

Dear Ajx,

Thank you for your answer, but I really would like to know if there is a way to make it directly, that is, once we have the expression, is there a command that make the calculation whitout I have to command interval by interval the respective expression?

Sorry for my english … my corrent language is portuguese.

I’m not sure exactly what you want to do. You could try MATLAB’s symlink, or maple or mathematic to compute the integral automatically. All of them should be able to easily handle piecewise functions.

thanks alot deaR….exactly what I was googling for

How would you do a multi-variable piecewise function?

F(x,y)=

0 when x<0 y<0

x*y 0<=x<=1 0<=y<=1

x 0<=x1

y 0<=y1

1 x>1 y>1

I tried the method given but it adds up the values

z= (0) .*(x<0 | y<0)+…

(x.*y) .*(0<=x<=1 | 0<=y<=1)+…

(x) .*(0<=x1)+…

(y) .*(0<=y1)+…

(1) .*(x>1 | y>1);

any help would be great, thanks

Hi Frank,

I’m not sure I understand. How many variables are there? Just x and y? What are x1 and y1? Also does “x<0 y<0” mean x less than zero OR y less than zero or does it mean x less than zero AND y less than zero? I have a feeling the component-wise ‘or’ operator ‘|’ is the culprit.

Sweeeeet

/ 0 if x<0 and y<0

| x*y if 0<=x<=1 and 0<=y<=1

F(x,y)= | x if 0<=x1

| y if 0<=y1

\ 1 if x>1 and y>1

Sorry about that, I must have typed my previous comment half asleep when I was frustrated trying to figure out how to use your method to plot the function. The one I wrote is the correct function, I also changed the “|” to “&”. eventually I plotted the function, though it required a bit of loops. it would be great if there was a way to do it your way. There are 2 variables, x and y.

Hi ajx,

I highly appreciate the fact that you took your time to write such a nice program for graphing piecewise functions. I have tried your code to some other similar functions like the one you have, and it works very well, except for the one that I have to do regarding my project.

I am working on an estimation problem in wish I need to generate a plot of the following function ( a cubic spline K0 = -3):

B0(x) =

0.25*y.^3 if k0 ≤ x < k1,

-0.75*y.^3+0.75*X.^2+0.75*y+0.75 if k1 ≤ x < k2,

0.75*y.^3 – 0.75*y.^2 +1 if k2 ≤ x < k3,

-0.25*y.^3+0.75*y.^2-0.75*y+0.75 if k3 ≤ x < k4,

where x is generated from

N =100;

x = rand(1,N); and decomposed into an interval kx given:

kx = floor(K*x) and

y = K*x-kx

Since B0 has 4 intervals, K = 4.

The peremeter to be estimated is a sum given by:

g = aK0*BK0(x) +aK0+1*BK0+1(x) +…aK-1BK-1(x)

where the other B-splines are translations of B0(x) given by:

BJ(x) = B0(x-kj). for j = K0, K0+1,….K-1, I also would like to plot these translated version of B0(x). K0 = -3 for cubic splines, and is not the same as for the intervals for B0, k0.

I would really be glad if you can help me.

Hi,

does anybody know how to handle relational operation in symbolic computation. For eg. if I want to check certain condition, if a>=b else NaN.

does anybody have idea ingerneal about looping in symobilc computation..

thanks.

This was a Great Trick. hatsoff

Awesome!

Could have read documentation. But thanks to google and you I got to the stuff i needed without reading boring definitions of this and that!

This is a nice trick, but it has a serious flaw that comes up regularly when I try to use. Consider this example:

f=@(x)(x>0).*(1./x)+(x<=0).*x;

When x==0, the function "should" return 0, but it returns NaN. Is there a real way to deal with piecewise functions in matlab?

@Jake,

I’ve run into this too. Usually I give up on having a one-liner in these situations, but it seems you can taylor-make something for your situation. There are a few options here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/147044. I like this one for example:

Absolute genius, worked like a charm. Thanks for your help!

THANK YOU!!!! I’ve been trying to figure out how to do this!!

Hey just curious, I tried using trig such as

ff = …

(sin(x)) .* (0 <= x <= 10) + …

(2) .* (10 <= x);

plot(x,ff)

When I use trig it seems to behave differently, any help?

Actually you can delete it haha, I see my mistake. Thanks for the code!

I want to use truncated ramp function defined as below:

f(t)=t for t=[0:1:100];

f(t)=1 for t=[100:1:6000];

I want to use this function in the form such that I can use f(t+3) + f(t-2) etc.

How should I define this function?

@Preet, aside from the fact that your f is defined twice for 100 (does f(100) equal 100 or 1?) then you would use f = @(t) (t<100).*t + (t>=100).*1;

I know this is out of date, but the way I deal with singularities is to make a second variable then substitute that in. It is two lines, but it allows anonymous functions to still be used->

Not sure where my dot-times went, tho

@Michael, nice. The asterisks got lost during the markdown parsing. I fixed your comment accordingly.

Thanks so musch

it worked!

best

Hi, I can’t define following function, why?

x=-3*pi:0.3:pi;

m = …

(0 ) .* (x <=0 ) + …

(exp(-1/(x.^2)) ) .* (0 < x );

plot(x,m)

I think one of your ads caused my internet browser to resize, you might want to put that on your blacklist.

Hi! Quick question that’s entirely off topic. Do you know how to make your site mobile friendly? My blog looks weird when viewing from my iphone4. I’m trying to find a template or plugin that might be able to fix this problem. If you have any suggestions, please share. Cheers!

I think one of your advertisings triggered my web browser to resize, you may well want to put that on your blacklist.

Do you mind if I quote a few of your posts as long as I provide credit and sources back to your site? My website is in the exact same area of interest as yours and my visitors would truly benefit from some of the information you present here. Please let me know if this ok with you. Thanks!

At this time it appears like Drupal is the top blogging platform available right now. (from what I’ve read) Is that what you are using on your blog?