Questions tagged [piecewise]

Questions about the Piecewise function, which is represented by different expressions for different ranges of values of its independent variable.

Filter by
Sorted by
Tagged with
32
votes
3answers
4k views

Plot a piecewise function with black and white disks marking discontinuities

Let's say I'd like to plot Sign[x + 0.5]: Plot[Sign[x + 0.5], {x, -1, 1}] Mathematica will give this: This plot does not ...
25
votes
4answers
5k views

Plotting piecewise function with distinct colors in each section

I have a piecewise function that I would like to plot but I was wondering if it is possible that each part of the function that is plotted when its corresponding condition is true be plotted with a ...
22
votes
5answers
5k views

Extracting the function from InterpolatingFunction object

I've used Interpolation[] to generate an InterpolatingFunction object from a list of integers. ...
17
votes
4answers
736 views

Piecewise imposes internal boundaries in NDSolve - is this expected?

In the following code I used True as the predicate for DirichletCondition and found that the boundary condition was applied not ...
13
votes
2answers
2k views

Converting ConditionalExpression to Piecewise

I have Solve[] return a list like ...
12
votes
2answers
1k views

Is there a way to symbolically invert a piecewise function

I have a piecewise function that is continuous and strictly monotonic, like this: f[t_] = Piecewise[{{t/4, t < 0}, {t/2, t < 3}, {3/2 + (t - 3)*3, True}}] ...
11
votes
2answers
385 views

StreamPlot No Longer Works With Piecewise Function?

Bug introduced in 7.0.1 or earlier and persisting through 11.1 I was trying to plot a phase portrait (StreamPlot) using a piecewise (...
10
votes
7answers
2k views

How to plot a list to look like step function?

Suppose I have the following simple list. l = {0,2,5,9,14}; I want to to make a plot that looks like the figure below. I.e. I want to have a horizontal line ...
10
votes
3answers
943 views

How to efficiently get breakpoints of piecewise functions

Setting. Imagine that we have several arbitrary linear functions in 2D that can be changed manually (say, slopes are parameters). Their Min (or ...
10
votes
3answers
2k views

Can NDSolve handle discontinuous data?

Backslide introduced in 9.0, persisting through 11.0.1 It is possible to numerically solve a differential equation if not-smooth data are involved? For example the following instruction return the ...
10
votes
5answers
1k views

NIntegrate extremely piecewised functions

I often need to integrate extremely piecewised functions, like the following one (not extreme, but gives an idea): ...
10
votes
2answers
215 views

Problem with Integrate with Piecewise and PrincipalValue

Bug introduced in 8.0.4 or earlier and persisting through 11.3 In the course of developing an alternative solution for question 127301, With, ...
10
votes
2answers
955 views

Solving a discontinuous differential-algebraic equation system for plasticity behaviour

I need to solve a discontinuous equation which is typical in theory of plasticity. For a simple case I get the following equation system (reformulated for numerical implementation): $s(t) = \frac{\...
10
votes
1answer
725 views

Plot3D with Piecewise is Empty [duplicate]

I don't understand what has happened here. Please can someone explain? I start by defining a 2D function and plotting a couple of cases ...
9
votes
5answers
999 views

Piecewise-constant function with infinitely many pieces

How can I define the following function in Mathematica: $$f(x)=\begin{cases} x-n,&\mbox{when $2n\le x\le 2n+1$,}\\n+1,&\mbox{when $2n+1\le x\le 2n+2$,}\end{cases}$$ where $n$ takes all ...
8
votes
2answers
504 views

How to display the support $\mathrm{supp}(f)$ of a piecewise function $z = f(x,y)$?

I'm wondering how to display the support $\mathrm{supp}(f)$ of a piecewise surface $z = f(x,y)$. As an example, let's consider the following piecewise function (and plot it): ...
8
votes
1answer
3k views

Fitting piecewise functions

I have several large data sets which follow the following pattern: A position is measured, a force is applied until a new equilibrium is found. I'd like to find a fit for the position, at least at the ...
8
votes
2answers
666 views

How to plot this function?

I'd like to plot the function $f$ in this answer https://math.stackexchange.com/a/788818/66096 Let $h(x) = \begin{cases} e^{ -\frac{1}{1 - (1-2x)^2}} & \mbox{ for } 0<x < 1\\ 0 & \...
8
votes
1answer
2k views

why mathematica outputs “True” sometimes in outputs that are conditional to mean “in all other cases”? [closed]

consider this simple Input: Probability[g <= m && m <= -g + 1, {m, g} \[Distributed] UniformDistribution[{{0, 1}, {0, gg}}]] which generates a ...
8
votes
2answers
491 views

Using OddQ and EvenQ in piecewise functions: Buggy?

I was trying to evaluate a sum over a piecewise function, not unlike this example. However, my piecewise function needed to be defined differently for even and odd ...
8
votes
1answer
144 views

Bug in PiecewiseExpand and Mod with assumptions

Bug introduced in 9.0 or earlier and fixed in 10.4 The following code using PiecewiseExpandand Modgives the wrong answer <...
8
votes
0answers
120 views

Syntax error in debug mode

Bug introduced in 8.0 and persisting through 10.3.0 It appears that in debug mode, the \[Piecewise] symbol and its associated grid give a syntax error. For a ...
7
votes
2answers
459 views

Solving Stefan's solidification problem - for the case of 3 regions

This question heavily related to this question, where the case of two PDE's are solved along with a zipping condition that is a function of time. Using the link in the code I have solved this set of ...
7
votes
1answer
2k views

Why does piecewise plot have a discontinuity when the function, first and second derivatives are equal?

There were some related questions to this one, but in this case we have the function value, the first derivative and the second derivative all equal (that was actually the problem I was solving). We ...
7
votes
4answers
401 views

Recursive piecewise integral relation with piecewise base case?

How is this recursive formula $$ f_{n+1}(z) = \int_0^1 f_{n}(z-y)\,{\rm d}y $$ implemented in Mathematica? The base case is $$ f_1(z) = \begin{cases} 1 & 0\leq z\leq 1 \\ 0 & \text{...
7
votes
3answers
2k views

Piecewise Polynomial Interpolation

Given some data pairs $(x_i,y_i)$, with $i=0,...,m$, and a degree $r$, I wish to build a piecewise polynomial function to interpolate these data. That interpolation should be continuous, and, on every ...
7
votes
1answer
103 views

Converting Piecewise function to C code

I'm trying to convert a function to C Code f = D[Piecewise[{{x^2, -10 < x < 0}, {x, x > 0}}], x] I decided to scan through conditions and corresponding ...
7
votes
1answer
233 views

Slow NIntegrate over complicated domains (sometimes)

This is no longer a problem with Mathematica 10.* where I can use regions to do the integrations Overview I have a very large matrix I need to calculate where each element requires the integration ...
6
votes
5answers
850 views

Plotting a function discontinuous at one point

I am trying to plot a function which has a discontinuity at one precise point. However, when I naïvely use Plot with this function the discontinuity point does not seem to be taken into account. For ...
6
votes
2answers
382 views

Alternatives to piecewise

As has been mentioned before, Piecewise causes the Mathematica Kernel to fail to during a NDSolve calculation. How can I write ...
6
votes
2answers
1k views

Piecewise differential equation

Say I have a very trivial function $$ V\left(r\right)=\begin{cases} -V_{0} & 0\leq r\leq R\\ 0 & r>R \end{cases} $$ with $V_{0}>0$ and $R>0$. I would like to let Mathematica solve ...
6
votes
2answers
135 views

The correct way of compute indicator function in Mathematica

$$X_1= \mathbb{1}_{[0,1]} $$ $$X_2= \mathbb{1}_{[0,\frac{1}{2}]}, X_3= \mathbb{1}_{[\frac{1}{2},1]}, $$ $$X_4= \mathbb{1}_{[0,\frac{1}{3}]}, X_5= \mathbb{1}_{[\frac{1}{3},\frac{2}{3}]}, X_6= \mathbb{...
6
votes
2answers
161 views

Switched linear systems

I'm curious whether it is possible to solve switched linear systems within the framework of NDSolve. For example a system of linear ode's like $$x'(t) = \left\{\...
6
votes
1answer
115 views

Troubles using Which or Piecewise to define a function

I have a very simple task, but for some reason I fail to accomplish it. I want to use Which to define a function. I think the code is self-explanatory: ...
6
votes
3answers
386 views

Integrating a piecewise function with symbolic pieces

Suppose I want to compute the antiderivative of this simple function: $$f(t)=\begin{cases} 1 & \text{if $0\le t\le t_1$,} \\ -1 & \text{if $t_1\le t\le t_2$}, \\ 0 & \text{otherwise,} \end{...
6
votes
2answers
594 views

Using units and piecewise functions in Integrate

For simple cases, Quantities appear to be handled well by Integrate: ...
6
votes
1answer
664 views

Using Boole to integrate over region

I have a complex and long piecewise function to integrate, so I have written a Mathematica script that cuts the whole range of integration, evaluates the integrand in each region and then integrates ...
6
votes
1answer
231 views

Plotting piecewise functions with distinct colors - issue found [duplicate]

I've been making use of the following thread: Plotting piecewise function with distinct colors in each section A handy feature I found there goes as follows: ...
5
votes
4answers
1k views

Plotting piecewise functions

I have a list of N functions called functionList. N can be any number. Also, I have a list ...
5
votes
3answers
790 views

Colors for multiple piecewise functions

I have the following $3$ piecewise functions, and I would like that f1, f2 and f3 have $3$ ...
5
votes
3answers
537 views

Transform Piecewise[] into sum of indicators

I'm interested in transforming a piecewise defined function into a sum of indicator functions, ultimately with the aim to be better able to integrate them. As an example I would like to transform the ...
5
votes
2answers
5k views

Plot Even Piecewise function

Is it possible in Mathematica to plot an even piecewise function like: $ f(x) = \begin{cases} 3t , 0 \le t \le \frac{\pi}{2} \\ 3t + 6 , \frac{\pi}{2} \le t \le \pi \end{cases}$ which has ...
5
votes
2answers
620 views

Defining a piecewise function from a sequence of functions [closed]

I'm attempting to define a piecewise function from a sequence of defined functions. I've attempted something like this: ...
5
votes
2answers
1k views

Derivatives of piecewise functions of functions

Background Derivatives of piecewise functions in Mathematica are computed according to special rules. According the Piecewise documentation (see Possible Issues), ...
5
votes
1answer
1k views

Integrate over piecewise function defined using /;

f[x_] := x /; x<0 f[x_] := x^2 /; x>=0 Integrate[f[x],{x,-1,1}] The above does not work (Mathematica returns it unevaluated), but the below does. <...
5
votes
3answers
447 views

Creating a function with integral zeroes of the 0th, 1st, and 2nd derivatives

I would like to be able to randomly generate functions, each of which satisfies $f : [-10, 10] \rightarrow [-10, 10]$ All the zeroes, critical points, and inflection points have an integral $x$-...
5
votes
1answer
399 views

Error messages from compiling the Piecewise function

The following works Compile[{{x, _Real}}, Piecewise[{{{0., 0.}, x > 5.}}, {1., 1.}]] but if I enter the Piecewise[...] in ...
5
votes
1answer
235 views

How to sort the ranges of a Piecewise expression?

I have the following PieceWise expression ...
5
votes
1answer
137 views

UnitStep vs HeavisideTheta; KroneckerDelta vs DiscreteDelta

What are the differences between these pairs of functions? In many cases, they seem to act the same. For example, both DiscreteDelta and ...
5
votes
1answer
131 views

Does Mathematica have this function implemented?

Is there a function in Mathematica that can express a piecewise function in terms of the Heaviside theta function? Say, for example, the solution to this ODE.