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
33
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 ...
29
votes
7answers
7k views

Extracting the function from InterpolatingFunction object

I've used Interpolation[] to generate an InterpolatingFunction object from a list of integers. ...
27
votes
5answers
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 ...
17
votes
4answers
779 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 ...
14
votes
3answers
672 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 ...
14
votes
2answers
2k views

Converting ConditionalExpression to Piecewise

I have Solve[] return a list like ...
12
votes
2answers
1k 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): $$\begin{align*} ...
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
496 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
1k 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
3k 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
222 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
1answer
824 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
1k 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 ...
9
votes
1answer
4k 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
533 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
2answers
684 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
133 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 ...
8
votes
1answer
165 views

Automatic Piecewise Approximation

I would like to create the following function: It should approximate a function func[x] by piecewise polynomial interpolations, splitting the approximate function ...
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
650 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
149 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
128 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
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
443 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
2answers
2k 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 ...
7
votes
2answers
432 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 ...
7
votes
3answers
204 views

How can I use Callout to label the segments of a piecewise function?

When I plot a Piecewise function, I like to put the function name above each part of the plot. I could not find a way to use ...
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
258 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
1k 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
4answers
617 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 ...
6
votes
2answers
269 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
170 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
686 views

Solving a PDE with its boundary condition expressed as a piecewise function

All I want to do is solve the PDE pde = D[y[x, t], t] + y[x, t] D[y[x, t], x] == 0 with the boundary condition expressed as a piecewise function, say, ...
6
votes
1answer
118 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
2answers
113 views

Best 5 segment piecewise linear fit

I'm trying to get a piecewise linear best-fit for the closing price of one of the stocks I'm interested in. The logic seems ok, and the workflow works for a straight-line (ie 2 pts, ie 1 segment) ...
6
votes
3answers
522 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
611 views

Using units and piecewise functions in Integrate

For simple cases, Quantities appear to be handled well by Integrate: ...
6
votes
1answer
730 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
0answers
61 views

difference between right click and “copy as LaTeX” and directly using TeXForm

...
6
votes
1answer
252 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
2answers
331 views

Why does plot not show where Piecewise function is not zero?

From my user point view this looks like bug. But I am not an expert. f[x_?NumericQ]:=Piecewise[{{1,x==1},{0,True}}]; Plot[f[x],{x,0,1}] Does not show ...
5
votes
1answer
623 views

Can one compile the Switch function?

I am aware that one can compile Which, e.g.: ff = Compile[x, Which @@ {x < 0, 2, x >= 0, 3} ff[1.] ...
5
votes
4answers
2k 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
833 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
2answers
301 views

Heat equation with variable conductivity

Let us consider the (very simple) problem $\qquad \partial_x[k(x)\,\partial_xu]=0,\quad x\in [0,1]$ $\qquad u(0)=0,\ u(1)=1,$ with $l\in(0,1)$ $\qquad k(x)=\left\{\begin{array}{l}k_1^{},x<l\\k_2^...
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 ...

1
2 3 4 5
8