Items tagged with piecewise piecewise Tagged Items Feed


I would like to build a periodic function based on the function x-->x².

For x belonging to [0,2], the function is defined like this :


And for the rest of the domain in R, the function should be periodic with a period of 2.

In other words, I would like to define a function which permits me to obtain this plot :


Sorry, for my picture which is very ugly, but it should enable to explain what I'm looking for.

1) Is a new piecewise function based on f function is the better solution to create this kind of periodic function ?

2) I didn't find or understand with the help how i can create "infinite" piecewise function. May you help me to define this kind of piecewise function ?

Thanks a lot for your help.

P.S: With the term "nested" in my title I only want to say that I want to apply another piecewise function (for example g) on a already created function (for example f). this term may be not very appropriated.




I would like to build this piecewise function :


I try this manner :

f:=x->piecewise(x=>0 and x <= 1, x^2,x > 1 and x<=2, (2-x)^2);

But, it doesn't work. I receive the following error message :

Error, `>` unexpected

Do you have some ideas about my mistake?

Thank you for your help.


thanks. I played around, and had problems implementing your ideas for one of the systems I'm interested in.I don't see a difference between this and what you had advised me on, but it gets an error.

any idea why?
or how to fix it?

thing1 := diff(B[1](t), t) = piecewise(t <= 500, 0.3e-2-(63/10000)*B[1](t)-(3/500)*B[2](t), -(3/10000)*B[1](t)):
thing2 := diff(B[1](t), t) = piecewise(t <= 500, 0.1e-1-(1/50)*B[1](t)-(13/625)*B[2](t), -(1/1250)*B[2](t)):
sol := dsolve({thing1, thing2, B[1](0) = 0, B[2](0) = 0}, {B[1](t), B[2](t)}, numeric, output = listprocedure); plots:-odeplot(sol, [B[1](t), B[2](t)], t = 450 .. 550);

Error, (in dsolve/numeric/DAE/explicit) unable to obtain the standard form of the DAE system due to the presence of leading dependent variables/derivatives in the piecewise: piecewise(t <= 500, 1/100-(1/50)*B[1](t)-(13/625)*B[2](t), -(1/1250)*B[2](t))-piecewise(t <= 500, 3/1000-(63/10000)*B[1](t)-(3/500)*B[2](t), -(3/10000)*B[1](t))
Error, (in plots/odeplot) curve is not fully specified in terms of the ODE solution, found additional unknowns {B[1](t), B[2](t)}

I'm trying to plot a piecewise function whose return value has units, but I get an error saying that it found a "name" (i.e., the unit symbol).


> f := t -> 2*t;

> fp := piecewise(f(tt) <= 4, 1*Unit(W), f(tt) > 4 and f(tt) <= 8, 2*Unit(W), f(tt) > 8, 3*Unit(W));

> plot(fp, tt = 0..10);


I've tried using the option useunits, but no success. Any help will be appreciated.

Piecewise function in Maple are continuous function. Now, how I able to write the commands for following discontinuous piecewise function?


and when find out f(0) the result is undefined?

In this code, plot() and Norm() fail when used over a piecewise() function:

Error, invalid input: VectorCalculus:-Norm expects its 1st argument, v, to be of type {Matrix, Vector}, but received piecewise(t < 0, Vector[row](2, {(1) = -1, (2) = 0}, attributes = [coords = cartesian]), Vector[row](2, {(1) = 1, (2) = 0}, attributes = [coords = cartesian]))

Note that the same Norm() and piecewise() work fine when used without the plot() function:


Code works fine when piecewise() is removed, leaving a plot() / Norm() combination, for example:

So only the simultaneous combination plot() / Norm() / piecewise() fails. 

Finally, the question is:

Is there a way to plot Norm() / piecewise() combinations without workarounds like intermediate PLOT structures?

Thank you

 with small step rather than two points?

I had writen my questions into this code And I think there's maybe something wrong with A=[0..130]

I think you can understand what I want by reading this code but if you are confused please let me know.

This is the code


Let me explain it.

I am sure that the mistakes must be about the expresstion of the I1(t) and I2(t). Actually if you delete I1(t) and I2(t) , the whole code works and get the picture at the bottom. 

What I want is to put the expresstion of the I1(t) and I2(t) into 'sol:=dsolve...' and 'plots...' to get the picture of I1(t) and I2(t) with respect to t. Before the t* which subject to Phi(t*)=0 (The blue line in the picture at the bottom is Phi) I want I1(t) and after t* I want I2(t).

I1(t) = (int(sqrt(2*(H(t)+omega*cos(q(t)))), q(t) = q(t)-2*Pi .. q(t), numeric))/Pi.    what I want of this experesstion is to get  'int(sqrt(2*(H(t)+omega*cos(q(t)))' from  'q(t)-2*Pi' to 'q(t)' by numeric method.This q(t) is the solution of the ODE sys.

For example(the number I used is not true,just for example) , at the point t=20, q(t)=30-2*Pi.

so I1(t)= (int(sqrt(2*(H(t)+omega*cos(x))), x = 30-2*Pi .. 30, numeric))/Pi.The I2(t) I want is similar to I1(t).


How can I solve it?


I use a map() command to get the values of function for each element of vector. Here is an example of a simple task:




I would like to do the same with piecewise function but it doesn't work. Here is an example:


h:=x->piecewise( 0<=x and x<= 5, f, 5<x and x <= 10, g ) ;

Is it possible to work with piecewise function? Maybe someone has an idea how to do it in different way?

Kind regards,



I'm writing a code and I seem to have an issue when trying to implement a procedure. Here is the code:



Z := 75; A := 189; k := 14.6; Rm := 8*R; r0 := 10^(-8)*R; c := 137.036; ms := 105.66/(.51100)

fmtoau := 10^(-15)/(0.529177e-1*10^(-9)):

R := 1.1*fmtoau*A^(1.0/(3.0)):

f := proc (x) options operator, arrow; 1/(1+exp(k*(x-1))) end proc:

n0 := 3*Z*k^2/(4*Pi*(Pi^2+k^2)*R^3):

n := proc (r) options operator, arrow; 4*Pi*n0*f(r/R) end proc:

int(r^2*n(r), r = 0 .. Rm);



plot(n/n0, 0 .. 2*R);


v1 := unapply(int(x^2*f(x), x), x):

Vfermi := proc (r) options operator, arrow; -4*Pi*n0*R^2*(R*(v1(r/R)-v10)/r+v2Rm-v2(r/R)) end proc:

Vuniform := proc (r) options operator, arrow; piecewise(r < R, -Z*(3/2-(1/2)*r^2/R^2)/R, -Z/r) end proc:

plot([Vuniform(r), Vfermi(r), -Z/r], r = r0 .. 2*R, V = 1.2*Vfermi(r0) .. 0, legend = ["uniform charge", "Fermi distribution", "point charge"]);



plotsol1s := proc (E, K, r0, S, col) local Eqns, ICs, fnl, gnl, r, soln; global ms, c; Eqns := diff(fnl(r), r) = gnl(r)*[E+2*ms*c^2-Vuniform(r)]/c-(K+1)*fnl(r)/r, diff(gnl(r), r) = -fnl(r)*[E-Vuniform(r)]/c-(1-K)*fnl(r)/r; ICs := fnl(r0) = 1, gnl(r0) = 0; soln := dsolve({Eqns, ICs}, numeric); plots:-odeplot(soln, [r, fnl(r)], r0 .. S, color = col) end proc:

plotsol1s(-3*10^5, -1, 10^(-10), Rm, red)

Error, (in f) unable to store '[HFloat(0.0)]' when datatype=float[8]




Any help would be greatly appreciated.


Gambia Man




  I have the following input



with( Statistics ):

g:= exp(-a*x) + c*a*exp(-a*x);
#f := x -> piecewise( x < 0, 0, x>0, g );
 f :=x -> piecewise( x < 0, 0, x>0, exp(-a*x) + c*a*exp(-a*x));

norm_factor:=int( f(x), x=0..infinity );

F := Distribution( PDF = 1/norm_factor*f ):
X := RandomVariable( F ):

N := 20;
S := convert( Sample(X,N), list );




The code works. However, if I comment out 

 f :=x -> piecewise( x < 0, 0, x>0, exp(-a*x) + c*a*exp(-a*x));

 , then use

f := x -> piecewise( x < 0, 0, x>0, g );



f := x -> piecewise( x < 0, 0, x>0, g );
 #f :=x -> piecewise( x < 0, 0, x>0, exp(-a*x) + c*a*exp(-a*x));


It is said "

Error, (in Statistics:-Sample) unable to construct the envelopes for _R, try to specify the initial range"


The norm_factors are actually the same for both inputs. What is the reason for the error message?  Suppose I still want to use something like

f := x -> piecewise( x < 0, 0, x>0, g );

,how to fix the problem?


Thank you very much



  restart: with(plots):
  H := a -> piecewise(a>=0,1):
  f1 := y->(H(y-1*Pi)-H(y-2*Pi))*sin(y)^2:

  g2:= int(g1, y= 0..100):

  g3:= diff(g2,t):
  g4:= diff(g2,x$2):
  g5:= (g3^2+g4^2)/2:
  E2:= unapply(Int(g5, x= 0..100, epsilon= 1e-4, digits= 7), t):
  CodeTools:-Usage( plot(E2, 0..20, numpoints= 50, labels= [t, E]));

The above mentioned code should give constant figure, but it takes a lot of time and not accurate.

If you can help me to improve these codes, I would be pleased.




This question is related to and , however I have not been able to apply the solutions given there to my problem.

I have a set of points given by

and certain function value points given by

where e1 is the function I am approximating.


I come up with my piecewise function.

When I do diff(e4,x), however, the points at the nodes show "float(undefined) x=0.2..."(the node).

As it turns out, the value of the derivative on the left of the node is not equal to that of the right side by a factor of 10^(-7), in other words, numerically unimportant but high enough for maple to realise it is not the same number. How could I tell Maple that I am happy choosing, for example, the value given by the function on right side of the node?

I welcome any suggestions.

Many thanks in advance.

I am trying to find the pdf of the inverse of a random variable X, that represents a distribution I defined. I currently have : 

T:=Distribution(PDF=(x->piecewise(0<x and x<1,1/4,3<x and x<5,3/8,0)));

but this seems to be incorrect as I get a message saying T1:=RandomVariable(Y) is being passed a wrong argument. 

What is the right way to obtain the pdf of 1/X?



I am trying to approximate a function in terms of piece-wise constant function:


$$f(x) = \sum_0^N c_iB_i(x)$$


what modules/packages of maple are helpful here? thanks

1 2 3 4 5 6 7 Page 1 of 8