Questions tagged [piecewise]
Questions about the Piecewise function, which is represented by different expressions for different ranges of values of its independent variable.
60
questions
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 ...
22
votes
5answers
6k views
Extracting the function from InterpolatingFunction object
I've used Interpolation[] to generate an InterpolatingFunction object from a list of integers.
...
26
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 ...
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
3answers
995 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 ...
2
votes
1answer
764 views
Piecewise Function, Explanation of Extra Case
Consider:
Can someone explain why the 0, True occurs?
As a second question, I like to fill the endpoints as follows:
...
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
510 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):
...
6
votes
4answers
571 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 ...
8
votes
2answers
535 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 ...
4
votes
1answer
144 views
Piecewise[] merge equivalent conditions
Is there a simple way to merge equivalent conditions in Piecewise? For example, in the following we have (trivially) pw==x.
<...
3
votes
1answer
270 views
Does 'True' stand for 'Else' in Maximize function output? [closed]
Maximize[a x^2 + b x + c, x]
gives this solution:
Does the word True stand for "else" here? What exactly did ...
10
votes
2answers
219 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,
...
7
votes
2answers
503 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 ...
17
votes
4answers
751 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 ...
7
votes
4answers
419 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{...
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
0answers
88 views
External and internal Assumptions on `PiecewiseExpand` and `Mod`
After this question I'm intrigued about the different behaviors depending where and how the assumptions are given.
...
4
votes
2answers
680 views
Derivative of piecewise function
I tried to find $f'(0)$ of this function:
$$f(x) = \begin{cases} x\cdot\sin(\frac{1}{x}) & \text{if $x\ne0$} \\ 0 & \text{if $x=0$} \end{cases}$$
This is what I tried in Mathematica:
...
3
votes
1answer
1k views
Piecewise Function within NDSolve
So I am quite new to Mathematica and programming in general and I am running into an issue while trying to use the numerical diff eq solver (NDSolve) within mathematica. This is what my code looks ...
1
vote
1answer
406 views
How to draw dots at beginning and end of range when plotting [duplicate]
I'm trying to plot a Piecewise function which should look like this:
How can I have similiar dots (filled or empty) at beginning and end of range?
...
0
votes
1answer
373 views
Finding the tangent to a piecewise function
I'm new to Mathematica and I am trying to calculate the expression of the tangent to the curve
...
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 ...
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}}]
...
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*}
...
11
votes
2answers
421 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 (...
4
votes
2answers
269 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 ...
3
votes
2answers
2k views
Trying to solve a differential equation with a piecewise initial condition
I am trying to solve $$u_t=\frac{1}{4}u_{xx}$$ $-\infty<x<\infty,\: t>0$
With the initial condition $u(x,0)=\phi(x)$ where
$$
\phi(x)= \left\{
\begin{array}{lr}
1 & |x|&...
8
votes
1answer
147 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
<...
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 ...
4
votes
4answers
866 views
How to select a certain portion of a piecewise function?
For example, if I have Piecewise[{{x^2, x < 0}, {x, x > 0}}], how do I select only the function that is for x < 0? I ...
3
votes
2answers
232 views
Plot a parametric function from $\mathbb{R}^2$ to $\mathbb{R}^2$
I want to plot the following function defined on $[-1,1]\times[-1,1]\setminus(0,0)$:
$$
F(x,y,t)=
\begin{cases}
(1-t)(x,y)+t(1,\frac{y}{x})& \text{if}\ -x\leq y\leq x \\
(1-t)(x,y)+t(\frac{x}{y}...
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):
...
8
votes
2answers
671 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 & \...
6
votes
1answer
235 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
2k 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
406 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 ...
4
votes
2answers
612 views
How I can make the StreamPlot of this differential equation?
I need the StreamPlot of this differential equation but I don't know how.
dp/dt = 0.4 p(1 - p/30), 0 <= t < 5 and 0.4 p (1 - p/30) - 0.25 p, t > 5
4
votes
3answers
505 views
Most efficient way to integrate a linear piecewise function
I am having a lot of trouble integrating a simple function. Here's the context: I generate data from two distributions, and I want to calculate the overlap coefficient of representing those datasets ...
4
votes
1answer
283 views
Integration of a piecewise function
Define
f[x_, i_] := Piecewise[{{1/n,
0 < x < (i - 1)/n}, {(i/n - x), (i - 1)/n < x < i/n} , {0,
i/n < x}} ];
I would like to compute
...
3
votes
1answer
656 views
What does True mean in this case? [duplicate]
I use the following code to find the expansion for small $x$:
...
3
votes
2answers
402 views
Derivative of Continuous and Differentiable Piecewise function is indeterminate
I'm attempting to analytically stitch together two linear functions with two quadratic functions. The function I'm considering, then, is a piecewise function consisting of 4 parts. Below are the ...
3
votes
0answers
106 views
PiecewiseExpand fails with min of two piecewise functions
I have two Piecewise functions that are (in this case) just the empirical CDFs of two sets of data:
...
2
votes
3answers
109 views
Identifying maximal element in a list with case distinctions
I need to create code that identifies the positions of all maximum elements of a list. I already know that if myList contains only numbers, I can do
...
2
votes
2answers
71 views
How to define a discrete distribution with non-integer sample space elements (outcomes)?
How to define a discrete distribution with non-integer states (sample space elements)?
I know how to define a discrete distribution with integer states:
...
2
votes
0answers
81 views
Piecewise function Syntax error only in debug mode
If I enter the following through mathematica (11.0) through UI $$\begin{cases} 1 &x \geq 0 \\ 0 &True \end{cases}$$ No error is reported.
$$\begin{cases} 1 &x \geq 0 \\ 0 &True \end{...
2
votes
2answers
69 views
Combine `EvenQ` and `Clip` to create a series of pulses
I have a function that produces a single impulse spanning the range [-1,1]:
func1 = Cos[Pi*(Clip[x]/2)]^2
I want to create a ...
2
votes
0answers
334 views
Piecewise functions Integration with symbol
I want to integrate one piecewise functions as following,
...
2
votes
1answer
151 views
NDSolve Dankwerts boundary conditions with changing discrete variable
I'm modeling a advection-diffusion-reaction plug flow reactor (2nd order ODE) at steady state:
$$0=-v \frac{\delta p[z]}{\delta z}+D\frac{\delta ^2 p[z]}{\delta z^2}-k p[z]$$
With Dankwerts boundary ...
2
votes
1answer
274 views
Piecewise function extraction
I have a piecewise function and I'm trying to extract the different functions for different values of the parameter. I use:
...