Skip to main content
25 votes

Difference between Piecewise, Which and If?

The main difference is their intended use. Which is a "programming function". It is meant for flow control. It is an equivalent of ...
Szabolcs's user avatar
  • 235k
21 votes

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

Important Update Through a copy-paste mistake, I dropped the temperature dependence on the effective heat capacity, which was the point of the exercise. I have modified the post to correct that ...
Tim Laska's user avatar
  • 16.4k
18 votes

Extracting the function from InterpolatingFunction object

In M11+ you can use the "GetPolynomial" method of an interpolating function to obtain the corresponding piecewise expression (but only when using the default "Hermite" method): ...
Carl Woll's user avatar
  • 131k
17 votes
Accepted

Piecewise-constant function with infinitely many pieces

That $F$ should really be something like $F_n$, defining a family of functions and then your total $F$ would really be the union of the $\{F_n\}$ over the domains where they are non-zero. Then ...
b3m2a1's user avatar
  • 46.9k
17 votes

Piecewise-constant function with infinitely many pieces

"... I need it only as a function of x" ClearAll[f] f[x_] := Min[x - Quotient[x, 2], 1 + Quotient[x, 2]] Plot[f[x], {x, 0, 10}] ...
kglr's user avatar
  • 397k
16 votes
Accepted

How to plot a list to look like step function?

...
Anjan Kumar's user avatar
  • 4,999
14 votes
Accepted

Can one compile the Switch function?

A drawback of the Which command is that it evaluates x repeatedly until one of the conditions of Which is satisfied. This is time consuming when x is a complicated expression or/and when the set of ...
Szabolcs's user avatar
  • 235k
14 votes
Accepted

How to set up a spherically symmetric Fourier transform?

Summary: To perform the 3D Fourier Transform of a spherically symmetric function $f(r)$ in Mathematica, use the command (4 Pi)/k FourierSinTransform[f[r] r, r, k] ...
Michael Seifert's user avatar
13 votes

Extracting the function from InterpolatingFunction object

You could use Series. What's necessary is to know which abscissa values were used for the interpolation. Let's generate some fake data. ...
LLlAMnYP's user avatar
  • 11.5k
13 votes
Accepted

StreamPlot No Longer Works With Piecewise Function?

It does seem to be a bug. As a temporary workaround, you can re-express your function in terms of either UnitStep[] or Boole[], ...
J. M.'s missing motivation's user avatar
13 votes
Accepted

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

There is a solution to the problem when the value of the function P1[x,t] on the border does not fall to zero, but to a critical value ...
Alex Trounev's user avatar
  • 45.5k
12 votes

Extracting the function from InterpolatingFunction object

Here is a (mostly) general routine that (tries to) convert a one-dimensional InterpolatingFunction[] into an equivalent ...
J. M.'s missing motivation's user avatar
12 votes
Accepted

How can I plot this piecewise function?

...
cvgmt's user avatar
  • 75.2k
11 votes
Accepted

Alternatives to piecewise

You can use the functions Simplify`PWToUnitStep and Simplify`SimplifyUnitStep to convert a ...
kglr's user avatar
  • 397k
11 votes
Accepted

Problem with Integrate with Piecewise and PrincipalValue

I think this is a bug, because if we transform the Piecewise function into a combination of UnitStep (which is mathematically ...
xzczd's user avatar
  • 66.9k
11 votes

Piecewise-constant function with infinitely many pieces

One more way is as follows. ...
user64494's user avatar
  • 26.7k
11 votes

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

Inspired by Tim's excellent answer I was wondering if it is not possible to use the FEM for this. In Version 12.0 there is no way to do this from top level. However, we still can solve this phase ...
user21's user avatar
  • 39.9k
10 votes
Accepted

What does True mean in this case?

From the documentation of Piecewise: The $\text{cond}_i$ are evaluated in turn, until one of them is found to yield True. ...
Szabolcs's user avatar
  • 235k
10 votes
Accepted

RuleDelayed (:>) involving Piecewise leads to Piecewise::pairs warning in ReplaceAll (/.)

While creating the delayed rule Mathematica evaluates the left hand side of the rule by default. Evaluating Piecewise[a_, b___] returns the error message, but stays ...
kirma's user avatar
  • 19.1k
10 votes

How can I plot this piecewise function?

We can use ExclusionsStyle -> {Dashed, AbsolutePointSize[10]} to style the excluded line and its endpoints without having to know where the discontinuities are. ...
kglr's user avatar
  • 397k
9 votes

Extracting the function from InterpolatingFunction object

Instead of trying to come up with a function that replicates the output of the InterpolatingFunction, one could instead "inactivate" it, and then see what kind of ...
Carl Woll's user avatar
  • 131k
9 votes

How to plot a list to look like step function?

The simplest way to make it is ListLinePlot with option InterpolationOrder: ...
PureLine's user avatar
  • 1,320
9 votes
Accepted

Automatic Piecewise Approximation

...
kglr's user avatar
  • 397k
9 votes
Accepted

Converting Piecewise function to C code

-10 < x < 0 // LogicalExpand // CForm (* -10 < x && x < 0 *)
xzczd's user avatar
  • 66.9k
8 votes
Accepted

Conversion between Piecewise[] and Abs[] representations

You need PiecewiseExpand, with its second argument being Reals: ...
xzczd's user avatar
  • 66.9k
8 votes

How to plot a list to look like step function?

Also, if you have a version 10.2 or above, you could use ListStepPlot Module[ {l}, l = {0, 2, 5, 9, 14}; ListStepPlot[l] ] ...
e.doroskevic's user avatar
  • 5,969
8 votes
Accepted

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

Although DSolve cannot solve this problem directly, it can make progress if given some assistance. First consider the corresponding problem with no boundary ...
bbgodfrey's user avatar
  • 61.6k
8 votes

Plotting a function discontinuous at one point

Without having to manually look: ...
Mariusz Iwaniuk's user avatar
8 votes

Continuous non-piecewise equivalent of smoothstep function?

You want a smooth function or bump function, a function with compact support which is infinitely differentiable. An example for a smooth unit step given in the Wikipedia article is ...
Thies Heidecke's user avatar
8 votes
Accepted

The correct way of compute indicator function in Mathematica

...
kglr's user avatar
  • 397k

Only top scored, non community-wiki answers of a minimum length are eligible