The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
3
votes
1answer
188 views

Edit piecewise function

Downstream of a long process I got the following function: $\begin{cases} 0 & x < 0 \, \vee \, x > \frac{307}{400} \\ 14.27-7.44\,x & 0 < x < \frac{307}{400} \\ \text{...
0
votes
1answer
106 views

Strange behavior of derivative when using Abs[] [duplicate]

This code where I try to visualize Newton's Method on the function $f(x)=\sqrt{|x|}$ works fine when I define $f(x)$ piecewise. Then why doesn't it work when I define ...
0
votes
1answer
429 views

How to apply a piecewise function to a list? [closed]

I have created this code: xn = {4, -4, 4, -4} f[x_] = Piecewise[{{Sqrt[x], x >= 0}, {Sqrt[-x], x < 0}}] f[xn] With this code I would expect as a result: ...
2
votes
2answers
156 views

Integrate over a piecewise function [closed]

I want to calculate the indefinite Integral of $$f(x)=\begin{cases} 2x\cos(\frac{1}{x})& \text{ if } x\ne 0 \\ 0& \text{ if } x=0 \end{cases}.$$ I use the following code: ...
2
votes
2answers
617 views

solve system including piecewise function

I've read posts about how to do this in a direct way, but the method doesn't work for my simple system? -- see code: ...
0
votes
1answer
95 views

When does this function equal 'true'? [closed]

I've used Mathematica to find the function $f(t)$ and I found this: Q: What does 'true' say about the function. Is it always equal to $ae^{-\frac{t}{p}}$ or what does it mean?
4
votes
1answer
260 views
2
votes
2answers
332 views

How do I solve Schrödinger equation with a piecewise periodic Hamiltonian? [duplicate]

My Hamiltonian is HI(t) = Piecewise[ {{H1, n τ <= t <= (n + 1/2) τ}, {H2, (n + 1/2) τ <= t <= (n + 1) τ}}] where ...
1
vote
4answers
399 views
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
1answer
2k views

How can I combine two Piecewise functions?

I want to be able to combine two piecewise functions. For example, suppose Piecewise1 = f=3 for 1 <=x <=2 and Piecewise2 = f=5 for 1.5<=x<=3, I ...
3
votes
2answers
384 views

Switching Differential Equation in NDSolve

I am trying to solve the following system of differential equations using NDSolve: $\dot{z}_t=.5(1-z_t)$ $\dot{y}_t=.05y_t+z_t-x_t$ subject to the following constraint: $-y_t-z_t\le0$ where $z_t$ ...
1
vote
2answers
114 views

Scale in plot and Piecewise

I wrote this code: ...
6
votes
2answers
387 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 ...
3
votes
1answer
330 views

Performance of numerical integral over piecewise function

Using mathematica version 10.3, I observe that an integral over a piecewise function evaluates a factor 10 slower than the same integral without the piecewise distinction. Here is the code: ...
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), ...
-1
votes
1answer
78 views
1
vote
0answers
279 views

NDSolve for an (slightly) discontinuous Piecewise input

I define a Piecewise discontinuous function V[f[t]], which depends on a function f[t] I want to get numerically using ...
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 ...
5
votes
1answer
402 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
1answer
351 views

Plotting a vector function composed of piecewise scalar functions

I am relatively new to Mathematica, and am having difficulty doing something that is conceptually very simple. I am trying to plot a vector field in which the vector function is composed of piecewise ...
1
vote
1answer
81 views

Plot of a piecewise function misbehaving [closed]

Hi: I am trying to plot a piecewise function. What's weird is that the plot either (a) does not appear at all, or (b) depending on settings, either appears as a horizontal/vertical time. I must be ...
1
vote
0answers
287 views

How to use interpolating functions in NSolve with a piecewise function?

I have a system of two non-linear differential equations that are piecewise, the solutions of which I would like to use to obtain the values of a third variable that is also piecewise. I have trouble ...
1
vote
0answers
77 views

NDSolve with (approximately continuous) Piecewise input

I am finding some troubles when using NDSolve with this piece of code: ...
3
votes
1answer
632 views

NDSolve with an interpolating function in Piecewise

Again I have question I hope someone can answer. I made a very simple example illustrating my more difficult problem. I want to solve a differential equation numerically with a conditional in the ...
1
vote
1answer
385 views

Piecewise function on boundary condition

Consider this initial-boundary-value problem: $$\frac{\partial u}{\partial t}=\frac{\partial^2u}{\partial x^2}+\frac{\partial^2u}{\partial y^2},\quad \Omega=(0,1)\times(0,1),\quad t>0.$$ $$u(x,0,t)=...
-1
votes
2answers
517 views

Make plot of parametric function with piecewise components

How can I plot this curve in Mathematica: $\begin{align} x(t)&=\begin {cases} 2 (t-n+1) \: , \: t\in [n-1,n-1/2] \\ 2-2 (t-n+1) \: , \: t\in [n-1/2, n] \end {cases}\\ y(t)&=\begin {cases} 2 (\...
4
votes
2answers
644 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: ...
-2
votes
1answer
120 views

Manipulate aborts the computation of NDSolve with the Piecewise function after curtain time [closed]

I'm facing the problem with performance: Manipulate is working fine till end time =60 sec, after that it aborts. There is NDSolve inside Manipulate with Piecewise function, and the plot of the ...
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: ...
3
votes
2answers
220 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}...
3
votes
2answers
895 views

How do I find the intersection points of two piecewise functions? [closed]

I've got a piecewise function that is defined as follows: ...
2
votes
1answer
171 views

How to constrain the range of a plot of a function to satisfy an inequality?

Given the function f[x_] := x^2, how can I plot f[x] over the range where f[x] >= 10 and <...
1
vote
2answers
98 views

Plot Piecewise does not work with NDSolve, works with ordinary function [closed]

How to join output from NDSolve with another function in a plot, using Piecewise or something else? I could join a simple function with another in a plot, but could not do it for the output. MWE: <...
0
votes
1answer
227 views

If Which Piecewise-defined function [closed]

How would I write a function using If and/or Which command for following Piecewise-defined function then plot the function ...
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 ...
2
votes
1answer
2k views

NDSolve with piecewise function gives error “Encountered non-numerical value for a derivative at x == 0.” [duplicate]

I am solving differential equation with piecewise function and i am getting error that NDSolve::ndnum: Encountered non-numerical value for a derivative at x == 0.. >...
10
votes
3answers
962 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 ...
1
vote
1answer
162 views

How to make the result of Piecewise be a closed interval?

Description This question comes from two questions. Namely Q1 and Q2 The defintion of B-Spline basis function as shown below: Let $\vec{U}=\{u_0,u_1,\ldots,u_m\}$ a nondecreasing sequence of real ...
2
votes
0answers
72 views

Why does this form of piecewise function cause a general stop message? [closed]

I am currently learning about Mathematica by reading the book Mathematica by Example, 4th Edition. In that book, a piecewise function was defined as follows: ...
1
vote
0answers
236 views

Piecewise in 2D with quadrants as conditions

I am trying to define a piecewise vector field. Can the condition be of the form I am using (i.e. a quadrant)? I can't also make Piecewise work with more than two ...
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 ...
7
votes
4answers
410 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{...
4
votes
1answer
280 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 ...
2
votes
1answer
111 views

Problems with multidimensional system of ODEs

I am trying to look at the migration of "i" number of species "c" in "10" number of habitats {designated as "j"} and to study their population size as a function of time "t". I have this following ...
2
votes
0answers
60 views

Mathematica Manipulate, Plot, Piecewise [duplicate]

I have two functions h2 and v2 that I need to graph using Piecewise and be able to ...
3
votes
1answer
397 views

About NDSolve with piecewise inputs

I am trying to solve numerically two differential equations with NDSolve. ...
10
votes
1answer
733 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 ...
8
votes
0answers
121 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 ...