Close
Close window
piecewise examples - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Mozilla Firefox.
Maplesoft logo
Maplesoft logo

Online Help

All Products Maple MapleSim


[フレーム] [フレーム]

Piecewise Functions

This worksheet contains a number of examples of the use of the piecewise function.

>

restart

>

withplots:

Some Simple Examples

The piecewise function has a straightforward syntax. Every piece is specified by a Boolean condition followed by an expression. The next several Maple command lines make use of the following piecewise function:

>

f:=piecewisex1&comma;x&comma;x1&comma;x2&comma;1<x&comma;sinx1x1

fxx−1x2x1sinx1x11<x

(1.1)

This can be evaluated at arbitrary points. For example,

>

fx&equals;2.|fx&equals;2.

2.

(1.2)
>

fx&equals;3.|fx&equals;3.

0.4546487134

(1.3)

Because of the division by zero, points such as x = 1 cannot be substituted. However, limx1f can be determined.

>

limx&rarr;1f

1

(1.4)

Such functions can be plotted to determine their behavior.

>

plotf&comma;x&equals;2..2

Besides evaluating limits, you can do operations such as computing derivatives, integrating, and solving differential equations with piecewise functions. Examples of solving DEs will be illustrated later. Using the same function, f(x), find its piecewise derivative.

>

fprime&DifferentialD;&DifferentialD; x f

fprime−1x<−1undefinedx=−12xx<1undefinedx=1cosx1x1sinx1x121<x

(1.5)

Integrating this function gives:

>

&int;f&DifferentialD;x

x22x−1x3316x1Six1+161<x

(1.6)

Evaluate the integral with values for the lower and upper bounds.

>

&int;23f&DifferentialD;x

136+Si2

(1.7)

where Si(x) is the Sine integral function .

Note: This works because discont is able to determine the potential discontinuities of piecewise functions. For example,

>

discontf&comma;x

(1.8)

Consider a new function, g(x).

>

gf piecewisex<0&comma;x&comma;&ExponentialE;x

gxx−1x2x1sinx1x11<xxx<0&ExponentialE;xotherwise

(1.9)

The piecewise function allows for common manipulations, such as simplifications. The addition of the selector 'piecewise' indicates to simplify that it should only do simplifications as they apply to piecewise functions. This is more efficient, in general.

>

simplifyg&comma;&apos;piecewise&apos;

x2x−1x3x<0x2&ExponentialE;xx1sinx1&ExponentialE;xx11<x

(1.10)

Piecewise functions can be converted to a number of other functions and distributions.

>

heavyf:=convertf&comma;&apos;Heaviside&apos;

heavyfxHeavisidex+1xx2Heavisidex1+x2Heavisidex+1+sinx1Heavisidex1x1

(1.11)

Distributions can be converted back to piecewise functions.

>

normalconvertheavyf&comma;&apos;piecewise&apos;

xx<−1undefinedx=−1x2x<1x3undefined+x2undefined+x3+sinx1undefinedx2x1x=1sinx1x11<x

(1.12)

The function piecewise lets us work with the CurveFitting[Spline] command. For example,

>

newcubicCurveFittingSpline0&comma;1&comma;2&comma;3&comma;0&comma;1&comma;4&comma;3&comma;x

newcubic45x3+15xx<12x3+425x2415x+145x<265x3545x2+1515x1145otherwise

(1.13)

This spline can be graphed.

>

plotnewcubic&comma;x&equals;1..3

But now we can also integrate it.

>

assume2<a

>

1anewcubic &DifferentialD;x

595+310a~4185a~3+15110a~21145a~

(1.14)

Other piecewise functions can also be converted to piecewise and be properly manipulated. For example,

>

convert1x&comma;&apos;piecewise&apos;

x1x−1x+1x<01xx<1x11x

(1.15)

Another example of conversion, in this case with signum and abs .

>

convertsignumx1x&comma;&apos;piecewise&apos;

x1x−1x+1x<00x=0x1x<11x1x

(1.16)

Note: The discontinuity at 0 in the above function.

Another simple example:

>

h:=convertx2&comma;&apos;piecewise&apos;

hx2

(1.17)

It turns out to be a well-behaved, non-piecewise function.

We can also determine the differentiability class of a piecewise continuous function. For example, in the case of our previous spline function, newcubic, we have

>

isdifferentiablenewcubic&comma;x&comma;2

true

(1.18)

This must be true for splines! However, when we check to see if it is C3, we obtain

>

isdifferentiablenewcubic&comma;x&comma;3&comma;&apos;badpoints&apos;

false

(1.19)

To determine the highest order of continuity and the problem points, enter:

>

badpoints

2,1&comma;2

(1.20)

Converting to Piecewise Functions

Define f(x) as:

>

f:=maxx22&comma;x1

fmaxx1&comma;x22

(2.1)

Derivatives can be found, and piecewise functions are returned.

>

fprime&DifferentialD;&DifferentialD; x f

fprime2xx<52+12undefinedx=52+121x<52+12undefinedx=52+122x52+12<x

(2.2)

The graph of the derivative can be drawn as well.

>

plotfprime&comma;x&equals;2..2&comma;&apos;discont&equals;true&apos;

The straightforward calculation of the series of f around x=0 can be computed by using the series command.

>

seriesf&comma;x

−1+x+Ox6

(2.3)

This produces an answer with a superfluous order term. However, series can do better when using piecewise. Thus, it can be converted to a new function g(x).

>

g:=convertf&comma;&apos;piecewise&apos;

gx22x52+12x1x52+12x2252+12<x

(2.4)
>

seriesg&comma;x

−1+x

(2.5)

We can then also plot this max function.

>

maxplot:=plotg&comma;x&equals;2..2&comma;color&equals;red&colon;

>

maxplot

LaTeX output can also be generated.

>

latexg

\left\{\begin{array}{cc}
x^{2}-2 & x \le -\frac{\sqrt{5}}{2}+\frac{1}{2}
\\
x -1 & x \le \frac{\sqrt{5}}{2}+\frac{1}{2}
\\
x^{2}-2 & \frac{\sqrt{5}}{2}+\frac{1}{2}<x
\end{array}\right.

Similarly, everything also works with min.

>

h:=convertminx22&comma;x1&comma;&apos;piecewise&apos;

>

minplot:=ploth&comma;x&equals;2..2&comma;color&equals;green&colon;

>

displaymaxplot&comma;minplot

hx1x52+12x22x52+12x152+12<x

Converting to piecewise also works with _Envsignum0 := 1;

>

_Envsignum0:=1

_Envsignum01

(2.6)
>

convertsignumx&comma;&apos;piecewise&apos;

−1x<010x

(2.7)
>

&DifferentialD;&DifferentialD; x

undefinedx=00otherwise

(2.8)

Solving Differential Equations with Piecewise

We can solve differential equations with piecewise functions in the coefficients. The type of equations that one can solve include all first-order methods using integration, Riccati, and higher-order methods including linear, Bernoulli, and Euler. Some examples are included in the sections that follow.

First-order Linear with Piecewise (Nontrivial) Coefficients

Consider the following DE:

>

eq:=&DifferentialD;&DifferentialD;xyx&plus;piecewisex<x24&comma;&ExponentialE;xyx

eq&DifferentialD;&DifferentialD;xyx+&ExponentialE;xx<x240otherwiseyx

(3.1.1)

Now solve this DE.

>

dsolveeq&comma;yx

yx=&ExponentialE;&ExponentialE;xc__1x<12172&ExponentialE;&ExponentialE;12172c__1x<12+172&ExponentialE;&ExponentialE;12172&ExponentialE;x+&ExponentialE;12+172c__112+172x

(3.1.2)

Now an example of a linear equation with polynomial coefficients and 0-1 perturbation.

>

eq:=&DifferentialD;2&DifferentialD;x2yx&plus;2x&DifferentialD;&DifferentialD;xyx&plus;2yx&equals;piecewise0<x&comma;1

eq&DifferentialD;2&DifferentialD;x2yx+2x&DifferentialD;&DifferentialD;xyx+2yx=10<x0otherwise

(3.1.3)

Solving the equation gives:

>

dsolveeq&comma;yx

yx=&ExponentialE;x2erfixc__1+&ExponentialE;x2c__2x<0&ExponentialE;x2erfixc__1+&ExponentialE;x2c__2+&ExponentialE;x2&ExponentialE;x22&ExponentialE;x220x

(3.1.4)

Now we attempt to solve a DE with y(x) inside the piecewise function.

>

eq:=&DifferentialD;&DifferentialD;xyx&equals;piecewise1<x&comma;yx&comma;1

eq&DifferentialD;&DifferentialD;xyx=yx1<x1otherwise

(3.1.5)
>

dsolveeq&comma;yx

yx=c__1+xx<1&ExponentialE;x1c__1+&ExponentialE;x11x

(3.1.6)

And now solve a nonlinear differential equation with discontinuities depending on parameters. The assumption is needed because this function is discontinuous in 0 and a.

>

assumea&gt;0&colon;

>

g:=convertsignumx ax&comma;&apos;piecewise&apos;&comma;x

ga~xxa~a~+xx<0a~+xx<a~a~xa~x

(3.1.7)
>

dsolve&DifferentialD;&DifferentialD;xyx&equals;g yx2&comma;yx

yx=22xa~+x2+2c__1x<a~22a~22xa~x2+2c__1x<022a~2+2xa~x2+2c__1x<a~22xa~+x2+2c__1a~x

(3.1.8)

Linear DE with Constant Coefficients and Piecewise Forcing Function

>

eq:=&DifferentialD;2&DifferentialD;x2yx&plus;yx&equals;piecewisex<0&comma;x&comma;0<x&comma;x

eq&DifferentialD;2&DifferentialD;x2yx+yx=xx<0x0<x

(3.2.1)


The solution with given initial values:

>

dsolvey0&equals;0&comma;Dy0&equals;1&comma;eq&comma;yx

yx=2sinxxx<0x0x

(3.2.2)

The general solution:

>

dsolveeq&comma;yx

yx=sinxc__2+cosxc__1xx<0sinxc__2+cosxc__1+x2sinx0x

(3.2.3)

This also works in the case of output = basis.

>

dsolveeq&comma;yx&comma;output&equals;basis

cosx&comma;sinx&comma;xx02sinxx0<x

(3.2.4)

Nonlinear Riccati DE with Piecewise Continuous Coefficients

Consider the following differential equation.

>

eq:=&DifferentialD;&DifferentialD;xyx&equals;piecewisex&gt;0&comma;xyx2

eq&DifferentialD;&DifferentialD;xyx=x0<x0otherwiseyx2

(3.3.1)
>

eq_sol:=dsolveeq&comma;yx

eq_solyx=1c__1x<02x22c__10x

(3.3.2)

For more information, see the piecewise help page. See also the following help pages: series , max , min , int , dsolve , discont , CurveFitting[Spline] , signum , and abs .

Return to Index for Example worksheets


Download Help Document

AltStyle によって変換されたページ (->オリジナル) /