Items tagged with piecewise



I have some matrix valued functions that that I can only define piecewise. Unfortunately I have some problems to manipulate those functions. Piecewise sees to do the job for  scalar valued functions. There I can easily differentiale and add results. For Matrix valued functions however, those features seem to not be availible.

I can work around these limitations by manually applying all operations to the operands of the piecewise function. Like I show here:



# a piecewise function


p1 := piecewise(a(t)^2 = 0, Vector(2, {(1) = cos(a(t))^2+sin(a(t))^2, (2) = 0}), Vector(2, {(1) = 1, (2) = 1/a(t)}))


# differentiation does not work


diff(piecewise(a(t)^2 = 0, Vector(2, {(1) = cos(a(t))^2+sin(a(t))^2, (2) = 0}), Vector(2, {(1) = 1, (2) = 1/a(t)})), t)


# selecting individual entries does not work


r := (piecewise(a(t)^2 = 0, Vector(2, {(1) = cos(a(t))^2+sin(a(t))^2, (2) = 0}), Vector(2, {(1) = 1, (2) = 1/a(t)})))(1)+1


# strange simplification behaviour


piecewise(t = RootOf(a(_Z)), _z1(RootOf(a(_Z))), Vector(2, {(1) = 1, (2) = 1/a(t)}))


# current workaround, define new piecewise function by using op


r := piecewise(a(t)^2 = 0, cos(a(t))^2+sin(a(t))^2+1, 2)






I think my workaround is very cumbersome and error prone. Is there a better way to tackle this probem? Or should I try to overload all needed operations, and is this even possible?

Thanks for all suggestions!


I am learning pdsolve in Maple. When I try to solve a diffusion pde, I get this strange error message, and I do not understand what it means: This is using Maple 2017 on windows 7


The error is

Error, (in casesplit/K) this version of casesplit is not yet handling the function: piecewise

Am I writing the initial conditions (ic) wrong?  Maple help shows nothing on this. I think Maple does not like my initial conditions. But do not know how to correct it now.

What causes this error?



Dear community, 

I'm new to maple and was wondering if you could help me out.

I have this curve where I want to make a line that goes from x=0.5 up to its value on the curve in this case 1.60 and then all the way to the y-axis so there is an area under the curve which I can color if that's even possible?

I have the following in maple:

k := 2.5;
Ca0 := 1;
v := 20;
Ca := Ca0*(1-x);
                             1 - x
Fa0 := Ca0*v;
Cb := Ca0*x;
ra := k*Ca*Cb;
                         2.5 (1 - x) x
plot(1/ra, x = 0 .. 1);

thank you for your help

Best Regards


I have a piecewise that exists on a large intervals, say [-5..15], however, I know need the function to represent [1..9].

There are two approaches I know of: using convert(f,pwlist) or using op(n,f), see attachments.    and  PiecewiseTruncate.pdf

However, these two methods are not elegant enough, I have to reassemble the pieces in the new function.

How to make the solution neat?


I want to plot a simple power function in diferent pieces by using diferent range and  for different values of parameter a[i] at i=0,1,2 define in the attached sheet.

I wonder if there is a way to represent each piece of a piecewise function in Maple.

For example, for a piecewise function: f:=x->piecewise(1<=x<=2,c[1]*x+c[2],2<=x<=3,c[3]*x+c[4],3<=x<=4,c[5]*x+c[6]);

I want to have a way to retrieve each piece, e.g.: c[3]*x+c[4], but f(x)[2] doesn't work.

Is it archievable?



I tryed this, because i thought i might be abel to shade this new piecewise function later on, but i dont know how to tell maple that there is 2 y-axes values in the interval from (2;3):

so it failed. 

pleace help regards Niklas.



 I need help regarding this figure. i want to write both equations in maple 18. Second equation is related to first equation.So, please help me to write this code. I will be grateful. 



The piecewise plot below displays a sphere truncated by the plane z = 2 - y.

f := proc (x, y, z) options operator, arrow; piecewise(z <= 2-y, x^2+y^2+z^2-16, z-2+y) end proc; implicitplot3d(f, -4 .. 4, -4 .. 4, -4 .. 4, style = surface, numpoints = 50000);

The 3 transforms below when executed in display(T(sphere([0, 0, 0], 4, numpoints = 50000)), scaling = constrained) display the truncated sphere differently:

1) the truncating plane only partly conforms to the boundary of the truncated sphere

2) the truncated sphere is correct provided that the else condition coordinate is in the truncating plane and truncated sphere

3) the truncated sphere is correct but hollow


1) T := transform(proc (x, y, z) options operator, arrow; `if`(z <= 2-y, [x, y, z], [x, y, 2-y]) end proc)

2) T := transform(proc (x, y, z) options operator, arrow; `if`(z <= 2-y, [x, y, z], [0, 0, 2]) end proc)

3) T := transform(proc (x, y, z) options operator, arrow; `if`(z <= 2-y, [x, y, z], [`&+-`(sqrt(16-y^2-z^2)), y, 2-y]) end proc)

Please explain the different behavior of the three transforms.           

Good morning Maple Expert,


I would like to explain the concept of piecewise curves in 2D & 3D to my students with plots.In this regard I request the associated expetrs in Maple to provide the appropriate Maple commands to get piecewise curves in 3D space.


With warm regards.



Associate Professor in Mathematics.

Hyderabad Institute of Technology and Management

I try to solve numerically a boundary VP for ODE with different order of discontinuity of right part.

Say, the following BVP is given:


y(0)=1, y(2)=1

Let's use piecewise right part

F  := piecewise(x<=1, -x, x>1, 2*x+(x-1)^2)

plot(piecewise(x<=1, -x, x>1, 2*x+(x-1)^2), x=0..2,thickness=5)

The function

piecewise(x<=1, 1-x, x>1, (x-1)^2)

plot(piecewise(x<=1, 1-x, x>1, (x-1)^2), x=0..2, color=blue,thickness=5)

as obviuos, satisfies the BVP exclung the point x=1, where its 1st and 2nd derivatives are discontinuos.

Numerical solution

As:=dsolve([diff(y(x), x$2)+diff(y(x), x)+y(x)=F,  y(0)=1, y(2)=1], y(x), type=numeric, output = Array([seq(2.0*k/N0, k=0..N0)]), 'maxmesh'=500, 'abserr'=1e-3):

provides the solution essentially different to exact one described above:

But if to use the right part

F := piecewise(x<=1, x^2+x+2, x>1, -x^2+x)

plot(piecewise(x<=1, x^2+x+2, x>1, -x^2+x), x=0..2, color=blue,thickness=5)

for which the function

piecewise(x<=1, 1-x+x^2, x>1, -1+3*x-x^2)

plot(piecewise(x<=1, 1-x+x^2, x>1, -1+3*x-x^2), x=0..2, thickness=5)

satisfies the BVP excluding x=1, where this function has discontinuity of 2nd derivative only, the corresponding numerical solution is very similar to this exact solution:

This reason of the difference between these two cases is clear. In the first case both 1st and 2nd derivatives are discontiuos, while in the second one -- 1st derivative is contiuos.

I wonder, if there are numerical methods, implemeted in Maple, for numerical solution of the first type BVP with non-smooth right part?

I am having trouble with understanding how to use piecewise to define a function f such that

f(x) = 1 whenever the VALUE of x is an integer (thus f((2^(0.5) - 1.0)*(2^(0.5) + 1.0)) = 1).

f(x) = 0 otherwise.




1)  Can we compute the error  ( x(t-2*Pi) - x(t) )  without plotting  abs(  x( t-2*Pi ) - x(t) ) ?

     i.e. using norm 1, 2 or infinity  ?


2)  what is op( [ a, b, c] , F ),  where F is a piecewise function ?



The page ?type,piecewise shows the example

type(piecewise[](x < 1, a, b), 'piecewise');

and lines 4-8 of showstat(`print/piecewise`) deal with the case of an indexed piecewise. Yet I can find no other reference to indexed piecewise. What is it used for? When I put an index on a piecewise, nothing special seems to happen, either computationally or display-wise:

piecewise[abs](x > 0, x, -x);
piecewise[Carl](x > 0, x, -x);

The code in `print/piecewise` suggests that it serves some purpose.

1 2 3 4 5 6 7 Page 1 of 8